2017-03-11 7 views
1

私は、繰り返し実行する必要があるRLMResultsを持っています。(長時間実行している)ダウンロードタスクを実行してください。このダウンロード結果が表示されます。 (答えのためのドキュメントを精練した後)私がしようとしたもので、最新の繰り返し意図したとおり、これは明らかに動作しませんが、このようなものですが、デモの目的のためにその出発点:レルム - バックグラウンドスレッドでRLMResultsを一括更新する

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; 
     }]; 
    }); 
} 

Iこれについて数十回の試行を試みましたが、私がやりたいことをやり遂げる正式なレルムの方法を理解することができません。何千ものイメージをダウンロードし、それぞれのレルムを更新しますバックグラウンドスレッドでのダウンロードの結果を持つオブジェクト。一度だけそれを行う、作成しRLMResults内の各オブジェクトのためのスレッドセーフな参照を解決するのではなく

答えて

1

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; 
     }]; 
    } 
}); 
+1

あなたが全体にスレッドセーフな参照を得ることができる私には発生しませんでしたRLMResults ...それは、ありがとう! – Mike

関連する問題