Okhan Okbay
Okhan Okbay

Reputation: 1394

Autoreleasepool in background queue for Realm

In the example project of RealmCocoa:

// Multi-threading
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    RLMRealm *otherRealm = [RLMRealm defaultRealm];
    RLMResults *otherResults = [Dog objectsInRealm:otherRealm where:@"name contains 'Rex'"];
    NSLog(@"Number of dogs: %li", (unsigned long)otherResults.count);
});

In the website docs:

dispatch_async(dispatch_queue_create("background", 0), ^{
    @autoreleasepool {
        Dog *theDog = [[Dog objectsWhere:@"age == 1"] firstObject];
        RLMRealm *realm = [RLMRealm defaultRealm];
        [realm beginWriteTransaction];
        theDog.age = 3;
        [realm commitWriteTransaction];
    }
});

Upvotes: 0

Views: 550

Answers (1)

bdash
bdash

Reputation: 18308

Why doesn't the example project have autoreleasepool?

It’s an oversight.

Should the example project's code also include an autoreleasepool?

Yes.

Which one of these usage is more appropriate?

The latter. As Realm’s documentation on threading states, all use of Realm from background threads should be contained within explicit autorelease pools.

Upvotes: 1

Related Questions