RESTKit:NSFetchResultsController在首次启动时不会更新

时间:2014-06-06 14:28:49

标签: restkit-0.20 nsfetchedresultscontroller

我有一个间歇性的问题。我从模拟器中删除了应用程序并运行Xcode。该应用程序首次启动。 RESTKit从服务器下载7个对象(下面的日志)。但是,NSFetchedResultsController显示节count == 0并且tableview为空

2014-06-06 07:14:22.541 App[4859:60b] I restkit.network:RKObjectRequestOperation.m:180 GET 'http://www.domain.com/meetings?lastrequest=2004-06-06T07%3A00%3A00Z'
2014-06-06 07:14:22.772 App[4859:7b03] I restkit.network:RKObjectRequestOperation.m:250 GET 'http://www.domain.com/meetings?lastrequest=2004-06-06T07%3A00%3A00Z' (200 OK / 7 objects) [request=0.1917s mapping=0.1528s total=0.2559s]

当我停止应用程序并重新启动它时,我在tableView中获得了3个部分和7个单元格。它就像NSFetchedResultsController第一次没有更新一样。随机应用程序有时会第一次加载数据。

以下是我的FRC设置:

- (NSFetchedResultsController *)fetchedResultsController
{
    if(_fetchedResultsController!=nil)
    {
        return  _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Meetings"
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *firstSort = [[NSSortDescriptor alloc] initWithKey:@"startDate"
                                                              ascending:NO];

    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:firstSort,nil];    
    [fetchRequest setSortDescriptors:sortDescriptors];


    self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest
                                                                       managedObjectContext:self.managedObjectContext
                                                                         sectionNameKeyPath:@"sectionIdentifier"
                                                                                  cacheName:nil];
    self.fetchedResultsController.delegate = self;
    return self.fetchedResultsController;
}

FRC代表:

#pragma mark - NSFetchedControllerDelegate

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
        [self.tableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
        [self.tableView reloadData];
        [self.tableView endUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
    switch (type)
    {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeUpdate:
        {
            Invite *changedInvite = [self.currentFRC objectAtIndexPath:indexPath];
            STInviteSummaryCell *cell = (STInviteSummaryCell *)[self.tableView cellForRowAtIndexPath:indexPath];
            [cell updateValuesForChangedInvite:changedInvite];
        }
            break;
        case NSFetchedResultsChangeMove:
        {
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        }
            break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    switch (type) {
        case NSFetchedResultsChangeInsert:
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

RESTKit Fetch:

RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];
operation.managedObjectContext = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;
operation.managedObjectCache = appDelegate.managedObjectStore.managedObjectCache;
operation.savesToPersistentStore = NO;

[operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

    //Before saving the objects, here I iterate through the each object in the array and flag each attribute that's changed in the object.  

    NSError *error;
    if ([self.managedObjectContext saveToPersistentStore:&error])
    {
        NSLog (@"******* OBJECTS SAVED **********");
    }

} failure:^(RKObjectRequestOperation *operation, NSError *error) {

    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        [self updateUI];
    });

}];

[operation start];
[operation waitUntilFinished];

}

问题再次是间歇性的。

修改

在同一课程中,我有以下代码:

- (void)loadView
{
    [self getManagedObjectFromAppDelegate];
    [self registerForReachibilityNotificaiton];
    [self fetchInvitesInBackgroundThread];

    [super loadView];
}


- (void)getManagedObjectFromAppDelegate
{
    STAppDelegate *appDelegate = (STAppDelegate *)[[UIApplication sharedApplication] delegate];

    [appDelegate setupCoreDataWithRESTKit];
    self.objectManager = [self getObjectManager];

    self.objectManager.managedObjectStore = appDelegate.managedObjectStore;
    self.objectManager.managedObjectStore.managedObjectCache = appDelegate.managedObjectStore.managedObjectCache;
    self.managedObjectContext = [RKManagedObjectStore defaultStore].mainQueueManagedObjectContext;
}

1 个答案:

答案 0 :(得分:1)

看起来托管对象上下文没有正确链接,因此您可以将mainQueueManagedObjectContext用于所有内容。可以创建另一个子上下文来运行该操作,但在分析显示您遇到性能问题之前,您不需要添加该复杂性。