更新领域上的对象

Updating an object on 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.'

我该如何解决?

尝试以下操作:

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

如异常消息所述,问题是您在写入事务之外修改对象。在交易中做那个突变应该可以解决问题![​​=11=]