Realm Database
Total Page:16
File Type:pdf, Size:1020Kb
Realm - Building Modern Swift Apps with Realm Database Realm: Building Modern Swi3 Apps with Realm Database By Marin Todorov Copyright ©2018 Razeware LLC. No<ce of Rights All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner. No<ce of Liability This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software. Trademarks All trademarks and registered trademarks appearing in this book are the property of their own respective owners. raywenderlich.com 2 Realm - Building Modern Swift Apps with Realm Database Dedica<ons "To my father. To my mom. To Mirjam and our beautiful baby." — Marin Todorov "For my wife Elia — my love, inspiration, and rock ❤. To my family and friends for their support." — Shai Mishali raywenderlich.com 3 Realm - Building Modern Swift Apps with Realm Database About the author Marin Todorov is the author of this book. Marin is one of the founding members of the raywenderlich.com team and has worked on seven of the team's books. Besides crafting code, Marin also enjoys blogging, teaching, and speaking at conferences. He happily open- sources code. You can find out more about Marin at www.underplot.com. About the editors Shai Mishali is the technical editor of this book. He's the iOS Lead for the Tim Hortons mobile app, and is involved in several open source projects on his spare time - mainly the RxSwiftCommunity and RxSwift projects. As an avid enthusiast of hackathons, Shai took 1st place at BattleHack Tel-Aviv 2014, BattleHack World Finals San Jose 2014, and Ford's Developer Challenge Tel-Aviv 2015. You can find him on GitHub and Twitter @freak4pc. Chris Belanger is the editor of this book. Chris is the Editor in Chief at raywenderlich.com. He was a developer for nearly 20 years in various fields from e-health to aerial surveillance to industrial controls. If there are words to wrangle or a paragraph to ponder, he’s on the case. When he kicks back, you can usually find Chris with guitar in hand, looking for the nearest beach. Twitter: @crispytwit. raywenderlich.com 4 Realm - Building Modern Swift Apps with Realm Database Table of Contents: Overview Introducon...................................................................... 11 Book license.............................................................. 13 Book source code and forums .............................. 14 About the cover ....................................................... 15 Secon I: GeDng Started with Realm......................... 16 Chapter 1: Hello Realm! ......................................... 17 Chapter 2: Your First Realm App .......................... 31 Secon II: Creang a Realm Data Model ................... 48 Chapter 3: Object Basics and Data types .......... 49 Chapter 4: Schema relaonships.......................... 70 Secon III: Working with Realm Data......................... 90 Chapter 5: Reading and Wring Objects............ 91 Chapter 6: Noficaons and Reacve Apps ... 113 Secon IV: Intermediate Realm Apps....................... 135 Chapter 7: Realm Configuraons....................... 136 Chapter 8: Mulple Realms / Shared Realms . 156 Chapter 9: Dependency Injecon and Tesng...................................................................... 176 Secon V: Advanced Realm Apps.............................. 195 raywenderlich.com 5 Realm - Building Modern Swift Apps with Realm Database Chapter 10: Effecve Mul-threading ............. 196 Chapter 11: Beginning Realm Migraons........ 216 Chapter 12: Advanced Schema Migraons..... 236 Secon VI:Realm Cloud ............................................... 254 Chapter 14: Real-Time Sync with Realm Cloud........................................................................ 255 Conclusion...................................................................... 276 More Books You Might Enjoy..................................... 277 raywenderlich.com 6 Realm - Building Modern Swift Apps with Realm Database Table of Contents: Extended Introducon...................................................................... 11 Book license ..................................................................... 13 Book source code and forums...................................... 14 About the cover............................................................... 15 Secon I: GeDng Started with Realm......................... 16 Chapter 1: Hello Realm!................................................. 17 Realm Database ...................................................................................................... 19 Live Objects............................................................................................................. 21 Realm Studio............................................................................................................ 24 Installaon................................................................................................................ 26 Curious to know more?......................................................................................... 30 Where to go from here?........................................................................................ 30 Chapter 2: Your First Realm App.................................. 31 GeDng started........................................................................................................ 32 Realm objects .......................................................................................................... 34 Reading objects from disk .................................................................................... 35 Creang some test data........................................................................................ 37 Adding an item........................................................................................................ 39 Reacng to data changes...................................................................................... 41 Modifying a persisted object ............................................................................... 43 Deleng items......................................................................................................... 45 Challenges................................................................................................................ 47 Secon II: Creang a Realm Data Model ................... 48 Chapter 3: Object Basics and Data types .................. 49 GeDng started........................................................................................................ 50 Object basics ........................................................................................................... 51 raywenderlich.com 7 Realm - Building Modern Swift Apps with Realm Database Storage data types ................................................................................................. 53 Computed properes ............................................................................................ 59 Convenience inializers ........................................................................................ 60 Meta informaon.................................................................................................... 63 @objcMembers ....................................................................................................... 67 Challenges................................................................................................................ 69 Chapter 4: Schema relaonships ................................. 70 GeDng started........................................................................................................ 71 To-one relaonships .............................................................................................. 71 To-many relaonships (for objects) .................................................................... 77 To-many relaonships (for values) ...................................................................... 81 Linking objects ........................................................................................................ 86 Designing object schemas.................................................................................... 88 Challenges................................................................................................................ 89 Secon III: Working with Realm Data......................... 90 Chapter 5: Reading and Wring Objects ................... 91 GeDng started........................................................................................................ 92 Reading objects....................................................................................................... 93 Live results............................................................................................................. 105 Wring objects ..................................................................................................... 107 Challenges ............................................................................................................