简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM