简体   繁体   中英

Reload UITableView data every time context changes?

I have a UITableViewController that's a subclass of CoreDataTableViewController (it's the Stanford class ). That implements a fetchedResultsController .

Now, in my viewWillAppear , I have this:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if(!self.managedObjectContext) {
        [self useManagedDocument];
    }
}

It initializes the managedObjectContext if I don't have one, and gets it from a helper class. In the MOC setter, I initialize the fetchedResultsController :

- (void)setManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
{
    _managedObjectContext = managedObjectContext;

    if(self.managedObjectContext) {
        NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:CD_ITEM_ENTITY_NAME];
        request.sortDescriptors = @[[NSSortDescriptor
                                     sortDescriptorWithKey:CD_ITEM_NAME_PROPERTY
                                     ascending:YES
                                     selector:@selector(localizedCaseInsensitiveCompare:)]];
        request.predicate = nil;
        self.fetchedResultsController = [[NSFetchedResultsController alloc]
                                         initWithFetchRequest:request
                                         managedObjectContext:self.managedObjectContext
                                         sectionNameKeyPath:nil
                                         cacheName:nil];
    } else {
        self.fetchedResultsController = nil;
    }

}

When my program starts, it loads the table data up correctly and my debugger says there was a fetch request made. However, after inserting data into my Core Data graph, and saving, it says the context changes and fires this delegate method:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    /*NSError *error;
    [controller performFetch:&error];
    if(error) {
        NSLog(@"%@", error);
    }
    [self.tableView reloadData];*/
}

I commented this because it wasn't working. Basically, what I want to do is reload the data every time the context changes. If I add an item in another view controller and then go back to this one, it should reload in that case too.

How do I implement this? I tried doing performFetch in that delegate method and it entered it (I checked by setting a breakpoint inside), but the performFetch did nothing and my table wasn't reloaded.

When I add an item in a modal VC (another one I have for managing items) this is what happens in my logger:

2013-05-10 22:41:38.264 App1[7742:c07] [ItemCDTVC performFetch] fetching all Item (i.e., no predicate)
2013-05-10 22:41:46.454 App1[7742:c07] NSManagedObjects did change.
2013-05-10 22:41:46.456 App1[7742:c07] NSManagedContext did save.

When I close my app but do not quit it from the multitasking bar, and then reopen it, it does nothing. No fetch. Well, if the context didn't change I don't want it to fire a request, but imagine if the user adds an item in another ViewController and then goes back to my ItemCDTVC, which lists all items. Does it get a context changed notification so it can call the delegate method to update the table, or will I always have to refresh regardless of changes in my viewWillAppear ? I currently have it set to do it only once, on app load.

Fixed, all I had to do is put a one liner in that delegate method:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}

This ends updates, inserts, deletes, and changes made to the table (refreshing my view, essentially) the same as per Apple's documentation .

It now updates on content change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM