This workshop will be retired on May 31, 2020.
Introduction7:32 with Amit Bijlani
Get an overview of the Realm database and the basics of of how to create and savel model objects to the database.
[MUSIC] 0:00 Hi, I'm Amit, an iOS developer and teacher. 0:04 Today, we're going to talk about a new mobile database called Realm. 0:08 Now before we dive into Realm, let's review some of the existing solutions we 0:12 have at our disposal to persist data to disk. 0:17 For example, you create a productivity application. 0:21 You want to save tasks or reminders the user has created and 0:25 retrieve them the next time the user opens the app. 0:29 There are several ways we can currently persist data 0:32 this JSON/PLIST, SQLite, Core Data. 0:37 JSON/PLIST, it's basically serializing objects to disk using NSCoding. 0:40 Basically, you create your objects and then serialized them to disk. 0:46 It's great and very limited use cases. 0:52 You can't obviously use complex queries to filter your results. 0:55 It's all in memory or all on disk. 1:00 It's very slow. 1:03 Each time you need something, you need to either serialize or deserialize it. 1:04 And finally, it's not thread-safe. 1:09 Next, we have SQLite. 1:11 Schema setup, primary and foreign keys. 1:15 You need to define the relations between the tables. 1:18 Define the schema of all the tables. 1:21 You have to manually write queries to fetch data. 1:24 You need to query results and then map those to models. 1:27 Schema migrations need to be manually managed. 1:32 Of course, queries are a lot faster with SQLite and 1:36 you have third-party libraries to make it easy, for example, FMDB. 1:39 And finally, you have core data by Apple. 1:43 It has a very steep learning curve. 1:46 And if you've ever tried to learn Core Data, you know your head will start 1:49 spinning once you start talking about persistent stores, manage object contacts 1:53 and all those terminologies simply to store or retrieve data from the database. 1:58 Core Data also has a object-graph mapper over SQLite. 2:03 Apple always say is that Core Data is not an object relational mapper or 2:09 ORM, but that it is an object-graph, 2:14 but it still has to map that graph to the underlying SQLite database. 2:16 Schema migrations are easier, but still cumbersome. 2:21 Core Data certainly makes it easier to migrate, but versioning and 2:25 data migrations definitely make it a bit more challenging. 2:29 It's not easy to implement concurrency with so many permutations and 2:33 how to implement concurrency, implementing it is definitely a very challenging task. 2:38 And finally, debugging core data is certainly 2:43 a complicated task not lending itself to be very developer friendly. 2:46 That's why Realm came into the picture to create a mobile database of the future. 2:51 Realm is an open-source database framework. 2:57 It's a complete replacement for Core Data and SQLite. 3:01 It's implemented from scratch and it's a zero-copy object store, 3:05 which means that objects and memory are directly mapped to those on disk. 3:10 And since it's not a wrapper built on top of SQLite, 3:15 they have created a modern database for 3:19 mobile devices with limited resources which makes it really, really fast. 3:22 And finally, it's cross-platform. 3:27 It's on Mac, Linux, iOS and Android. 3:29 And most of all, it's free. 3:33 If you go to realm.io, you will see that it's being used by a lot of popular 3:36 companies and they claim to have over a billion users rely on it. 3:40 Maybe you're convinced or maybe just curious. 3:44 You want to know if Realm is the right solution for your next project. 3:48 So stay tuned, because I'm going to show you how to build a shopping list app with 3:52 very minimal effort. 3:57 Before we dive into the app, let's cover some of the basics. 3:59 With Core Data, you have to understand a lot of terminology and 4:02 concepts about the architecture before you even wrote a single line of code. 4:06 With Realm, all you have to know is that you can get 4:11 an instance of your Realm database with one line of code. 4:14 Realm creates a database file in the documents directory of your application, 4:19 which you can browse using the Realm Browser, Mac client. 4:23 Realm data models are defined using SWF classes with properties. 4:27 Simply subclass object or 4:32 an existing model class to create your Realm data model objects. 4:34 Realm model objects mostly function like any other SWF objects. 4:40 You can add your own methods and protocols to them and 4:45 use them like you would any other object. 4:48 Notice that the Realm model properties need the dynamic var attribute in order 4:51 for these properties to become accessors for the underlying database data. 4:56 Notice the category property, which will establish the many to 5:01 one relationship as long as category is a subclass of object. 5:06 For a one to-many relationship like a category containing multiple items, 5:11 we have to use a list which is very similar to an array and 5:16 objects contained in a list can be accessed using the indexed subscripting. 5:21 Unlike arrays, lists only hold objects of a single subclass type. 5:26 Creating instances off your object subclass or your Realm model is just as 5:32 easy as defining one, you can simply create an instance of the model and 5:37 then assign its properties values since we have defined defaults. 5:43 Just make sure you have the appropriate keys and 5:48 values defined or we can use a dictionary to initialize it. 5:51 Finally, we can instantiate it using arrays. 5:55 The values in the arrays have to be in the same order as 5:59 the corresponding properties in the model. 6:02 Persisting or saving objects to the Realm database. 6:06 It's important to note that you write the data to the Realm database in each 6:10 transaction, which means that it's an asynchronous process which would 6:14 throw an error if the write failed. 6:19 The write will not be committed preventing the database from being corrupted. 6:22 Any changes made are available to the other threads that use the same Realm 6:27 when the right transaction is committed. 6:31 Also, please note that the rights block each other and 6:34 will block that thread that they're made on. 6:36 The recommended architecture is to have your rights on a background thread, so 6:39 as not to block the main thread which is in line with how you would set it up in 6:43 core data as well. 6:48 If you have no idea what I mean by all this threading, 6:50 then check out the workshop on concurrency linked in the teacher's notes. 6:53 When you want to update an object and persist it, 6:56 then simply do it within a transaction. 6:59 Past the object to be deleted to the realm.delete method 7:02 within a write transaction. 7:06 These are the very basics of Realm. 7:08 As we can see, the syntax and concepts are very simple. 7:11 If you can create model objects within your app, then you can implement Realm. 7:15 In the next video, we'll create a shopping list application using Realm 7:20 where you will learn how to insert, update, delete, query and 7:25 sort data from the Realm database. 7:28
You need to sign up for Treehouse in order to download course files.Sign up