I have a problem with my tableview. I am saving all of my data that I get back from a webservice inside a core database. I implemented a pull down to refresh function for refreshing my tableview. When the app is started the first time all works great. All the data shows up in the tableview and I can refresh as many times as I want.
The problem that I have is that when the app restarts I want that it refreshes the tableview automatically. So I have put this in my viewDidLoad.
- (void)viewDidLoad
{
[self checkForWIFIConnection];
if (!self.genkDatabase) {
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Default appGenk Database"];
// url is now "<Documents Directory>/Default Photo Database"
self.genkDatabase = [[UIManagedDocument alloc] initWithFileURL:url]; // setter will create this for us on disk
}
pull = [[PullToRefreshView alloc] initWithScrollView:(UIScrollView *) self.tableView];
[pull setDelegate:self];
[self.tableView addSubview:pull];
[super viewDidLoad];
}
This method is gonna look if there is a database created yet. After this method it goes to the following function UseDocument
- (void)useDocument
{
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.genkDatabase.fileURL path]]) {
// does not exist on disk, so create it
[self.genkDatabase saveToURL:self.genkDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
NSLog(@"no database created yet");
[self fetchNewsIntoDocument:self.genkDatabase];
[self setupFetchedResultsController];
}];
} else if (self.genkDatabase.documentState == UIDocumentStateClosed) {
// exists on disk, but we need to open it
NSLog(@"closed");
[self.genkDatabase openWithCompletionHandler:^(BOOL success) {
if([self checkForData] == YES){
NSLog(@"there is data in viewdidload");
[self emptyNews];
[self fetchNewsIntoDocument:self.genkDatabase];
}
[self setupFetchedResultsController];
}];
} else if (self.genkDatabase.documentState == UIDocumentStateNormal) {
// already open and ready to use
[self setupFetchedResultsController];
}
}
What this method is gonna do, is look at which state the document is. After restart of the app the document is closed. So then I am going to check if there is data. If there is, I am gonna delete all en fetch it all again into the database. This also works.
Problem
What my problem is now is that it only shows the cells that are on that specific moment on screen. So when I scroll down I get empty cells. Like you can see on the screenshot below.

I also get this error.
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-2372/UITableView.m:1054
2012-12-26 09:34:54.831 Krc Genk[24584:c07] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. Invalid update: invalid number of sections. The number of sections contained in the table view after the update (0) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null)
Hope anyone can help me with this.
Kind regards
The error is well defined, and you should get an idea of where you are getting wrong.
ITs the number of sections.