大多數情況下,我會得到所有結果,但有時候我不會,這顯然是一個問題。我有這樣一個功能:領域有時不會返回最新結果
func addProduct(thisProduct:Product) {
let realm = try! Realm()
try! realm.write {
realm.add(thisProduct)
}
countProductsAddedToday()
// sometimes doesn't include the just added product to the count
}
問題是我的功能countProductsAddedtoday()
並不總是返回產品的正確數量的寫入事務後立即調用像在這種情況下。如果稍後調用它(通過點擊我的應用中的按鈕),它將返回正確的計數。 我希望這是一個普遍的問題,我正在做一些我不應該做的事情。
功能countProductsAddedToday()
是這樣的:
func countProductsAddedToday() -> Int {
let predicate = NSPredicate(format: "timeAdded >= %d", unixTimestampForToday)
let realm = try! Realm()
let results = realm.objects(Product).filter(predicate)
return results.count
}
其他信息:我不能肯定,但我認爲很快加時的產品,但隨後停止發生問題的情況經常發生。有沒有延遲寫入或緩存結果或類似的東西?
添加一個物理設備上的產品時,我得到的錯誤:
0 Realm 0x000000010042d7e0 _ZN5realm4util18terminate_internalERNSt3__118basic_stringstreamIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE + 44
1 Realm 0x000000010042da0c _ZN5realm4util9terminateEPKcS2_l + 340
2 Realm 0x00000001002a70ac _ZN5realm5_impl16RealmCoordinator19run_async_notifiersEv + 2124
3 Realm 0x00000001002a6668 _ZN5realm5_impl16RealmCoordinator9on_changeEv + 40
4 Realm 0x000000010026adf4 _ZN5realm5_impl20ExternalCommitHelper6listenEv + 672
5 Realm 0x000000010026b89c _ZZN5realm5_impl20ExternalCommitHelperC1ERNS0_16RealmCoordinatorEENK3$_0clEv + 24
6 Realm 0x000000010026b878 _ZNSt3__112__async_funcIZN5realm5_impl20ExternalCommitHelperC1ERNS2_16RealmCoordinatorEE3$_0JEE9__executeIJEEEvNS_15__tuple_indicesIJXspT_EEEE + 64
7 Realm 0x000000010026b82c _ZNSt3__112__async_funcIZN5realm5_impl20ExternalCommitHelperC1ERNS2_16RealmCoordinatorEE3$_0JEEclEv + 24
8 Realm 0x000000010026b73c _ZNSt3__119__async_assoc_stateIvNS_12__async_funcIZN5realm5_impl20ExternalCommitHelperC1ERNS3_16RealmCoordinatorEE3$_0JEEEE9__executeEv + 32
9 Realm 0x000000010026bfc0 _ZNSt3__114__thread_proxyINS_5tupleIJMNS_19__async_assoc_stateIvNS_12__async_funcIZN5realm5_impl20ExternalCommitHelperC1ERNS5_16RealmCoordinatorEE3$_0JEEEEEFvvEPSB_EEEEEPvSG_ + 388
10 libsystem_pthread.dylib 0x0000000180cebb28 <redacted> + 156
11 libsystem_pthread.dylib 0x0000000180ceba8c <redacted> + 0
12 libsystem_pthread.dylib 0x0000000180ce9028 thread_start + 4
是'countProductsAddedToday()'調用同一個線程上的示例代碼指示,還是將其分派到不同的隊列或線程? – bdash
我試過兩種情況,兩種情況下都存在同樣的問題。 – TimSim
我已經在模擬器上測試過了。現在我在物理設備上嘗試了它(每秒增加1個產品),它似乎工作正常,但後來發生了一個崩潰'realm_coordinator.cpp:435:[realm-core-0.97.4]斷言失敗:m_advancer_sg- > get_version_of_current_transaction()== new_notifiers.front() - > version()',重複整個事情,它又發生了。很顯然,用戶不會每秒都會添加一個產品,但我喜歡所發生的一切都是一致的,而且這裏有一些我不明白的事情。 – TimSim