当我在后台队列中添加新对象时,我正在使用RealmSwift + RBQFetchedResultsController尝试获取自动tableview更新,而我似乎错过了一个步骤。
以前,我没有发出任何通知,我的FetchedResultsController
在初始人口之后是惰性的。我可以在我的后台线程中毫无问题地添加对象。
现在,我已将realm.add()
次调用更改为提供给realm.addWithNotification()
的RBQ,现在我在通知触发后发生此崩溃:*** Terminating app due to uncaught exception 'Invalid value origin', reason: 'Object must be from the Realm being queried'
我可能遇到过RBQFetchResultsController的错误,但似乎我在模型层中做错了。一些相关的代码:
class func threadedRealm() -> Realm {
if NSThread.currentThread() == NSThread.mainThread() {
return mainRealm
}
if let realm = self.threadedRealms[NSThread.currentThread()] {
return realm
}
let realm = try! Realm()
self.threadedRealms[NSThread.currentThread()] = realm
return realm
}
class func add(object: Object) -> Realm {
let realm = self.threadedRealm()
let type = object.dynamicType
do {
try realm.write {
//If the object's class has a primary key, update/insert it, otherwise just insert it
realm.addWithNotification(object, update: type.primaryKey() != nil)
}
} catch {
print(error)
}
return realm
}
//called from a background thread
static func saveMessage(json: JSON, chatThread: ChatThread) -> Message {
let message: Message
let results = DataStore.threadedRealm().objects(Message).filter("id = %@", json["id"])
if results.first != nil {
message = results.first!
} else {
message = Message(chatId: json["chatId"].stringValue, id: json["id"].stringValue)
message.chatThread = chatThread
DataStore.add(message) // crash here
}
return message
}
我已经采取了一些调试步骤:
message
和chatThread
具有相同的域,而threadedRealm()
正在返回我在整个过程中所期望的内容。这是我用尽的想法。它看起来像一个非常简单的用例,它反映了RBQFetchedResultsController在其文档页面上的建议,但我必须遗漏一些东西。如果有人有任何建议,我们将不胜感激。
哦,这是一个堆栈跟踪,万一有帮助。
#1 in RLMPrecondition(bool, NSString*, NSString*, ...)
#2 in (anonymous namespace)::add_link_constraint_to_query(realm::Query&, NSPredicateOperatorType, (anonymous namespace)::ColumnReference const&, RLMObject*)
#3 in void (anonymous namespace)::do_add_constraint_to_query<(anonymous namespace)::ColumnReference, objc_object* __strong>(realm::Query&, RLMPropertyType, NSPredicateOperatorType, unsigned long, (anonymous namespace)::ColumnReference, objc_object* __strong)
#4 in void (anonymous namespace)::add_constraint_to_query<(anonymous namespace)::ColumnReference, objc_object* __strong>(realm::Query&, RLMPropertyType, NSPredicateOperatorType, unsigned long, (anonymous namespace)::ColumnReference, objc_object* __strong)
#5 in (anonymous namespace)::update_query_with_value_expression(RLMSchema*, RLMObjectSchema*, realm::Query&, NSString*, objc_object*, NSComparisonPredicate*)
#6 in (anonymous namespace)::update_query_with_predicate(NSPredicate*, RLMSchema*, RLMObjectSchema*, realm::Query&)
#7 in (anonymous namespace)::update_query_with_predicate(NSPredicate*, RLMSchema*, RLMObjectSchema*, realm::Query&)
#8 in RLMUpdateQueryWithPredicate(realm::Query*, NSPredicate*, RLMSchema*, RLMObjectSchema*)
#9 in -[RLMResults objectsWithPredicate:]::$_10::operator()() const
#10 in auto translateErrors<-[RLMResults objectsWithPredicate:]::$_10>(-[RLMResults objectsWithPredicate:]::$_10&&, NSString*)
#11 in -[RLMResults objectsWithPredicate:]
#12 in -[RBQFetchRequest fetchObjects]
#13 in -[RBQFetchedResultsController createStateObjectWithFetchRequest:realm:cache:cacheRealm:]
#14 in -[RBQFetchedResultsController calculateChangesWithAddedSafeObjects:deletedSafeObjects:changedSafeObjects:realm:]
#15 in __58-[RBQFetchedResultsController registerChangeNotifications]_block_invoke
#16 in -[RBQRealmNotificationManager sendNotificationsWithRealm:entityChanges:]
#17 in __50-[RBQRealmChangeLogger registerChangeNotification]_block_invoke
#18 in -[RLMRealm sendNotifications:]
#19 in (anonymous namespace)::RLMNotificationHelper::did_change(std::__1::vector<realm::BindingContext::ObserverState, std::__1::allocator<realm::BindingContext::ObserverState> > const&, std::__1::vector<void*, std::__1::allocator<void*> > const&)
#20 in realm::_impl::transaction::commit(realm::SharedGroup&, realm::BindingContext*)
#21 in realm::Realm::commit_transaction()
#22 in -[RLMRealm commitWriteTransaction:]
#23 in -[RLMRealm transactionWithBlock:error:]
#24 in Realm.write(() -> ()) throws -> ()
#25 in static DataStore.add(Object) -> Realm
#26 in static Message.saveMessage(JSON, chatThread : ChatThread) -> Message
#27 in static Message.messagesFromAPIResponse(APIResponse) -> ()