0

我想了解如何在覈心數據中同時執行搜索。如何在覈心數據中同時執行搜索?

這裏是我的榜樣,但它不工作,因爲GCDS的人似乎從來沒有激活 如果我離開custon 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"); 

    }); 

} 

回答

2

我看到幾件事情,讓我懷疑,但沒有明顯的確鑿證據。

如果你看到「無法找到模型」,即意味着你的持久性存儲協調員未被配置方式你認爲它是這樣的,NSLog self.persistentStoreCoordinator.managedObjectModel和self.persistentStoreCoor dinator.managedObjectModel.entitiesByName。

核心數據的首選GCD方法是使用performBlock:或performBlockAndWait:,併爲您的託管對象上下文使用適當的併發類型。請參閱http://developer.apple.com/library/mac/#releasenotes/DataManagement/RN-CoreData/index.html

您在addResultsToTemporaryResults:call中保留了您的抓取結果。我們沒有看到它的來源,但它是線程安全的嗎?您發現的那些記錄在您提取它們的tempContext之外不存在,並且只能從發現它們的線程訪問。你可能想在那裏使用NSManagedObjectIDs(也許你已經是)。

您的第二次調用dispatch_queue_create()將始終執行。你是否想要做一個if-else而不是簡單的if?

當您執行-executeFetchRequest:error:時,請檢查結果。如果這是一個零結果,請看看你通過的NSError。

+0

+1這就是@waf首先應該考慮的問題。 – 2013-03-22 01:42:51

+0

有趣,將檢查出來。謝謝 – 2013-03-23 01:33:59