1

我正在尝试保存一个包含 30 张图像的 NSDictionary。我正在调用将字典保存在 ViewController 的 viewDidDisappear 中的方法。问题是用户界面在保存时冻结。这是一个小的延迟,不到一秒,但有点烦人。你有什么想法让它更流畅吗?也许我应该异步保存字典,也许在一个块中,但我不知道如何使用它们。

这是我的储蓄和获取方法:

+ (NSDictionary*)getProgramImages{
    NSString *path = [DataManager getProgramImagesFileDirectory];
    NSDictionary *programImages = [NSKeyedUnarchiver unarchiveObjectWithFile:path];

    return programImages;
}

+ (void)saveProgramImages:(NSDictionary*)programImages{
    NSString *path = [DataManager getProgramImagesFileDirectory];

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:programImages];
    [data writeToFile:path options:NSDataWritingAtomic error:nil];
}

非常感谢你的帮助!

鲍里斯

4

3 回答 3

2

您可以尝试使用以下代码包装您的函数调用,该代码使用 Grand Central Dispatch 在后台线程上运行该代码。目前无法测试以查看是否可以解决您的问题。

dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // call that function inside here
});
于 2014-06-13T02:15:31.973 回答
1

有很多方法可以解决您的问题。你应该阅读这个 并发编程

Grand Central Dispatch 是个不错的选择。

于 2014-06-13T02:20:00.513 回答
1

也许dispatch_async可以帮助您平滑在主线程上运行的代码。

 dispatch_async(dispatch_get_main_queue(),^{
        //your code goes here
    });
于 2014-06-13T02:15:17.363 回答