Ios 删除领域对象会导致一个func崩溃,而不是另一个func崩溃
我有两个几乎相同的函数。这个很好用:Ios 删除领域对象会导致一个func崩溃,而不是另一个func崩溃,ios,swift,realm,Ios,Swift,Realm,我有两个几乎相同的函数。这个很好用: func deleteEveryDamnThing(){ let allAccounts = realm.objects(Account.self) let allTransactions = realm.objects(Transaction.self) try! realm.write { realm.delete(allAccounts) realm.delete(allTransactions
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()
}
然而,这一点:
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()
}
解释如下:
Terminating app due to uncaught exception 'RLMException', reason: 'Object has been deleted or invalidated.'
第二个函数
只删除域中的self.currentAccount
,而第一个函数删除域中的一切,包括self.currentAccount
。两个func
都位于同一视图控制器class
,并且都是从同一类调用的
如果有必要的话,我正在使用领域通知
有人有什么想法吗
谢谢你的关注 我认为问题可能是您可能调用了函数actuallyDeleteCurrentAccount()
两次,或者从其他地方删除了该函数
另一个原因可能是,在通知令牌上,您假设对象存在,但该对象刚刚被删除
我建议您使用一些打印或断点来缩小问题的范围。
你也可以做这样的检查
try! realm.write {
thisAccount.invalidated == false {
realm.delete(thisAccount)
}
//otherwise the object is already invalidated
}
然后,您可以使用领域浏览器检查该对象是否确实已被删除。谢谢,@David!我试过这个,但得到了同样的结果。这就像是在告诉我“我不能按你的要求去做,因为已经做了。”当然,证据表明还没有。我不能使用领域浏览器,除非我误解了,因为它只能与模拟器一起使用。我不能使用模拟器,因为我使用的是我的iPhone摄像头,而且模拟器启动需要大约45分钟。如果我能找到解决办法,我会向你汇报。。。