在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);
});
在网站文档中:
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];
}
});
答案 0 :(得分:1)
为什么示例项目没有autoreleasepool?
这是一种疏忽。
示例项目的代码是否也包含autoreleasepool?
是
这些用法中哪一种更合适?
后者。作为Realm’s documentation on threading states,所有使用后台线程中的Realm都应包含在显式自动释放池中。