Reputation: 1479
I have two nearly identical functions. This one works fine:
func deleteEveryDamnThing(){
let allAccounts = realm.objects(Account.self)
let allTransactions = realm.objects(Transaction.self)
try! realm.write {
realm.delete(allAccounts)
realm.delete(allTransactions)
}
self.activeAcctLabel.text = "No Active Account"
self.currentBalLabel.text = "$0.00"
self.highBalLabel.text = "$0.00"
self.balBarTopConstraint.constant = 505
self.balanceBar.backgroundColor = UIColor.red
self.currentBalLabel.textColor = UIColor.red
self.littleDash.backgroundColor = UIColor.red
self.popNoActiveAccountAlert()
}
However, this one:
func actuallyDeleteCurrentAccount(){
let thisAccount = self.currentAccount
try! realm.write {
realm.delete(thisAccount)
}
self.activeAcctLabel.text = "No Active Account"
self.currentBalLabel.text = "$0.00"
self.highBalLabel.text = "$0.00"
self.balBarTopConstraint.constant = 505
self.balanceBar.backgroundColor = UIColor.red
self.currentBalLabel.textColor = UIColor.red
self.littleDash.backgroundColor = UIColor.red
self.popNoActiveAccountAlert()
}
crashes with the explanation:
Terminating app due to uncaught exception 'RLMException', reason: 'Object has been deleted or invalidated.'
The second func
deletes only self.currentAccount
from the Realm, while the first one deletes everything in the Realm, including self.currentAccount
. Both func
s are in the the same View Controller class
, and are both called from the same class
.
I'm using Realm notifications, if that matters.
Anybody have any ideas?
Thanks for looking!
Upvotes: 1
Views: 720
Reputation: 39
I think that the problem could be that you may be calling the function actuallyDeleteCurrentAccount()
twice or it's getting erased from somewhere else.
Another reason could be that on your notification token you are assuming your object exists but the object just got deleted.
I recommend you using some prints or breakpoints to narrow down the problem. Also you can do some checks like this
try! realm.write {
thisAccount.invalidated == false {
realm.delete(thisAccount)
}
//otherwise the object is already invalidated
}
Then you can use the realm browser to check if the object is in fact deleted.
Upvotes: 1