TimSim
TimSim

Reputation: 4036

Realm sometimes doesn't return latest results

Most of the time it I get all the results, but sometimes I don't, and that's obviously a problem. I have a function like:

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
}

The problem is my function countProductsAddedtoday() is not always returning the correct number of products when called immediately after a write transaction like in this case. If it gets called later (by tapping a button in my app), it returns the right count. I'm hoping this is a common problem and that I'm doing something I'm not supposed to be doing.

The function countProductsAddedToday() is like this:

func countProductsAddedToday() -> Int {
    let predicate = NSPredicate(format: "timeAdded >= %d", unixTimestampForToday)
    let realm = try! Realm()
    let results = realm.objects(Product).filter(predicate)
    return results.count
}

Additional information: I can't be sure but I think the problem happens more often when adding products quickly, but then stops happening. Is there some delayed writing or caching of results or something like that?

The error I'm getting when adding products on a physical device:

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

Upvotes: 0

Views: 142

Answers (1)

bcamur
bcamur

Reputation: 894

Change your function to:

func countProductsAddedToday() -> Int {
    let predicate = NSPredicate(format: "timeAdded >= %d", unixTimestampForToday)
    let realm = try! Realm()
    realm.refresh()
    let results = realm.objects(Product).filter(predicate)
    return results.count
}

Upvotes: 2

Related Questions