RESTKIT + COREDATA = FetchResultsController问题

您好,我有NSFetchedResultsController由Web应用程序的数据填充问题。 为了从服务器端映射表,我使用了RestKit框架。 当我更改当前用户时,我的UITableViewController的数据仍然呈现来自之前用户的单元格,但没有从服务器接收到信息 – 只是默认的标签文本值。 但是,单元格的数量与以前的用户相同。 它发生在我身上只有在其他一切UITableViewController工作正常。 你可以看看我的NSFetchedResultsController委托方法,并注销方法,并解释我是什么做错了,或者我应该改变,使其工作?

 - (void)viewDidLoad { [super viewDidLoad]; _managedObjectContext = [[DateModel sharedDataModel]objectStore].mainQueueManagedObjectContext; [self loadAlerts]; } #pragma mark - TableView Data Source - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [[self.fetchedResultsController sections] count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections[section]; return [sectionInfo numberOfObjects]; } -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; return [sectionInfo name]; } #pragma mark FetchedResultsControllerDelegate methods - (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController != nil) { return _fetchedResultsController; } NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"Alerts" inManagedObjectContext:_managedObjectContext]; [NSFetchedResultsController deleteCacheWithName:nil]; [fetchRequest setEntity:entity]; [fetchRequest setFetchBatchSize:12]; NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created_at" ascending:NO]; NSArray *sortDescriptors = @[sortDescriptor]; [fetchRequest setSortDescriptors:sortDescriptors]; NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_managedObjectContext sectionNameKeyPath:@"created_at.groupAlertsByDate" cacheName:nil]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController; NSError *error = nil; if (![self.fetchedResultsController performFetch:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); } return _fetchedResultsController; } - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller { [self.tableView beginUpdates]; } - (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; } } - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath { UITableView *tableView = self.tableView; switch(type) { case NSFetchedResultsChangeInsert: [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeDelete: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; break; case NSFetchedResultsChangeUpdate: [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath]; break; case NSFetchedResultsChangeMove: [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade]; break; } } - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { [self.tableView endUpdates]; } 

当用户从应用程序注销时,我正在调用以下RestKit函数:

  NSError *error; [[[RKObjectManager sharedManager]managedObjectStore]resetPersistentStores:&error]; 

我也尝试了在方法resetPersistentStores的文档中find的以下方法:

 -(void)truncateAllManagedObjectsFromContext { NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{ NSManagedObjectContext *managedObjectContext = [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext; [managedObjectContext performBlockAndWait:^{ NSError *error = nil; for (NSEntityDescription *entity in [RKManagedObjectStore defaultStore].managedObjectModel) { NSFetchRequest *fetchRequest = [NSFetchRequest new]; [fetchRequest setEntity:entity]; [fetchRequest setIncludesSubentities:NO]; NSArray *objects = [managedObjectContext executeFetchRequest:fetchRequest error:&error]; if (! objects) for (NSManagedObject *managedObject in objects) { [managedObjectContext deleteObject:managedObject]; } } BOOL success = [managedObjectContext save:&error]; if (!success) RKLogWarning("Failed saving managed object context: %@", error); }]; }]; [operation setCompletionBlock:^{ // Do stuff once the truncation is complete }]; [operation start]; } 

在此先感谢您的帮助