NSUserDefaults 有哪些限制? [英] What are the limitations of NSUserDefaults?

查看:26
本文介绍了NSUserDefaults 有哪些限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iPhone 中永久存储数据通常使用 Core Data 或 sqlite3 来完成.大多数人更喜欢将 NSUserDefaults 用作应用程序首选项的存储,而不是将其用作普通数据库(例如 sqlite).

Storing data permanently in an iPhone is usually done using Core Data or sqlite3. Most people prefer to user NSUserDefaults as a storage for application preferences, rather than using it as a normal database (such as sqlite).

我发现NSUserDefaults可以存储大量数据,使用起来非常方便,而且速度很快.那么为什么不把它用作永久存储呢?NSUserDefaults 作为数据库有什么限制?

I have found that large amount of data can be stored in NSUserDefaults, it is extremely easy to use and it is fast. So why not use this as a permanent storage? What are the limitations of NSUserDefaults as a database?

更新:
我经常使用三种不同的方式将数据保存到磁盘.

Update:
I frequently use three different ways of saving my data to disk.

  • 核心数据
  • 将对象序列化为 plists
  • NSUserDefaults

我不再使用 FMDB(或直接使用 sqlite).每种方法的主要优点和缺点是什么?

I do not use FMDB (or sqlite directly) anymore. What are the main advantages and disadvantages of each approach?

我遇到的 NSUserDefaults 的一些优点:

Some advantages of NSUserDefaults that I've come across:

  • 使用 NSPredicate 可以轻松完成排序、分组等操作.
  • NSUserDefaults 是线程安全的.
  • 获取数据并将其保存到 NSUserDefaults 需要一行代码.

推荐答案

NSUserDefaults 提供简单的学习曲线和线程安全的实现.

NSUserDefaults offers a trivial learning curve and thread safe implementation.

否则我发现 Core Data 在各方面都更胜一筹.尤其是在配置默认值和迁移例程方面.

Otherwise I've found Core Data superior in every way. Especially with regards to configuring default values and migration routines.

事实证明,NSUserDefaults线程安全性"似乎来自在主线程上运行的操作.这在我的一个应用程序中导致了严重的跳帧;我最终删除了 NSUserDefaults 并用一个线程安全的 NSMutableDictionary 替换它,该 NSMutableDictionary 被序列化为一个文件.

As it turns out, NSUserDefaults "thread-safeness" seems to come from running operations on the main-thread. This caused severe frame-skipping in one of my applications; I ended up ripping out NSUserDefaults and replacing it with a thread-safe NSMutableDictionary which gets serialized to a file.

这篇关于NSUserDefaults 有哪些限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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