Way to Purge All But One Object Types (Models) in a Realm

way to purge all but one object types (models) in a realm

You can access the types from your Realm configuration, filter them to exclude the one you want to keep than delete each object of each type that you don't want to keep.

let typeToBeKept = MyObjectClass.self
realm.configuration.objectTypes?.filter{$0 != typeToBeKept}.forEach{ type in
try! realm.write {
realm.delete(realm.objects(type.self))
}
}

way to purge all but one object types (models) in a realm

You can access the types from your Realm configuration, filter them to exclude the one you want to keep than delete each object of each type that you don't want to keep.

let typeToBeKept = MyObjectClass.self
realm.configuration.objectTypes?.filter{$0 != typeToBeKept}.forEach{ type in
try! realm.write {
realm.delete(realm.objects(type.self))
}
}

How to delete all objects except one type?

According to Dávid's answer:

func deleteAll(except types: Object.Type...) {
guard let realm = realm else { return }

try? realm.write {
realm.configuration.objectTypes?.filter{ type in types.contains{ $0 == type } == false}.forEach{ objectType in
realm.delete(realm.objects(objectType.self))
}
}
}

Usage:

deleteAll(except: Dog.self, Chicken.self)

how to remove all objects that belong to the same object in Realm in Swift

At present, Realm doesn't automatically delete objects that are linked from the object intended for deletion (Although it's on our roadmap!)

At the moment, it will be necessary to manually delete the linked LifelineProductModel objects yourself before deleting the parent LifelineModel.

How to remove all models that are not contained in the current array in Realm

Realm doesn't have any built-in functionality to achieve what you want. An alternative to what you have is to leverage Swift's Set type.

First, you should make sure equality and the hash value property are defined for your model class, and you should probably have a primary key (which seems to be id).

Next, turn your arrays of existing and new models into Sets. Set has an initializer that takes any sequence of objects, so you can pass arrays and Realm Lists alike into it.

Next, use the subtract(_:) method on Set to get a new set containing just the elements you want to delete:

// itemsToDelete contains models in oldModels that aren't in newModels
let itemsToDelete = oldModels.subtract(newModels)

Finally, you can use the Realm delete(_:) method that takes a sequence to delete all the obsolete models at once:

try! realm.write {
realm.delete(itemsToDelete)
}

I hope this helps. Note that this isn't a panacea; if your arrays are huge you may want to consider whether this approach uses too much memory.

How can I easily delete all objects in a Realm

Use deleteAll():

let realm = try! Realm()
try! realm.write {
realm.deleteAll()
}

Delete all data from specific Realm Object Swift

You're looking for this:

let realm = Realm()
let deletedValue = "two"
realm.write {
let deletedNotifications = realm.objects(Notifications).filter("value == %@", deletedValue)
realm.delete(deletedNotifications)
}

or perhaps this:

let realm = Realm()
let serverValues = ["one", "three"]
realm.write {
realm.delete(realm.objects(Notifications)) // deletes all 'Notifications' objects from the realm
for value in serverValues {
let notification = Notifications()
notification.value = value
realm.add(notification)
}
}

Although ideally, you'd be setting a primary key on Notifications so that you can simply update those existing objects rather than taking the extreme approach of nuking all your local objects simply to recreate them all (or almost).

Delete Realm Object By Value

The issue is this statement

I am moving files into the folder

While that may be what you want to do, the objects you're moving aren't actually being moved. When you add a managed object to a a List property, it adds a reference to the object, not the object itself.

In other words, when a FilesModel is added to a GroupsModel files List property, it's a reference to the object. If you delete the FilesModel, it will also be gone from the List. But, removing it from the List does not change the original object.

However, Lists can also contain Embedded Objects - an embedded object only ever exists within it's parent object. Perhaps that would be an option?

So like this. Here's a couple of models

class FileModel: EmbeddedObject {
@Persisted var fileName = ""
}

class GroupsModel: Object {
@Persisted var files = List<FilesModel>()
}

Then instantiate a group and add a couple of file objects to it

let group = GroupsModel()

let f0 = FileModel()
f0.fileName = "some filename"

let f1 = FileModel()
f1.fileName = "another filename"

group.files.append(objectsIn: [f0, f1])

results in a group with two files that only ever appear within that group.

Another option is to simply make a copy of the object

let objectCopy = MyObject(value: objectToCopy)

and then add the objectCopy to your Groups model. But again, by doing that Realm will instantiate the objectCopy and actually add it back to Realm (possibly overwriting the original object).

deleting object from realm database.

I found out that that problem comes from "delete" word. It seems that this word preserved for Xcode. When I call it , it goes to another delete method in another place not mine. I changed the name to for example deleteSomethings and it worked.



Related Topics



Leave a reply



Submit