NSMutableArray 在应用程序终止或使用 NSKeyedArchiver 重新启动后消失
我有一个应用程序将 Task
对象(自定义类)存储在 NSMutableArray
中。 Task
类符合 NSCoding
和 NSCopying
协议,我还实现了 encodeWithCoder
和 initWithCoder
方法。
当用户将新任务添加到 NSMutableArray
list
时,我使用 NSKeyedArchiver
保存该数组。 list
填充 UITableView
。
数据正在保存,当我退出应用程序并重新进入时,数据仍然存在。当我使用另一个应用程序一段时间并回来时,数据仍然存在。但是,当我在多任务管理中“杀死”应用程序或重新启动设备时,数据就会消失。以下是一些重要的代码片段:
#define kFilename @"epapsTasksFile"
.
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
- (void)viewDidLoad {
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
self.list = [[NSKeyedUnarchiver unarchiveObjectWithFile:kFilename] retain];
}
else {
self.list = [NSMutableArray arrayWithCapacity:1];
}
...
}
- (void)applicationWillResignActive:(NSNotification *)notification {
NSMutableArray *updatedList = [[NSMutableArray alloc] initWithArray:self.list];
[NSKeyedArchiver archiveRootObject:updatedList toFile:kFilename];
}
为什么我的应用程序在“终止”或设备重新启动时不保存数据?另外,值得注意的是,当我重新启动 iPhone 模拟器时,数据保持不变。
I have an app that is storing Task
objects (custom class) inside of a NSMutableArray
. The Task
class conforms to the NSCoding
and NSCopying
protocols, and I have also implemented the encodeWithCoder
and initWithCoder
methods.
When the user adds a new task to an NSMutableArray
list
, I save the array using NSKeyedArchiver
. The list
populates a UITableView
.
The data is being saved, and when I exit the app and reenter, the data is still there. When I use another app for a while and come back, the data is still there. However, when I "kill" the app in the multitasking task manage or restart the device, the data disappears. Here are some important code snippets:
#define kFilename @"epapsTasksFile"
.
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
- (void)viewDidLoad {
NSString *filePath = [self dataFilePath];
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
self.list = [[NSKeyedUnarchiver unarchiveObjectWithFile:kFilename] retain];
}
else {
self.list = [NSMutableArray arrayWithCapacity:1];
}
...
}
- (void)applicationWillResignActive:(NSNotification *)notification {
NSMutableArray *updatedList = [[NSMutableArray alloc] initWithArray:self.list];
[NSKeyedArchiver archiveRootObject:updatedList toFile:kFilename];
}
Why is my app not saving the data when the app is "killed" or the device is restarted? Also, it may be interesting to note that when I restart the iPhone simulator, the data stays in place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要保存数据
(
[NSKeyedArchiver archiveRootObject:updatedList toFile:kFilename];
在
applicationWillTerminate
委托方法中以及在终止时保存它。编辑:
applicationWillTerminate
在 IOS4.0 及以上版本中不受保证。最好是检查
archiveRootObject:toFile:
的返回状态,看看数据是否正确存储。正如您所发现的,可能是文件路径错误的情况。You need to save the data
(
[NSKeyedArchiver archiveRootObject:updatedList toFile:kFilename];
in
applicationWillTerminate
delegate method as as well to save it on termination.EDIT:
applicationWillTerminate
is not gauranteed in IOS4.0 and above.Best is to check the return status of
archiveRootObject:toFile:
and see if the data is stored properly. As you figured it out, it can be case with wrong file path.