PE PERSIS ISTIN ING G DATA WIT ITH REALM MOBIL ILE DATABASE
Adam Fisher • Application Developer Integrated Web & Mobile Solutions Team CareSource • Xamarin Certified Mobile Developer • May 2015 – May 2016 • July 2017 – July 2018 @calmtuna • Author of the Xamarin Forms Video Player component @adamgfisher • Worked with for 8 months in Chicago /adamfisher • Currently working on the CareSource mobile app
Outline • Existing Mobile Persistence Solutions • Benefits of a Mobile Database • What is Realm? • Code Examples • Questions
Existing Persistence Solutions KEY/VALUE STORAGE DATABASE STORAGE • iOS: NSUserDefaults • Android: SharedPreferences • React Native: AsyncStorage • Xamarin: Xamarin.Essentials
Realm SQLite Object Oriented Database Relational Database Cross-Platform Cross-Platform Objects Tables, Indexes, Views Remote Cloud Sync X Built-in Encryption X Live Objects X
await AsyncStorage.setItem(’ApplicationData', '{ "data": [ { "id": 1, "name": "Bitcoin", "symbol": "BTC", "website_slug": "bitcoin" }, { "id": 2, "name": "Litecoin", "symbol": "LTC", "website_slug": "litecoin" }, { "id": 3, "name": "Namecoin", "symbol": "NMC", "website_slug": "namecoin" }, { "id": 4, "name": "Terracoin", "symbol": "TRC", "website_slug": "terracoin" }, { "id": 5, "name": "Peercoin", "symbol": "PPC", "website_slug": "peercoin" }, { "id": 6, "name": "Novacoin", "symbol": "NVC", "website_slug": "novacoin" }, { "id": 8, "name": "Feathercoin", "symbol": "FTC", "website_slug": "feathercoin" }, { "id": 9, "name": "Mincoin", "symbol": "MNC", "website_slug": "mincoin" }, { "id": 10, "name": "Freicoin", "symbol": "FRC", "website_slug": "freicoin" }, { "id": 13, "name": "Ixcoin", "symbol": "IXC", "website_slug": "ixcoin" }, { "id": 14, "name": "BitBar", "symbol": "BTB", "website_slug": "bitbar" }, { "id": 16, "name": "WorldCoin", "symbol": "WDC", "website_slug": "worldcoin" }, { "id": 18, "name": "Digitalcoin", "symbol": "DGC", "website_slug": "digitalcoin" }, { "id": 25, "name": "GoldCoin", "symbol": "GLD", "website_slug": "goldcoin" }, { "id": 31, "name": "Argentum", "symbol": "ARG", "website_slug": "argentum" }, { "id": 32, "name": "Fastcoin", "symbol": "FST", "website_slug": "fastcoin" }, { "id": 34, "name": "Bitgem", "symbol": "BTG", "website_slug": "bitgem" }, { "id": 35, "name": "Phoenixcoin", "symbol": "PXC", "website_slug": "phoenixcoin" }, { "id": 37, "name": "Megacoin", "symbol": "MEC", "website_slug": "megacoin" }, { "id": 41, "name": "Infinitecoin", "symbol": "IFC", "website_slug": "infinitecoin" }, { "id": 42, "name": "Primecoin", "symbol": "XPM", "website_slug": "primecoin" }, { "id": 43, "name": "Anoncoin", "symbol": "ANC", "website_slug": "anoncoin" }, { "id": 45, "name": "CasinoCoin", "symbol": "CSC", "website_slug": "casinocoin" }, { "id": 49, "name": "Bullion", "symbol": "CBX", "website_slug": "bullion" }, { "id": 50, "name": "Emerald Crypto", "symbol": "EMD", "website_slug": "emerald" }, { "id": 51, "name": "GlobalCoin", "symbol": "GLC", "website_slug": "globalcoin" }, { "id": 52, "name": "XRP", "symbol": "XRP", "website_slug": "ripple" }, { "id": 53, "name": "Quark", "symbol": "QRK", "website_slug": "quark" }, { "id": 56, "name": "Zetacoin", "symbol": "ZET", "website_slug": "zetacoin" }, { "id": 57, "name": "SecureCoin", "symbol": "SRC", "website_slug": "securecoin" }, { "id": 58, "name": "Sexcoin", "symbol": "SXC", "website_slug": "sexcoin" }, { "id": 61, "name": "TagCoin", "symbol": "TAG", "website_slug": "tagcoin" }, { "id": 63, "name": "I0Coin", "symbol": "I0C", "website_slug": "i0coin" }, { "id": 64, "name": "FlorinCoin", "symbol": "FLO", "website_slug": "florincoin" }, { "id": 66, "name": "Nxt", "symbol": "NXT", "website_slug": "nxt" }, { "id": 67, "name": "Unobtanium", "symbol": "UNO", "website_slug": "unobtanium" } ], "metadata": { "timestamp": 1532545712, "num_cryptocurrencies": 66, "error": null } }');
Realm, Inc. • Danish startup founded in 2011 • Native object oriented database • Used to be called TightDB
What is a Realm? • A Realm is a lightweight object container that acts like a database • Data can be queried, filtered, interconnected, persisted • Live/Reactive objects • Can contain multiple types with schema enforcement
Benefits of a database built for mobile Offline-First Functionality Cross-Platform Apps Make your app work as well offline Use the same database for all your as it does online. apps, on any major platform. Encryption Fast Queries Even complex queries take Secure your data with transparent nanoseconds, and stay up to date encryption and decryption. with new data. Reactive Architecture Safe Threading Connect your UI to Realm, and Access the same data concurrently data changes will appear from multiple threads, with no automatically. crashes.
Supported Platforms
Correlation Realm Relational Database Database /Connection Realm Table Schema Row Object Column Object.Property
Re Realm St Studio • Open and edit local and synced Realms • Administer any Realm Object Server instance • Available for Mac, Windows, Linux
Supported Types (JavaScript) Types Maps To JavaScript Boolean values bool JavaScript number values int, float, double Internally int and double are stored as 64 bits while float is stored with 32 bits string string ArrayBuffer data Date date
Query Language • Inspired by NSPredicate • Query Language: https://realm.io/docs/javascript/latest/api/tutorial-query-language.html • Cheat Sheet: https://bit.ly/2nrwC7S
Fo Format String Su Summary
Ba Basic c Com Comparison ons
Ba Basic c Com Compou ound Pr Predicates
Str String Com Comparison on Op Operators
Ag Aggregate Op Operators
Un Unsupported Op Operations
Un Unsupported Op Operations
• Using OR OR OR instead of IN , results in repeatable code Ti Tips s & Tricks and can be less efficient. • When using REGEX and Matches , make sure they are the last part of your predicate statement so it does less work. • Write transactions have a non-negligible overhead; you should try to minimize the number of write blocks within your code.
Define the Objects in Your Schema const PersonSchema = { name : 'Person' , primaryKey : 'id' , properties : { id : 'int' , // primary key firstName : 'string' , lastName : 'string' } };
Define the Objects in Your Schema const CryptocurrencySchema = { name : 'Cryptocurrency' , primaryKey : 'symbol' , properties : { name : { type : 'string' , indexed : true }, symbol : 'string' , price : 'double?' , marketCap : { type : 'int' , default : 0 }, founder : 'Person' } };
Recommend
More recommend