1

你好老鄉overflowersNSFetchedResultsController,核心數據和線程

我有一個很難搞清楚如何在後臺線程獲取NSManagedObjects然後顯示通過一個NSFetchedResultsController的結果。

這是我到目前爲止的代碼:

 _theManagedObjectContext = [[DataManager sharedInstance] mainManagedObjectContext]; 

    __block NSMutableArray *objectsIDs; 

    [[[DataManager sharedInstance] backgroundManagedObjectContext] performBlock:^{ 

     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
     fetchRequest.entity = [NSEntityDescription entityForName:@"Ret" inManagedObjectContext:[[DataManager sharedInstance] backgroundManagedObjectContext]]; 

     NSArray *results = [[[DataManager sharedInstance] backgroundManagedObjectContext] executeFetchRequest:fetchRequest error:nil]; 

     for (Ret *ret in results) { 
      NSManagedObjectID *moID = [ret objectID]; 

      [objectsIDs addObject:moID]; 

      NSLog(@"%@", objectsIDs); 
     } 

     [[[DataManager sharedInstance] mainManagedObjectContext ] performBlock:^{ 

      [self loadDishesWithObjectIDs:objectsIDs]; 

     }]; 



    }]; 

首先我取在後臺線程中的所有對象,然後轉化中的NSMangedObjectIDs主線程。

在我的「loadDishes」的方法:

- (void)loadDishesWithObjectIDs:(NSArray *)objectsIDs { 

    /* 
    [NSFetchedResultsController deleteCacheWithName:@"dishes"]; 
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:**???**? managedObjectContext:_theManagedObjectContext sectionNameKeyPath:nil cacheName:@"dishes"]; 
    _fetchedResultsController.delegate = self; 




    NSError *error = nil; 


    if (![_fetchedResultsController performFetch:&error]) { 
     NSLog(@"Fetch Failed"); 

    } 


    NSArray *theDishes = _fetchedResultsController.fetchedObjects;*/ 



} 

我將如何管理由的ObjectID是從後臺線程獲取的,以顯示與NSFetchResultscontroller的對象?

預先感謝您:)

回答

1

您將需要創建一個讀取請求,看起來像這樣

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
fetchRequest.entity = [NSEntityDescription entityForName:@"Ret" 
            inManagedObjectContext:[[DataManager sharedInstance] mainManagedObjectContext]]; 
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"self IN %@", objectIDs]; 
相關問題