Realm Mobile Database Presentation
Realm Mobile Database Presentation
Realm Mobile Database Presentation
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
• iOS: NSUserDefaults
• Android: SharedPreferences
• Xamarin: Xamarin.Essentials
Realm SQLite
Object Oriented Database Relational Database
Cross-Platform Cross-Platform
Reactive Architecture
Safe Threading Connect your UI to Realm, and
Access the same data concurrently
from multiple threads, with no data changes will appear
crashes. automatically.
Supported Platforms
Correlation
Realm Relational Database
Schema Table
Object Row
Object.Property Column
Realm
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
string string
data ArrayBuffer
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
Format String
Summary
Basic
Comparisons
Basic
Compound
Predicates
String
Comparison
Operators
Aggregate
Operators
Unsupported
Operations
Unsupported
Operations
• Using OR OR OR instead of IN, results in repeatable code
Tips & 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'
}
};
Define the Objects in Your Schema
const ExchangeSchema = {
name: 'Exchange',
primaryKey: 'name',
properties: {
name: 'string',
websiteUrl: 'string',
coins: 'Cryptocurrency[]',
}
};
Realm.open({schema: [PersonSchema, ExchangeSchema, CryptocurrencySchema]})
.then(realm => {
// Create Realm objects and write to local storage
realm.write(() => {
const bitcoin = realm.create('Cryptocurrency',
{ name: 'Bitcoin', symbol: 'BTC', price: 7500 });
realm.write(() => {
// Create a coin object
let coin = realm.create(‘Cryptocurrency’,
{
title: 'Potcoin',
symbol: 'POT',
price: 77.45
});
const CryptocurrencySchema = {
name: 'Cryptocurrency',
primaryKey: 'symbol',
properties: {
// The following property definitions are equivalent
founder: {type: 'Person'},
founder: 'Person'
}
};
To-Many Relationships
const exchange = realm.objects('Exchange').filtered('name = "Coinbase "')[0];
let coins = createCoinObjects(); // <-- Not yet in Realm
// exchange.coins = coins; // <-- Error
coins = realm.copyToRealmOrUpdate(coins);
exchange.coins = coins; // <-- OK
Inverse Relationships
• Links to other objects are unidirectional.
• With LinkingObjects properties, you can obtain all objects that link to a given object from a
specific property.
const PersonSchema = {
name: 'Person',
properties: { exchangesOwned: 'Exchange[]' }
}
const ExchangeSchema = {
name: ‘Exchange',
properties: {
// No shorthand syntax for linkingObjects properties
founder: {type: 'linkingObjects', objectType: 'Person', property: 'exchangesOwned'}
}
}
Migrations
Realm.open({ schema: [PersonSchema, ExchangeSchema, CryptocurrencySchema],
schemaVersion: 1,
migration: (oldRealm, newRealm) => {
// only apply this change if upgrading to schemaVersion 1
if (oldRealm.schemaVersion < 1) {
const oldObjects = oldRealm.objects('Person');
const newObjects = newRealm.objects('Person');
// loop through all objects and set name property in the new schema
for (let i = 0; i < oldObjects.length; i++) {
newObjects[i].name =
oldObjects[i].firstName + ' ' + oldObjects[i].lastName;
}