NSMutableArray 在应用程序终止或使用 NSKeyedArchiver 重新启动后消失

发布于 2024-11-15 06:46:33 字数 1466 浏览 7 评论 0原文

我有一个应用程序将 Task 对象(自定义类)存储在 NSMutableArray 中。 Task 类符合 NSCodingNSCopying 协议,我还实现了 encodeWithCoderinitWithCoder 方法。

当用户将新任务添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺渁∝ 2024-11-22 06:46:33

您需要保存数据
([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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文