我遇到了这个问题,在更新现有记录时,它会崩溃我的应用程序。这是我的代码:
if self.statisticsArray.count == 0 {
try! self.realm.write({
self.statistics.summary = 500
self.realm.add(self.statistics)
self.realm.add(record)
})
} else {
if day == self.statisticsArray.last?.date {
try! self.realm.write({
self.realm.objects(Statistics).last?.summary += 500
self.realm.add(record)
})
} else {
try! self.realm.write({
self.statistics.summary = (self.statisticsArray.last?.summary)! + 500
self.realm.add(self.statistics)
self.realm.add(record)
})
}
我已将所有更新流程放入write
块中,那么为什么它会崩溃我的应用?任何人都可以形容我,这里出了什么问题,我该如何解决?
错误:
Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'
更新
do {
try self.realm.write({
self.statistics.mlSummary = 250
self.realm.add(self.statistics)
self.realm.add(record)
})
} catch let error {
print("E \(error)")
}