RESTKIT + COREDATA = FetchResultsController issue

独自空忆成欢 提交于 2019-12-11 19:07:13

问题


Hi I have a problem with NSFetchedResultsController which is populated by data from the web application. To map tables from the server side I'm using RestKit framework . When I'm changing the current user the data inside my UITableViewController still present cells from the previous user but without information received from the server - just default label text values. However the number of cells is the same as the previous user. It happens to me only in one UITableViewController in others everything works fine. Can you take a look at my NSFetchedResultsController delegates methods and sign out method and explain me what am I doing wrong or what should I change to make it work?

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

When the user sign out from the app I'm calling the following RestKit function:

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

I also tried the following method that I find in the documentation for method 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];
}

Thanks in advance for your help

来源:https://stackoverflow.com/questions/23885420/restkit-coredata-fetchresultscontroller-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!