Data is not reloaded automatically in uitableview when using nsfetchedresultscontroller

In my application, data is stored in master data in the background. The main context managed object (in the appdelegate) is notified of every background save using a notification method. I am displaying data as a table. I used nsfetchedrequestcontroller to automatically update the tableview. I used the same managed context object in the appdelegate for the fetchedresultscontroller. I am getting the data as expected and the table view is refreshed every time the data is saved in the background. But sometimes the table view becomes empty with only one or two cells. I did a lot of research into this issue and found the fault data being processed. So I changed the context managed object for fetchedresultscontroller to a new context managed object as mentioned in the link below.

NSFetchedResultsController transmitter fires even when context is saved

After that, I don't get empty cells, but the tableview is not automatically refreshed. How can I automatically update the table?

-(NSFetchedResultsController*)mfetchedResultsController
{

if (_mfetchedResultsController != nil) {
    return _mfetchedResultsController;
}

NSString *loginUser=[[NSUserDefaults standardUserDefaults] valueForKey:@"currentUser"];

AppDelegate *sharedDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//NSManagedObjectContext *context = [sharedDelegate managedObjectContext];
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
context.persistentStoreCoordinator = [sharedDelegate persistentStoreCoordinator];


NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ThreadInfo"
                                          inManagedObjectContext:context];
[fetchRequest setEntity:entity];

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                          initWithKey:@"threadDate" ascending:NO];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];


NSPredicate *threadPredicate = [NSPredicate predicateWithFormat:@"userEmail == %@",loginUser];

[fetchRequest setPredicate:threadPredicate];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                    managedObjectContext:context sectionNameKeyPath:nil
                                               cacheName:nil];
_mfetchedResultsController = theFetchedResultsController;
_mfetchedResultsController.delegate = self;

return _mfetchedResultsController;

}

      

Also I get this warning in my console when the Tableview becomes empty with only one or two LOG cells :

 2015-06-19 09:58:43.259[17460:1401522] CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  *** -[_PFBatchFaultingArray objectAtIndex:]: index (0) beyond bounds (0) with userInfo (null)

      

+3


source to share


1 answer


I think you are creating context incorrectly in your result receiver. You have to use the NSManagedObjectContext

API initWithConcurrencyType

and assign a parent controller.

Also, I think you shouldn't create a new context every time you create the selected result controller. Probably your delegate conflict is caused by this constellation.



The context of the selected results should be on the main thread and you should only have one context on the main thread, so it might be better if the class providing your main data stack (I see it is an application delegate) provides you always with the same basic context.

0


source







All Articles