领域 - 在后台线程上批量更新 RLMResults
Realm - Batch update RLMResults on background thread
我有需要迭代的 RLMResults,做一个潜在的 "long-running" 下载任务(足够长的时间它不应该在主线程上),并用这个下载的结果更新每个对象.我尝试的最新迭代(在搜索文档寻找答案后)是这样的,虽然这显然不能按预期工作,但它是演示目的的起点:
RLMResults *objectsToSaveImagesFor = [self allObjectsToSaveImagesFor];
for (Object *object in objectsToSaveImagesFor) {
RLMThreadSafeReference *objectRef = [RLMThreadSafeReference referenceWithThreadConfined:object];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
RLMRealm *realm = [RLMRealm realmWithConfiguration:self.realm.configuration error:nil];
Object *threadSafeObject = [realm resolveThreadSafeReference:objectRef];
BOOL success = [self downloadImageForObject:threadSafeObject];
[realm transactionWithBlock:^{
threadSafeObject.imageSaved = success;
}];
});
}
我已经尝试了大约十二次迭代,但无法找到规范的 Realm 方法来做我想做的事情,即下载大量图像(数千张)并使用后台线程上的下载结果更新我的每个 Realm 对象。
与其为 RLMResults
中的每个对象创建和解析线程安全引用,不如只做一次:
RLMResults *objectsToSaveImagesFor = [self allObjectsToSaveImagesFor];
RLMThreadSafeReference *objectsRef = [RLMThreadSafeReference referenceWithThreadConfined:objectsToSaveImagesFor];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
RLMRealm *realm = [RLMRealm realmWithConfiguration:self.realm.configuration error:nil];
RLMResults *objectsToSaveImagesFor2 = [realm resolveThreadSafeReference:objectsRef];
for (Object *object in objectsToSaveImagesFor2) {
BOOL success = [self downloadImageForObject:threadSafeObject];
[realm transactionWithBlock:^{
object.imageSaved = success;
}];
}
});
我有需要迭代的 RLMResults,做一个潜在的 "long-running" 下载任务(足够长的时间它不应该在主线程上),并用这个下载的结果更新每个对象.我尝试的最新迭代(在搜索文档寻找答案后)是这样的,虽然这显然不能按预期工作,但它是演示目的的起点:
RLMResults *objectsToSaveImagesFor = [self allObjectsToSaveImagesFor];
for (Object *object in objectsToSaveImagesFor) {
RLMThreadSafeReference *objectRef = [RLMThreadSafeReference referenceWithThreadConfined:object];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
RLMRealm *realm = [RLMRealm realmWithConfiguration:self.realm.configuration error:nil];
Object *threadSafeObject = [realm resolveThreadSafeReference:objectRef];
BOOL success = [self downloadImageForObject:threadSafeObject];
[realm transactionWithBlock:^{
threadSafeObject.imageSaved = success;
}];
});
}
我已经尝试了大约十二次迭代,但无法找到规范的 Realm 方法来做我想做的事情,即下载大量图像(数千张)并使用后台线程上的下载结果更新我的每个 Realm 对象。
与其为 RLMResults
中的每个对象创建和解析线程安全引用,不如只做一次:
RLMResults *objectsToSaveImagesFor = [self allObjectsToSaveImagesFor];
RLMThreadSafeReference *objectsRef = [RLMThreadSafeReference referenceWithThreadConfined:objectsToSaveImagesFor];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
RLMRealm *realm = [RLMRealm realmWithConfiguration:self.realm.configuration error:nil];
RLMResults *objectsToSaveImagesFor2 = [realm resolveThreadSafeReference:objectsRef];
for (Object *object in objectsToSaveImagesFor2) {
BOOL success = [self downloadImageForObject:threadSafeObject];
[realm transactionWithBlock:^{
object.imageSaved = success;
}];
}
});