Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 616
Peter Lapisu Avatar asked Sep 09 '15 11:09

Peter Lapisu


1 Answers

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.

like image 122
yoshyosh Avatar answered Nov 15 '22 19:11

yoshyosh