Reputation: 1170
I have an iOS app in Master-Detail form where posts made by users are shown in the master view and comments shown in the detail. When I select a post I can see all of the comments just fine but when I go back and select another post, the data does not change to the comments for the next post.
I call this in viewDidLoad
self.fetchedResultsController = nil;
[self.tableView reloadData];
But nothing happens.
Here is my code for fetchedResultsController
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Comment" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"question == %@", self.detailItem];
[fetchRequest setPredicate:predicate];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creator" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error in detail %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
Upvotes: 2
Views: 2175
Reputation: 1430
The reason your fetched-results-controller doesn't refresh is because you have set a cache. You will either need to pass nil
as your cache-name, or delete the cache prior to refreshing the FRC:
[NSFetchedResultsController deleteCacheWithName:@"Master"]
Upvotes: 6
Reputation: 33471
Have you tried calling your reset code in viewDidAppear
instead? viewDidLoad
is typically not called multiple times in the view's lifecycle. viewWillAppear
/viewDidAppear
are called when the view is actually coming on screen.
Have you tried to breakpoint your table reload method to make sure it's actually being called?
Upvotes: 2
Reputation: 739
Just try this code:
before calling this method don't forget to write self.fetchedResultsController = nil;
- (NSFetchedResultsController *)fetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
if (_fetchedResultsController != nil )
{
return _fetchedResultsController;
}
NSLog(@"Length:: %d",[[_fetchedResultsController mutableCopy] length]);
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Comment"];
[fetchRequest setIncludesSubentities:YES];
// Sort by Application name
NSSortDescriptor* sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"creator" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
//Sort by CommandNameloca
NSArray* sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor1, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:100];
ordersArr=[[NSMutableArray alloc]initWithArray:sortDescriptors];
NSLog(@"----- ordersArr Count == %d",[ordersArr count]);
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"creator" cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _fetchedResultsController;
}
Upvotes: 1