0

在多上下文核心数据方面苦苦挣扎,我会很高兴得到任何帮助。

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

[[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 *)controller 不会触发。

- (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);
        }];
    }];
4

1 回答 1

0

发生这种情况是因为在执行 loadRecordsFromCoreData 的块之前调用了 reload table 方法。只需进行一些更改,[self.tableView reloadData];然后NSLog(@"objects: %lu", (unsigned long)objects.count);self.managedObjectContext performBlock块中调用。

于 2013-01-03T10:47:25.970 回答