What is the cost of refreshing a Realm?

Viewed 1362

I have read the docs and understand that in many cases you do not need to manually call refresh on the Realm instance. However, in this pretty common scenario it turned out to be necessary because the completion block may query the Realm before the start of the next run loop.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [[RLMRealm defaultRealm] transactionWithBlock:^{
        // Add some RLMObjects
    }];

    if (completion) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [[RLMRealm defaultRealm] refresh]; // necessary if it queries realm
            completion();
        });
    }
});

I thought I was being a good citizen by performing write operations on a background thread, but now that I have to call refresh, I'm wondering if the overhead involved in this call defeats the point of doing the background processing.

So my questions are:

1) What is the performance cost of calling refresh on the Realm?

2) Adding just one object to the realm in this pattern probably is pointless. After adding how many objects in this pattern would I see an advantage over the alternative of just performing the write transaction on the main thread synchronously?

1 Answers
Related