Here's my current situation:
When I open up my app,
UITableView
on my first screen is blankI fetch my data from a remote server and parse its resultant JSON object, and save it to Core Data storage, and issue a fetched request to the storage and show the result to the aforementioned
UITableView
However, even after I post a notification after the parse-save-query and reload my table, the table isn't updated until I tap or scroll over the screen.
I tried [_myTableView reloadData];
and [_myTableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
from notification, but the result doesn't change at all and I still have to tap or scroll over the screen in order to see the reloaded data on the table.
I use Core Data and NSFetchedResultsController, and I confirmed my query was successfully fetched. Just a reload table issue. And also note that I call the notification from AppDelegate.m
but my actual table view is on RootViewController.m
, another class, for the reasons stated above (remote server data fetching).
Also, when I wrote out the following code:
-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[_myTableView reloadData];
[_myTableView endUpdates];
}
I still have to see the result by tapping the screen. However, when I added the following code:
-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
NSLog(@"updated begin");
[_myTableView beginUpdates];
}
I cannot see the reloaded data even after I tap the screen at all.
Also, using printf debug, I confirmed that even if the two methods above are called many, many times (probably it's called in the same number of times of the actual fetched managed objects, right?), the numberOfRowsInSection:
and cellForRowAtIndexPath:
aren't called at all after I fetched the newly added datasets.
Note that cellForRowsAtIndexPath:
is called and the data is displayed if I wait for 10 or 20 seconds or so, which means it's displayed even if I don't tap or scroll over the screen. Weird.
So what's happening here? And how can I fix this strange issue?
I use iOS 7 and Xcode 5.
update
I found out that the reason I wasn't able to see the reloaded data when I write controllerWillChangeContext:
is I got the following error: CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)
. It's still quite weird that I don't get this error when I implement controllerDidChangeContext:
only, but if I write both, I got the error and the table doesn't show the data even after I tap the screen.
So maybe I cannot take the route of displaying blank result first, and updating the table with the new data, and instead I should just make users wait to show the correct data... ?