CoreData - 保存时的疑问

发布于 2024-11-30 04:55:54 字数 682 浏览 0 评论 0原文

我从 coreData 开始,我有一个疑问。

我有一个从服务器读取数据的应用程序,我解析数据,并获取对象的 NSDictionary。

要将数据保存到 coreData,我执行以下操作:

    for (NSDictionary *activityData in arrayWithResult){
        [CompanyActivity createActivityWithInfoFromServer:activityData inManagedObjectContext:self.managedObjectContext];
    }

    if (![self.managedObjectContext save:&error])
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

这将读取大约 300 条记录。方法“createActivityWithInfoFromServer:”检查是否有任何具有该名称的记录。如果是,则更新数据,如果不是,则创建数据。

问题是,当“for”循环运行时,如果我与用户界面交互,它有时会停止保存核心数据,但并非总是如此。为什么?

如果我在循环内进行 SAVE,问题就会消失。

我应该怎么办?

谢谢大家,

RL

I'm starting in coreData, and I have a doubt.

I have an App that reads data from a server, I parse the data, and get as NSDictionary of Objects.

To save the data to coreData, i do the following:

    for (NSDictionary *activityData in arrayWithResult){
        [CompanyActivity createActivityWithInfoFromServer:activityData inManagedObjectContext:self.managedObjectContext];
    }

    if (![self.managedObjectContext save:&error])
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);

This reads about 300 records. The method 'createActivityWithInfoFromServer:' checks if there is any record with that name. If so, updates the data, if not, creates one.

The problem is that, while the "for" cycle is running, if i interact with the user interface, it stops saving in core data, sometimes, not always. Why?

If I take the SAVE inside the cycle, the problem disappears.

What should I do?

Thanks all,

RL

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

み格子的夏天 2024-12-07 04:55:54

根据您仍然允许用户与 UI 交互的理由,您可以暂时禁用 UI,或者创建一个新线程来处理您发布的代码段。

至于它有时无法保存的原因,我认为有时它不必创建那么多新对象并更快完成,并且可以在与 UI 交互之前保存。如果你在 for 循环中保存,那么你会在查看每个项目后保存所有内容,这是相当低效的。

Depending on what your reasoning is to still allow the user to interact with the UI, you could either disable the UI for that time, or create a new thread that handles the piece of code that you posted.

As for the reasoning why it only fails to save sometimes, I assume that sometimes it doesn't have to create as many new objects and finishes sooner, and can save before you interact with the UI. If you save inside the for loop than you are saving everything after looking at each item which is fairly inefficient.

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