1

我似乎无法弄清楚如何归档我的 CLLocation 数据。

我想存档当应用程序被杀死(由用户或操作系统)时从位置管理器检索到的 CLLocation 对象,并在启动时检索它。然而,在 applicationWillEnterForeground 方法中取消归档后,NSLog 打印计数为 0 有人能告诉我我做错了什么吗?这就是我所拥有的:

-(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];
}

-(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];
}

这些是文档,我已经找到了它们,但它们对我没有任何意义!

https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Archiving/Articles/codingctypes.html#//apple_ref/doc/uid/20001294-96887

4

1 回答 1

1

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.

于 2012-06-01T14:33:05.163 回答