翻译于 2016/02/21 20:32
2 人 顶 此译文
If you want to make a great app that runs fast and just works (no bugs allowed) you’ll have to think about all development aspects of your app carefully. One of the aspects you must figure out is how to store and search for large amounts of data, so you’ll probably use a database. The most common options for iOS databases are SQLite and Core Data, plus a relatively newer player called Realm.
This article covers the pros and cons of each option and discusses the process of switching to Realm if you are already using SQLite or Core Data.
SQLite is the most used database engine in the world and its open source as well. It implements a transactional SQL database engine with no configuration and no server required. SQLite is accessible on Mac OS-X, iOS, Android, Linux, and Windows.
It delivers a simple and user-friendly programming interface as it is written in ANSI-C. SQLite is also very small and light and the complete database can be stored in one cross-platform disk file.
The reasons for the great popularity of SQLite are its:
Independence from a server
Safe access from multiple processes and threads
Stores data in tables with one or more columns that contain a specific type of data
Core Data is the second main iOS storage technology available to app developers. Depending on the type of data and the amount of data you need to manage and store, both SQLite and Core Data have their pros and cons. Core Data focuses more on objects than the traditional table database methods. With Core Data, you are actually storing contents of an object which is represented by a class in Objective-C.
Although they are fundamentally different, Core data:
Uses more memory than SQLite
Uses more storage space than SQLite
Faster in fetching records than SQLite
There’s a new(ish) player in town called Realm. Realm was designed to be faster and more efficient than the previous database solutions. This new solution is a cross-platform mobile database called Realm. It is available in Objective-C and Swift, and it’s designed for iOS and Android.
The major upsides of Realm are:
It’s absolutely free of charge
Fast, and easy to use
Works on its own persistence engine for speed and performance
really great about it is that you can handle all the work with a couple
of lines of code. Realm is very easy to install and faster to work with
compared to SQLite and Core Data. Also, the database files are
shareable among iOS and Android.
If you are designing an app with a lot of records and for a large number of users, you need to pay special attention to scalability from the very beginning. Realm is great at this and it allows you to handle a lot of data fast.
To get started with Realm, all you need is at least iOS 8 or OS X 10.9. Older versions don’t support this new straightforward solution for managing local storage and databases.
Realm 的最大亮点是仅仅用一两行代码就能解决所有问题。Realm易于安装，并且运行速度比SQLite 和 Core Data都快。而且数据库文件即可以在IOS上使用，也可以在Android上使用。
要想使用Realm，只需要iOS 8 或 OS X 10.9以上的系统就可以了。旧的系统还不支持这种新的直接了当的管理本地存储和数据库的解决方式。
Switching To Realm
If you have been working with Core Data and you want to move on to Realm, the process is straightforward. Many developers have made the journey in a couple hours. Note that both Core Data and Realm treat data as objects so what you need to do is refactor the Core Data code to use Realm.
Realm has outlined the process thoroughly, but here is a summary:
Remove the Core Data Framework. Locate the parts of your code that include Core Data code and refactor them. They outline a nice trick to throw a compiler error for each line of code using Core Data
Remove the Core Data Setup Code. There’s going to be a portion of Core Data setup code somewhere in your app and you need to get rid of it all. Realm is automatically configured as soon as you access a Realm object for the first time. You can choose where you want to store the Realm data file, and it will still be optional at runtime.
Migrate Your Model Files. You can easily convert managed object subclasses to Realm. Realm manages property keywords internally (you don’t have to specify them), which makes the class header look more minimal. Also, you can safely remove all NSNumber cruft because Realm supports simple numerical data (NSInteger and CGFloat). There are also limitations to Realm. Unlike Core Data whose objects have NSManagedObjectID to identify objects uniquely, Realm leaves this up to you as the developer. Secondly, the current version of Realm can’t deal with object properties with a nil value. This is a minor inconvenience, but the developers are promising that the next version of Realm won’t have this problem.
Migrating Your Write Operations. Realm’s save operations are a little different than in Core Data and you need to get familiar with this. Once the Realm objects are added to a Realm object, they can’t be modified. This ensures consistency of data in different threads. To be able to modify the properties, the object where these were saved needs to be in a 'write' transaction.
Migrate Your Queries to be able to retrieve your data as needed. To fetch a file, in Core Data you need approximately 10 lines of code. In Realm, you can do the same thing with just one line.
Migrate Your Users’ Production Data for active apps created in Core Data, you can re-link the Core Data framework back into the app, fetch user’s data and pass it on to Realm. An easier solution for replaceable users’ data is to remove all Core Data save files and start over the next time the app is open.
key is to keep your design process simple with the best techniques and
tools. Once you’ve invested time to set up your tried and true process,
don’t let the "attraction to new" take you off course. May "the new"
work to be a part of your process, and let time decide what is not
如果你已经使用了Core Data，并且你想转换到Realm，过程很简单。很多开发者用了一两小时就完成了。注意Core Data 和 Realm 都是以对象的形式处理数据的，因此你只需要重构Core Data 代码，使用Realm就可以了。
移除Core Data 框架。定位你代码中包含Core Data 代码的部分，然后重构。它们总结了一个很好的方法来帮助使用 Core Data的每一行代码抛出编译错误。
移除Core Data 安装部分的代码。 你的应用里的某个地方一定有Core Data 安装代码的部分，你必须彻底的移除掉Core Data 。Realm在你第一次访问Realm对象的时候就会立马自动配置。你可以选择你想存储Realm数据文件的位置，但在运行时，你仍然可以选择。
迁移你的模型文件。 你可以可容易的转换管理对象的子类到Realm。Realm为了让类标头看起来更小些，在内部管理属性关键词（你不必自己指定它们）。你可以安全的去掉所有NSNumber的东西，因为Realm支持简单的数值类型（NSInteger和CGFloat)。但是Realm也有一些限制。不像Core Data的数据对象使用NSManagedObjectID来确定对象的唯一性，Realm把这个交给开发人员来管理。第二，当前版本的Realm还不能处理含有nil值的对象属性。这是一个小问题，但是在下一个版本里开发人员有望解决这个问题。
迁移你的写操作。Realm的保存操作和Core Data 有一点不相同的地方，你得了解这个问题，一旦把Realm数据对象添加到Realm对象，就不能被修改了。这确保了数据在不同线程下的一致性。为了能够修改属性。这些被保存的对象必须在’写’的事务里。
迁移你的查询， 让你的查询，在需要时可以检索到你的数据。在Core Data里，为了取一个文件，你需要写将近10行的代码。但是在Realm里仅仅需要一行就够了。
迁移用户在Core Data里产生数据，你可以在应用里重新连接Core Data框架，然后用它去数据，传递给Realm。对于那些可替代的用户数据有一个简单的解决方案就是删除所有的Core Data 保存文件，下次应用启动的时候重新开始。