Swift Remove Object from Realm

How to delete object in realm in Swift

First get Object model in viewDidLoad()

var DatabaseModel: Results<ObjectClassRealm>!

override func viewDidLoad() {
super.viewDidLoad()
let realm = RealmService.shared.realm
DatabaseModel = realm.objects(ObjectClassRealm.self)
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
guard editingStyle == .delete else { return }
let objectData = DatabaseModel[indexPath.row]
RealmService.shared.delete(objectData)
}

Or you can delete on button click

@IBAction func DeleteData(_ sender: Any) {
let realm = try! Realm()
let deleteData = realm.object(ofType: ObjectClassRealm.self,
forPrimaryKey: PRIMARYKEY)
if deleteData == nil {
print("No data in DB")
} else {
RealmService.shared.delete(deleteData!)
}
}

read documents of realm, also you will find many solutions for same question.

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.

Delete objects from List in Realm - Swift

List.removeAll() doesn't delete the objects from Realm. It just removes them out of that List object, deleting their relationship to their parent object (in this case, the favorite object). Deleting objects along with their parent List object is a feature called 'cascading deletes' and it's still being discussed on the Realm GitHub.

If you actually want to delete them, then simply call realm.delete(favorite!.videos). This will delete them from Realm and automatically clear out the List property.

You might need to be careful with your implementation there though. Once an Object has been deleted from Realm, any existing references to it cannot be re-added back into Realm. It may just be appropriate to just delete the newVideo objects themselves instead of cleaning out the whole List.

func removeVideos(at indexes: [Int]) {
let newVideos = [Video]()
for (index, video) in favorite!.videos.enumerated() {
if !indexes.contains(index) {
newVideos.append(video)
}
}

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

As long as you've set a Realm notification block on your collection view, this should be all you need to do for them to be removed from your UI.

Realm Swift - How to remove an item at a specific index position?

Results are auto-updating and you cannot directly modify them. You need to update/add/delete objects in your Realm to effect the state of your Results instance.

So you can simply grab the element you need from your Results instance, delete it from Realm and it will be removed from the Results as well.

Assuming the Results instance shown in your question is stored in a variable called recipes, you can do something like the following:

let recipeToDelete = recipes.filter("id == %@","04b8d81b9e614f1ebb6de41cb0e64432")
try! realm.write {
realm.delete(recipeToDelete)
}

Deleting Objects with subclasses in Swift Realm

You just delete items first.

self.realm.delete(categoryForDeletion.items)
self.realm.delete(categoryForDeletion)

Or, with this extension, you can do this.

self.realm.delete(categoryForDeletion, cascading: true)

Delete specific object from LinkingObjects list - Realm Swift

Try something like this. You only want to remove the lens from the list, not delete it from the Realm.

try! realm.write {
filteredResults.forEach { lens in
if let index = listToSearch[0].lensList.index(of: lens) {
listToSearch[0].lensList.remove(at: index)
}
}
}

Note that this will remove from that one specific list all lenses that match your filter.

Edit: Updated to reflect Realm's custom List class.

The if let is required, because index(of:) could potentially return nil if the object is not found in the list. Additionally, we must do it one item at a time rather than getting all the indexes first, since removing an item would cause the index array to be wrong.



Related Topics



Leave a reply



Submit