RESTKIT + COREDATA = FetchResultsController问题

时间:2014-05-27 09:14:02

标签: ios objective-c core-data restkit nsfetchedresultscontroller

您好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的文档中找到了这个方法:

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

提前感谢您的帮助

0 个答案:

没有答案