0

IOS アプリケーションで Core Data を使用していますが、データのフェッチ中にボトルネックが発生しています。ループ内で複数回呼び出しexecuteFetchRequestsて、毎回 1 つの結果を取得しています。各フェッチには短い時間がかかりますが、約 500 回呼び出しているため、フェッチには少なくとも 1 秒かかります。GCD を使用して executeFetchRequest を呼び出すのに問題があります。

私のコードは次のようになります。(問題ではないので、データを保存するコードを削除しました)。

セットアップ コード (これをスレッド化されたコード内に入れる必要があるかどうかはわかりません。どちらの方法でも機能しません)。

    NSManagedObjectContext *context = [self managedObjectContext];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity"
                                              inManagedObjectContext:context];

    NSFetchRequest *fetchrequest = [[NSFetchRequest alloc]init];

    [fetchrequest setEntity:entity];

GCD のセットアップ

    dispatch_group_t x_group = dispatch_group_create();
    dispatch_queue_t x_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

各述語を反復処理する

    for (NSPredicate *predicate in arrayOfPredicates) {

        [fetchrequest setPredicate:predicate];    

        dispatch_group_async(x_group, x_queue, ^{

        NSError *error;

        NSArray *array  = [context executeFetchRequest:fetchrequest error:&error];

        for (Entity *managedObject in array) {

            // save stuff to array inside of thread to pass to an array using locks. 

        }    
        });
    }

    dispatch_group_wait(x_group, DISPATCH_TIME_FOREVER);

... more code here...

ただし、このコードは決して過去のものではなくdispatch_group_wait、このメソッドを使用してフェッチする場合、フェッチされた managedObject は常に空の文字列です。長い遅延期間がないように、これを非同期で行うにはどうすればよいですか?

4

1 に答える 1