views:

482

answers:

1

I need to update the tableview as soon as the content is pushed in core data database.

for this AppDelegate.m contains following code

NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];

[request setEntity:[NSEntityDescription entityForName:@"FeedItem" inManagedObjectContext:moc]];
//for loop
// push data in code data & then save context  
[moc save:&error];

ZAssert(error == nil, @"Error saving context: %@", [error localizedDescription]);
//for loop ends 

This code triggers following code from RootviewController.m

- (void)controllerWillChangeContent:(NSFetchedResultsController*)controller 
{
[[self tableView] beginUpdates];
}

But this updates the tableview only at the end of the for loop ,the table does not get updated after immediate push in db.

I tried following code but that didn't work

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// In the simplest, most efficient, case, reload the table view.
[self.tableView reloadData];
}

I have been stuck with this problem for several days.Please help.Thanks in advance for solution.

A: 

Hi,

It sounds like you're running the first bit of code in the main thread. As this thread also controls the UI updating, nothing will happen until your loop is finished. There are three ways to solve this.

1) Update coredata from a second thread and pass a message into the main thread to tell it to update the UI. This is quite tricky to do - have a look at the core data multithreading docs for some scary warnings!

2) Don't do your updates in a for loop, do them as part of a timer instead. this will give the ui time to update between each insert i.e.

    ...
    self.stuffToUpdate = [NSMutableArray arrayWithObjects:...];
    [NSTimer scheduledTimerWithInterval:0.1 target:self selector:@selector(doStuff:) userInfo:nil repeats:YES];
    ...

- (void) doStuff:(NSTimer *)timer {
    // If there's noting to update, bail
    if (0 == stuffToUpdate.count) {
        [timer invalidate];
        return;
    }

    // Get the next object to update
    id object = [stuffToUpdate objectAtIndex:stuffToUpdate.count-1];
    [stuffToUpdate removeLastObject];

    // Do stuff to object
    [object doStuff];

    // Save
    NSError &error = nil;
    [moc save:&error];

    // Tell the table to update
    [self.tableView reloadData];
}

However, a continually updating table like that might give wierd user interface issues - you'd have to try it in user tests to see if it works Ok. Also, this will slow the total time taken to update coredata but that might not be a problem if you're updating the UI all the time.

3) Change your app so that is displays a 'please wait' message with an activity indicator while coredata is populated! This is much easier to do but if your updates take a long time, this might put users off.

deanWombourne
sorry, but I didn't understand this part , // Get the next object to update id object = [stuffToUpdate objectAtIndex:stuffToUpdate.count-1]; [stuffToUpdate removeLastObject];the objects in my case are rss feed items and downloaded articles. this is getting pushed in core data in AppDelegate.m . But the code which you have written will be in RootviewController.m. how should i use ur second method ? Pls help