Realm Crashes with Rlmexception: Object Has Been Deleted or Invalidated

Realm crashing with RLMException Object has been deleted or invalidated when deleting from the database

Because your array is not updated after you are deleting and item.
// Observe Realm database for changes and reload tableview

projectsToken = realm.observe { (notification, realm) in
projects = getProjectsForDay(day: currentDate)
self.dayViewTableView.reloadData()
}

This should fix it.
Your object will remain in array because realm will not delete it from there, and when tableView will try to reload it's data it will crash at the reference of the deleted item. What are you trying to achieve is done with var projects: Results.
And you are doing a bit of overkill observing the whole realm. You can add observer on a specific query, but it's more hard to achieve that kind of live data when you are modifying the day.

And please don't name properties with capital letters.

Realm crashes with RLMException: object has been deleted or invalidated

When the RLMProject itself is invalidated, it won't be possible to check for videoAssets.invalidated or soundtracks.invalidated, which is why your stack trace shows that an uncaught exception is being thrown in RLMGetArray.

This implies that the object has been deleted from the realm before you call setTimeLineModel. Please look for realm.deleteObject(_:) in your code to see where this might be happening.

Finally, a few general tips to make your code a little safer and simpler:

Instead of RLMRealm.defaultRealm() everywhere inside your RLMObject, you should use its realm property. This way, if you decide to change the location of your realm at some point, your code inside your RLMObject will continue to work.

Also, rather than create an extension on RLMArray to add map, you could use Swift's free map function.

map(videoAssets) { ($0 as RLMMediaAsset).getMediaAsset() }

Object has been deleted or invalidated. (Realm)

Normally, that error should only be thrown if you try and access a property of a Realm object that has been deleted, or if you explicitly told its parent Realm object to invalidate.

Like James said, it's quite likely that your product variable there has already been invalidated, in which case trying to call product.id would likely cause that crash.

If that's the case, then the easiest thing to do to fix this would be to avoid using the product variable and instead, simply making a copy of the value of id directly. This way, if the object is deleted/invalidated, you still have its primary key in which you can test to see if it still exists.

On a side note, this code could certainly be made a bit more efficient as well. It's not necessary to perform queries inside write transactions and you should only open a write transaction if there actually was an object to delete (Write transactions are pretty heavy things, so they should be avoided as much as possible).

let productID = product.id //save a copy of the ID in case 'product' gets deleted.

let realm = try! Realm()

let dbProduct = realm.objectForPrimaryKey(DBProduct.self, key: productID)
if dbProduct != nil {
try! realm.write {
realm.delete(dbProduct!)
}
}

I hope that helped!



Related Topics



Leave a reply



Submit