更新领域上的对象

时间:2015-05-05 12:02:49

标签: ios realm

我想用realm更新一个对象。但我的应用程序崩溃了。

我的代码:

var findConversations = ShufflePerson.objectsWhere("receiver='\(from)'")
var shuf = findConversations[0] as! ShufflePerson
shuf.unreadMessage=shuf.unreadMessage++
self.realm.beginWriteTransaction()
self.realm.commitWriteTransaction()

崩溃日志:

Terminating app due to uncaught exception 'RLMException', reason: 'Attempting to modify object outside of a write transaction - call beginWriteTransaction on an RLMRealm instance first.'

我该如何解决?

1 个答案:

答案 0 :(得分:2)

尝试以下方法:

var findConversations = ShufflePerson.objectsWhere("receiver='\(from)'")
var shuf = findConversations[0] as! ShufflePerson
self.realm.beginWriteTransaction()
shuf.unreadMessage++
self.realm.commitWriteTransaction()

正如异常消息所说,问题在于您正在修改写入事务之外的对象。在事务中执行该突变应该可以解决问题!