我应该多长时间保存到Core

我应该多长时间保存到Core

本文介绍了我应该多长时间保存到Core Data?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个由Core Data支持的应用程序。
现在,当我向上下文添加或删除实体时,我保存对象上下文。
恐怕会影响性能,所以我想延迟保存。
事实上,我可以一直延迟,直到应用程序将终止。
只有当应用程序即将关闭时才保存数据是否太冒险?我应该多久调用一次对象上下文保存?

I'm working on an application backed by Core Data.Right now, I'm saving the Object Context as and when I add or delete an entity to and from the Context.I'm afraid it will affect the performance, so I was thinking of delaying the save.In fact, I could delay it all the way until the application is gonna terminate.Is it too risky to save the data only when the application is about to close? How often should I call the save on Object Context?

我想要有一个单独的线程处理保存:它将等待一个信号量。每当应用程序的任何部分调用helper / util方法来保存核心数据时,它将递减信号量。当它为零时,保存线程将执行一次保存,并将信号量增加到,例如5,然后再次休眠。

I was thinking of having a separate thread handle the save: it will wait on a semaphore. Every time any part of the application calls a helper/util method to save the Core Data, it will decrement the semaphore. When it is down to zero, the "save thread" will do a save once and it increments the semaphore to a, say, 5, and then sleep again.

任何好建议?
谢谢!

Any good recommendation?Thanks!

推荐答案

您应该经常保存。保存操作的实际性能与您使用的持久存储类型有很大关系。由于二进制和XML存储是原子的,它们需要在每次保存时完全重写到磁盘。随着对象图的增长,这真的会减慢你的应用程序。另一方面,SQLite存储器更容易逐渐写入。所以,虽然会有一些东西在上面和超出你保存的对象,但开销比使用原子存储类型低得多。只要几个对象的保存总是很快,不管整体对象图的大小。

You should save frequently. The actual performance of the save operation has a lot to do with which persistent store type you're using. Since binary and XML stores are atomic, they need to be completely rewritten to disk on every save. As your object graph grows, this can really slow down your application. The SQLite store, on the other hand, is much easier to write to incrementally. So, while there will be some stuff that gets written above and beyond the objects you're saving, the overhead is much lower than with the atomic store types. Saves affecting only a few objects will always be fast, regardless of overall object graph size.

也就是说,如果你在一个循环中导入数据,等待直到完成操作的结束,以保存而不是在每次迭代时保存。您的主要目标应该是防止数据丢失。 (我发现用户不太在乎这么多!)性能应该是接近秒。您可能需要做一些工作来平衡性能的保存频率,但是上面提到的解决方案似乎过度,除非您确定了一个具体的和重大的性能问题。

That said, if you're importing data in a loop, say, I would wait until the end of the complete operation to save rather than saving on each iteration. Your primary goal should be to prevent data loss. (I have found that users don't care for that very much!) Performance should be a close second. You may have to do some work to balance the frequency of saving against performance, but the solution you outline above seems like overkill unless you've identified a specific and significant performance issue.

这篇关于我应该多长时间保存到Core Data?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 07:06