Peter Lapisu
Peter Lapisu

Reputation: 20965

Realm fetching objects on background thread and pass to main thread

I want to fetch large amount of objects on the background thread, however i cannot pass them to the main thread, as i get

*** Terminating app due to uncaught exception 'RLMException', reason: 'Realm accessed from incorrect thread'

fetch code

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){

    //Background Thread
    RLMRealm * realm = [RLMRealm defaultRealm];
    self.allObjectsRLMResult = [MyClass allObjectsInRealm:realm];

    dispatch_async(dispatch_get_main_queue(), ^(void){

        // use self.allObjects and do stuff on main thread

    });

});

How to perform a fetch on the background and pass the object to the main thread so there is minimal performance impact

I could get the primary keys, and then refetch on the main thread, but this will be the same performance (possibly even slower) as fetching them directly

Upvotes: 10

Views: 3493

Answers (1)

yoshyosh
yoshyosh

Reputation: 14086

Joe from Realm here. Currently what you described (getting primary keys) is the best way to do it. We are aware of this and have been looking into a thread handover solution but for now that will be the best way for you to get your objects.

Upvotes: 6

Related Questions