0

コアデータで同時検索を実行する方法を理解しようとしています。

これが私の例ですが、GCD の 1 つがアクティブにならないように見えるため、機能しません。カスタム MOC をそこに残しておくと、「エンティティ 'レシピ' のモデルが見つかりません」というエラーが表示されます。

-(void)performSearch:(NSString*)name{

    //TODO: Is there a better way

    //In case the previous search hasn't finished
    if (globaDispatchRequestInprogress) {
        //Send on GCD
        dispatch_queue_t searchQueque = dispatch_queue_create("search queque 2", NULL);
        dispatch_async(searchQueque, ^{
            NSLog(@"\n\nDispatch 2 In Progress*******\n\n");


            //Init local variables
            NSFetchRequest *fetchRequest =[[NSFetchRequest alloc]init];
            NSError *error;


             //Create own MOC for multiThread
             NSManagedObjectContext *tempContext = [[NSManagedObjectContext alloc]init];

             [tempContext setPersistentStoreCoordinator:[self persistentStoreCoordinator]];


            NSPredicate *recipeName = [NSPredicate predicateWithFormat:@"ANY recipe.name ==[c] %@",name];

            //Set predicate to fetch request
            [fetchRequest setPredicate:recipeName];

            //Set query. We are searching recipes
            NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:tempContext];
            //sets up fetch request details
            [fetchRequest setEntity:entity];

            //Attempt to speed up program
            [fetchRequest setReturnsObjectsAsFaults:NO];


            //Perform fetch assign to return array
            NSArray*records = [tempContext executeFetchRequest:fetchRequest error:&error];

            //Add to temporary results
            //TODO: Add to NSDictionary
            [self addResultsToTemporaryResults:records];

            NSLog(@"Total results = %i",[_temporaryResultsArray count]);

            NSLog(@"\n\nDispatch 2 END**************\n\n");

        });

    }
    //Send on GCD
    dispatch_queue_t searchQueque = dispatch_queue_create("search queque", NULL);
    dispatch_async(searchQueque, ^{
        NSLog(@"\n\nDispatch In Progress*******\n\n");

        //Set flag
        globaDispatchRequestInprogress=YES;

        //Init local variables
        NSFetchRequest *fetchRequest =[[NSFetchRequest alloc]init];
        NSError *error;


         //Create own MOC for multiThread
         NSManagedObjectContext *tempContext = [[NSManagedObjectContext alloc]init];

         [tempContext setPersistentStoreCoordinator:[self persistentStoreCoordinator]];

        NSPredicate *recipeName = [NSPredicate predicateWithFormat:@"ANY recipe.name ==[c] %@",name];

        //Set predicate to fetch request
        [fetchRequest setPredicate:recipeName];

        //Set query. We are searching recipes
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Recipe" inManagedObjectContext:tempContext];
        //sets up fetch request details
        [fetchRequest setEntity:entity];

        //Attempt to speed up program
        [fetchRequest setReturnsObjectsAsFaults:NO];

        //Perform fetch assign to return array
        NSArray*records = [tempContext executeFetchRequest:fetchRequest error:&error];

        //Add to temporary results
        //TODO: Add to NSDictionary
        [self addResultsToTemporaryResults:records];

        NSLog(@"Total results = %i",[_temporaryResultsArray count]);
        globaDispatchRequestInprogress=NO;

        NSLog(@"\n\nDispatch END**************\n\n");

    });

}
4

1 に答える 1

2

疑わしい点がいくつかありますが、明白な決定的な証拠はありません。

「モデルが見つかりません」というメッセージが表示される場合は、永続ストア コーディネーターが想定どおりに構成されていないことを示しています。NSLog self.persistentStoreCoordinator.managedObjectModel、および self.persistentStoreCoordinator.managedObjectModel.entitiesByName も興味深いでしょう。

Core Data に対する推奨される GCD のアプローチは、管理対象オブジェクト コンテキストに適切な同時実行タイプを指定して、performBlock: または performBlockAndWait: を使用することです。http://developer.apple.com/library/mac/#releasenotes/DataManagement/RN-CoreData/index.htmlを参照してください。

addResultsToTemporaryResults: 呼び出しで、フェッチの結果を保持しています。ソースはわかりませんが、スレッドセーフですか? 見つけたレコードは、フェッチした tempContext の外には存在せず、それらを見つけたスレッドからのみアクセスできます。おそらく、そこで NSManagedObjectIDs を使用したいと思うでしょう (そしておそらく既に使用しています)。

dispatch_queue_create() への 2 回目の呼び出しは常に実行されます。単純な if の代わりに if-else を実行するつもりでしたか?

-executeFetchRequest:error: を実行したら、結果を確認します。結果が nil の場合は、渡した NSError を見てください。

于 2013-03-21T21:33:39.080 に答える