Join Brian Advent for an in-depth discussion in this video Traditional ways to store data, part of Learning Realm for iOS App Development.
- [Instructor] Before we talk about Realm in more detail, let's first of all have a look at what data persistence is, because Realm is all about storing data, and what native ways there are to store data. So first of all, what is data persistence? We're talking about data persistence when we're talking about storing information. And your users actually generate data like notes, text, images, and so on, and you do not want this data to be lost every time your app is closed. So you're saving this data somewhere, either in a database or somewhere on your device, or you're storing it in the cloud, so that your users can access this data from multiple devices and sync between the cloud and the local database.
So data persistence is a very vital part of iOS app development, because your users are really expecting this to happen. And it is a basic requirement for most apps, because you cannot allow your user data to be lost, after your app is closed. And ideally, data persistence allows offline access to stored data, so that your users do not have to go online or use their mobile data to access already stored information. And before we talk about Realm, let's talk now about some native ways to store data, because native ways are always preferable, because you do not depend on a third party and the changes that they make, or slower changes that might occur when you're switching between Swift language versions, like version three and four.
So you have now a good and solid knowledge base to decide if you want to use a native way to store data or use, for example, Realm to store data. So what native ways are there to store data? You have, for example, user defaults, Core Data, NSCoding, or CloudKit to store information. All of these technologies are coming from Apple and are already included in iOS. So what are user defaults? User defaults allows you to store key-value pairs.
And this is really just small amounts of data, so do not store photos, for example, in user defaults. You can store some settings, or a username, or a high score in user defaults. This is really cool, because you get no queries here, so you cannot search for data easily. You have to know where to look for your data. But therefore, user defaults are really easy to use, and they can easily be synced with iCloud. And then there is Core Data, which is an object graph and persistence framework, provided by Apple.
It allows you to create complex data models. You have sophisticated query options but only limited iCloud support. And it's harder to learn than using user defaults on Realm. Realm is very easy to learn, as you're going to experience through this course. And what you also can do is using the NSCoding protocol, because it is an easy way to serialize data, and you have direct control over the serialization process. But therefore, the NSCoding protocol is slower than Core Data, and it does not allow you to perform any queries.
Nevertheless, it is still a perfectly reasonable choice in certain situations, because not all apps need to query data, and not all apps need to have automatic migration between data models, and not all apps work with large or complex object graphs. So even apps that do may have certain components better served with a simpler solution like NSCoding. And then there's also CloudKit. And CloudKit is an online first service, which means that your users can store existing data in the cloud, and they can access it from multiple devices, and it even offers a public area, so that different users can access the same amount of information.
And it's very convenient for users, because they do not have to create a user account first. And the cool thing is that you can even integrate CloudKit, or the CloudKit user service, with Realm. But to summarize that, CloudKit provides interfaces for moving data between your app and your iCloud containers. And you use CloudKit to take your app's existing data and store it in the cloud, so that the user can access it on multiple devices. And there is this public area that you also have. So these are just a few of the ways that you can actually store and persist data, on a device or in the cloud, that are coming from Apple.
And now we're going to have a look at what Realm offers. And this is going to help you decide which way you can use to persist data that fits your needs best.
- What is Realm?
- Data storage
- Installing Realm
- Working with object classes
- Making a model
- Adding objects
- Using the Realm browser
- Retrieving and updating objects
- Creating queries and filters
- Deleting objects
- Realm relationships
- Fetching notes and notebooks