I can't seem to figure out how to archive my CLLocation data.
I want to archive the CLLocation objects retrieved from the location manager when the app gets killed (either by user or OS) and retrieve it on startup. However after unarchiving in the applicationWillEnterForeground method the NSLog print a count of 0 Could someone tell me what Im doing wrong? this is what I have :
-(void)applicationWillTerminate:(UIApplication *)application {
if([self.locs count] > 0) {
NSArray* arr = [[NSArray alloc] initWithObjects:self.locs, nil];
NSString *archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Locs.archive"];
[NSKeyedArchiver archiveRootObject:arr toFile:archivePath];
//[arr release];
}
[self.locationManager stopUpdatingLocation];
[self.locationManager release];
}
and
-(void)applicationWillEnterForeground:(UIApplication *)application {
NSString *archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Locs.archive"];
self.locs = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];
NSLog(@"friet : %i", [self.locs count]);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStatusChanged:) name:kReachabilityChangedNotification object:nil];
[self sendLocations];
}
these are the docs, I've already found them, but they don't make any sense to me whatsoever!
You have to realize that as of iOS 4, if the device supports multitasking, the applicationWillTerminate:
method never gets called. Use applicationDidEnterBackground:
instead. Better yet, save the data immediately every time it changes and don't wait for the app to be backgrounded. Otherwise, you will lose data if the app crashes or if you kill it in the debugger.
Also, I don't know if it's a good idea to save a file to NSTemporaryDirectory()
, even for testing. Have you checked whether a file saved to that location will actually be there when the app is relaunched. It might be a better idea to choose the library directory instead.