App crashes when mixing up new and old data
I have made a question which is a bit similar to this, though the old one actually got solved but I found a new issue which relates a bit. Anyways, I chose to make seperate topics.
I have an application which reads data from a property list on my webserver. I want the application to be usable when offline therefore I copy the property list onto the iPhone as Bands.plist and I read the data from the local plist.
My problem is, that if the data has been updated and an old version of Bands.plist is stored on the device, then my application starts reading from Bands.plist while updating and the data becomes a mix of old and new data which causes the application to crash. I can't figure out how to fix this issue and hope that someone has an idea.
I have an image which illustrates my issue:
What happens is, that my view has a subview which has a page controller. Each page contains a table view where the data is loaded into.
Here you see that I am sliding from one view to another. In the first view (the left) is the old data and in the second view (the right) is the new data. In this transition of views, the application crashes.
Can anyone help me figure this out? It seems that the application starts loading the data before it is saved.
Here is some code. Please say, if more code will help. I can't figure out how to fix this issue.
This is from the App Delegate. Here the data is retrieved from the webserver and is saved to Bands.plist.
NSLog(@"[AppDelegate] Data has been downloaded.");
// throw data into a property list (plist)
NSMutableArray *tmpArray = [NSPropertyListSerialization propertyListFromData:receivedData mutabilityOption:NSPropertyListMutableContainers format:nil errorDescription:nil];
NSMutableArray *plistArray = [[NSMutableArray alloc] initWithArray:tmpArray];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
开发者_如何学Python NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Bands.plist"];
NSLog(@"[AppDelegate] Bands.plist has been populated.");
This is from the table view. Here the data is loaded from Bands.plist.
pageNumber = page;
NSLog(@"[TableView] Loading view.");
NSArray *whatDays = [[NSArray alloc] initWithObjects:@"Onsdag", @"Torsdag", @"Fredag", @"Lørdag", @"Ikke programsat", nil];
sections = [[NSMutableDictionary alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Bands.plist"];
tmpArray = [[NSMutableArray alloc] initWithContentsOfFile:path];
// scan through data and sort
for(int i = 0; i < [whatDays count]; i++) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.weekDay == %@", [whatDays objectAtIndex:i]];
NSArray *bandsThisDay = [tmpArray filteredArrayUsingPredicate:predicate];
[sections setObject:bandsThisDay forKey:[whatDays objectAtIndex:i]];
}
[whatDays release];
Would it be possible to call a method in tableview implementation from the app delegate and tell it to populate the table? So it's waiting for the download to be done?
I solved this by sending a notification to NSNotificationCenter
and add an observer to the table view which will wait for the notification and then call reloadData
on the table.
精彩评论