iPhone - different ways to store data, advantages and disadvantages

 ̄綄美尐妖づ 提交于 2019-12-04 12:46:01

Plist files are another option if you want to manage your own storage on the file system. NSArray and NSDictionary provide methods for writing and reading those collections to and from plist files as long as you can store all of your data in one of the supported plist data types. See the Property List Programming Guide for details. It might be a good option if you can easily break up your data into distinct files and always want to load an entire file at once.

CoreData is a powerful tool, especially if you want to store a graph of objects. It might be an appropriate choice when you want to be able to store and load model objects easily.

SQLite is great if you want to store relational data and run queries against it. It might be a good choice if you want fast and efficient queries but don't need to convert the results into model objects (or have some reason for writing your own ORM layer).

As you mentioned NSUserDefaults is a convenient tool for storing user credentials but is not intended for larger volumes of data. It also allows you to expose settings in the settings app so that a user can set application behavior in one common location without launching your app.

Any form of file based storage may have additional value if you want to expose those files to the user through the File Sharing settings, allowing application data to appear in the iTunes Documents directory when synching to a PC.


Regardless of the storage mechanism you use every one of these options requires that you manage some sort of schema for your data.

You need track of the format your data is stored in in every version of your application. Any time you change your expectation of the format of saved data you need to support old versions. I see too many apps crash after an update because they do not handle data saved by old versions of the app or assume that users will have installed and run every version of the app instead of skipping some updates.

CoreData has some support for migrating data from one schema to another but it is something that requires developer work, awareness, and testing in all cases.

Manny

SQLite DB - DB Used by iOS development, you can interact with this directly depending on your preferences (e.g. you want to use SQL statements).

CoreData - Abstraction to SQLite DB so you can remove SQL statements and use the API instead. Advantage of this is its compatibility with the Cocoa API. In our production applications we use CoreData over SQLite.

File System - You can store files directly here and use a convention. You might also want to read about using the cache folder iOS development for temporary data.

XML - Case to case. In commercial application, we only use XML for interfacing between systems. E.g. iPad to Cloud server.

UserDefault - only for parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!