我似乎不知道如何归档我的 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];
}
这些是文档,我已经找到了它们,但它们对我来说没有任何意义!
最佳答案
您必须意识到,从 iOS 4 开始,如果设备支持多任务处理,则永远不会调用 applicationWillTerminate:
方法。请改用 applicationDidEnterBackground:
。更好的是,每次更改时立即保存数据,不要等待应用程序后台运行。否则,如果应用程序崩溃或在调试器中将其终止,您将丢失数据。
此外,我不知道将文件保存到 NSTemporaryDirectory()
是否是个好主意,即使是为了测试。您是否检查过保存到该位置的文件在重新启动应用程序时是否真的存在。选择库目录可能是更好的主意。
关于ios - 将 CLLocation 数组保存到存档 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10851988/