Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can only delete an object from the Realm it belongs to

Im getting this error "Can only delete an object from the Realm it belongs to" every time I try to delete an object from realm on my tableview. Here is the relevant code:

let realm = try! Realm()
var checklists = [ChecklistDataModel]()

override func viewWillAppear(_ animated: Bool) {


    checklists = []
    let getChecklists = realm.objects(ChecklistDataModel.self)

    for item in getChecklists{

        let newChecklist = ChecklistDataModel()
        newChecklist.name = item.name
        newChecklist.note = item.note

        checklists.append(newChecklist)
    }

    tableView.reloadData()

}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return checklists.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "ChecklistCell", for: indexPath) as! ListsTableViewCell

    cell.name.text = checklists[indexPath.row].name
    return cell
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {

        // Delete the row from the data source
        try! realm.write {
            realm.delete(checklists[indexPath.row])
        }

        //delete locally
        checklists.remove(at: indexPath.row)

        self.tableView.deleteRows(at: [indexPath], with: .fade)
    }
}

I know it is this part to be specific:

     // Delete the row from the data source
        try! realm.write {
            realm.delete(checklists[indexPath.row])
        }

Any ideas of what is going on? Thanks in advance!

like image 607
Noah-1 Avatar asked May 09 '17 04:05

Noah-1


People also ask

How do I Remove objects from a realm?

In the Security section, click Realms. The list of realms in the mount point is displayed. Select a realm and click the Edit button. The Edit Realm wizard displays. Click Next to move to the Realm Objects page where you can click Remove to delete objects from the realm.

What happens when a command rule is deleted from a realm?

If a command rule is deleted from a realm, it is not evaluated while enforcing security on the realm. The command rule still continues to exist for use in other realms. If an encryption object is removed, it disables encryption on the realm and results in decryption of all encrypted files in the realm.

What happens when I remove an encryption object from a realm?

If an encryption object is removed, it disables encryption on the realm and results in decryption of all encrypted files in the realm. To delete an object from a realm, follow these steps:

How do I remove a realm from a mount point?

Click the Security/Encryption tab. In the Security section, click Realms. The list of realms in the mount point is displayed. Select a realm and click the Edit button. The Edit Realm wizard displays. Click Next to move to the Realm Objects page where you can click Remove to delete objects from the realm. Click Done.


2 Answers

You are trying to delete copies of your Realm objects stored in a collection instead of your actual Realm objects stored in Realm.

try! realm.write {
    realm.delete(Realm.objects(ChecklistDataModel.self).filter("name=%@",checklists[indexPath.row].name))
}

Without the definition of CheklistDataModel, I am not sure if I got the NSPredicate right, but you should be able to figure it out from here.

like image 81
Dávid Pásztor Avatar answered Sep 19 '22 21:09

Dávid Pásztor


From the code snippets you shared, you appear to be creating new ChecklistDataModel objects but never adding them to any Realm. Then you attempt to delete these objects from your Realm in your try! realm.write block.

Simply instantiating an object does not mean it has been added to a Realm; until it is added to a Realm through a successful write transaction it behaves just like any other Swift instance. Only after you've added the object to a Realm can you successfully delete it from that same Realm.

like image 26
AustinZ Avatar answered Sep 22 '22 21:09

AustinZ