如何在从其他上下文更改后刷新fetchedresultscontroller

时间:2013-01-03 10:09:48

标签: ios core-data nsfetchedresultscontroller

努力学习多个上下文核心数据,我会很乐意为您提供帮助。

同步操作完成后,将调用此块:

[[SyncEngine sharedEngine]setFinishedSyncCompletionBlock:^{
        //---setting FRC to nil did refresh tableview but it is kinda slow
       //  self.fetchedResultsController=nil;
        [self loadRecordsFromCoreData];
        [self.tableView reloadData];

    }];

在这里,我可以看到新的托管对象在上下文中。但是调用[self.tableView reloadData];在此之后不刷新我的tableview,或者(void)controllerDidChangeContent :( NSFetchedResultsController *)控制器不会触发。

- (void)loadRecordsFromCoreData {
    NSLog(@"here loadRecordsFromCoreData");
    [self.managedObjectContext.parentContext performBlock:^{
       //---this is also a trial for resetting FRC
       // [self.managedObjectContext reset];
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Event"];

        // Edit the sort key as appropriate.
        fetchRequest.sortDescriptors = @[ [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO] ];

        NSArray *objects = [self.managedObjectContext.parentContext executeFetchRequest:fetchRequest error:NULL];
        NSArray *objectIDs = [objects valueForKey:@"objectID"];

        [self.managedObjectContext performBlock:^{

            NSArray *objects = [self.managedObjectContext objectsWithIDs:objectIDs];
          //  self.fetchedResultsController.managedObjectContext = self.managedObjectContext;
            NSLog(@"objects: %lu", (unsigned long)objects.count);
        }];
    }];

1 个答案:

答案 0 :(得分:0)

这是因为在执行loadRecordsFromCoreData的块之前调用了重载表方法。只需进行一些更改,请在[self.tableView reloadData];阻止NSLog(@"objects: %lu", (unsigned long)objects.count);之后致电self.managedObjectContext performBlock