Realm,无法从写入事务中注册通知块



我正在使用一个函数将领域对象保存到我的服务器。它被称为使用DataManager().updatedCategory(~InsertCategory~)。执行是这样的;

func updatedCategory(_ c: Category) {
guard let realm = try? Realm() else { return }
guard let category = realm.object(ofType: Category.self, forPrimaryKey: c.id) else { return }
do {
try realm.write {
c.updatedAt = Date()
}
} catch {
print(error)
}
dbCategories.document(category.id).updateData(category.dictionaryValue())
}

此函数是在使用realm.write块之后调用的。它不在另一个写块中,甚至在调用之前UI也在更新。我不知道是什么原因导致了这个错误,因为没有注册通知。

我认为这个

try realm.write {
c.updatedAt = Date()
}

应该是

try realm.write {
category.updatedAt = Date()
}

另外,这个

dbCategories.document(category.id).updateData(category.dictionaryValue())

看起来您也在尝试将数据写入领域(?(。如果是这样的话,它需要在写闭包中。如果没有,请澄清它的作用,以便我可以更新答案。

最后,有点不清楚

This function is being called after using a realm.write block.

您不能将写闭包嵌入另一个闭包中,添加观察者也应该发生在写闭包之外——错误表明观察者附加在闭包中

最新更新