【问题标题】:Saving array of CLLocation to Archive ios将 CLLocation 数组保存到存档 ios
【发布时间】:2012-06-06 19:05:47
【问题描述】:

我似乎不知道如何归档我的 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

【问题讨论】:

  • 它有什么作用?它不做什么? Locs.archive 中有什么?它是否曾经进入您的终止/进入前台功能? ....
  • 对不起,我猜我忘记了一些东西:P self.locs = [NSMutableArray] 填充了 CLLocation 对象。在 applicationWillEnterForeground 函数中 self.locs 的大小(取消归档后)= 0。虽然进入的大小> 0。所以要么归档要么取消归档出错

标签: ios arrays object archive cllocation


【解决方案1】:

您必须意识到,从 iOS 4 开始,如果设备支持多任务处理,则永远不会调用 applicationWillTerminate: 方法。请改用applicationDidEnterBackground:。更好的是,每次数据发生变化时立即保存数据,而不是等待应用程序后台运行。否则,如果应用崩溃或在调试器中终止应用,您将丢失数据。

另外,我不知道将文件保存到NSTemporaryDirectory() 是否是个好主意,即使是为了测试。您是否检查过保存到该位置的文件是否会在应用程序重新启动时实际存在。最好选择库目录。

【讨论】:

  • 感谢您的遮阳篷! NSTemporaryDirectory() 是苹果在文档中使用的关于归档的内容,所以我认为它会没问题。一旦我发现问题,我想我不会像你建议的那样等待终止。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-19
  • 2015-12-05
  • 2013-12-15
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多