1

你好同胞溢出

我很难弄清楚如何在后台线程中获取 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 的对象?

先感谢您 :)

4

1 回答 1

1

您需要创建一个看起来像这样的获取请求

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:@"Ret" 
                                  inManagedObjectContext:[[DataManager sharedInstance] mainManagedObjectContext]];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"self IN %@", objectIDs];
于 2013-04-05T15:26:15.250 回答