iOS - setObject与Synchronize [英] iOS - setObject vs. Synchronize

查看:155
本文介绍了iOS - setObject与Synchronize的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新手问题。如果我将对象设置为NSUserDefault,我还需要同步以确保该对象永久返回NSUserDefault数据库吗?我想我要问的是,setobject是将对象永久地放回数据库中还是放入需要同步的一些临时存储中以确保永久存储?

Newbie question. If I set an object into NSUserDefault do I also need to Synchronize to be sure the object goes back into NSUserDefault database permanently? I guess what I am asking is, is "setobject" placing the object right back into the database permanently or into some temporary storage that needs to be synchronized to ensure permanent storage?

例如:
//下面我在添加一行数据后在NSUserDefaults中放置一个列表的新副本

For example: //below I place a new copy of a list in NSUserDefaults after adding a row of data

userDefaults.setObject(newMutableList, forKey: "itemList")

//我还需要同步马上确保数据库更新?

//do I also need to synchronize right away to be sure database updates permanently?

userDefaults.synchronize() 

我的代码在没有同步的情况下工作,这意味着我不需要它。但我想确保跳过同步的使用在将来不会在另一种情况下引起问题。

My code works without the synchronize, meaning I don't need it. But I want to make sure that skipping the use of synchronize doesn't cause problems in another scenario in future.

提前致谢。

推荐答案

在iOS 7及更早版本中:

On iOS 7 and earlier:

当您调用-setObject:forKey:时,NSUserDefaults会安排一个在将来大约10秒钟同步操作本身。发生这种情况时,到目前为止的所有内容都会保存到持久存储中。这里的想法是你可以自由地使用set和get方法,而无需每次都支付大量的性能成本。

When you call -setObject:forKey:, NSUserDefaults schedules a synchronize operation itself for roughly 10 seconds in the future. When that occurs, everything up to that point is saved to persistent storage. The idea here is that you can freely use the set and get methods without paying the significant performance cost of going out to disk every time.

在iOS 8上,NSUserDefaults的工作方式不同,延迟是几毫秒,而不是10秒,所以-synchronize几乎从来没用过。

On iOS 8, NSUserDefaults works differently, and the delay is a few milliseconds, rather than 10 seconds, so -synchronize is almost never useful.

这篇关于iOS - setObject与Synchronize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆