realm - phoenix mobile festival

30
DJ RAUSCH ANDROID ENGINEER @ MOKRIYA DJRAUSCH.COM REALM

Upload: dj-rausch

Post on 14-Jan-2017

39 views

Category:

Engineering


3 download

TRANSCRIPT

DJ RauschAndroid Engineer @ MokriyaDJRAUSCH.com Realm

Realm - A Mobile Database ReplacementPhoenix Mobile FestivalUse #phxmobi as the twitter hashtag and follow @phxmobifestival for updates. Download Phoenix Mobile Festival App (search for 'phxmobi') from AppStore or Google Play. From the app you can create your own agenda for the day and provide feedback on the sessions.

Realm - A Mobile Database ReplacementWho Am I?Android Developer at Mokriya.Developing Android apps since the start (first Android phone was the Droid)Before working at Mokriya, worked for Jaybird, and a few smaller companiesSelf published a few apps - Spotilarm, Volume Sync, Bill Tracker

Realm - A Mobile Database ReplacementWhat Is Realm?Its a database.A replacement for Sqlite. It is NOT an ORM for Sqlite.

Realm - A Mobile Database ReplacementWhy Should I use It?Easy SetupCross PlatformAndroid, iOS (Objective-C and Swift), Xamarin, React NativeFAST

Realm - A Mobile Database ReplacementWho Is Using Realm?

Realm - A Mobile Database ReplacementFeatures Of RealmFluent InterfaceField AnnotationsMigrationsEncryptionAuto Updates and NotificationsRxJava Support

Realm - A Mobile Database ReplacementFluent InterfacegetRealm().where(Bill.class).equalTo("deleted", false).between("dueDate", new DateTime().minusWeeks(1).toDate(), new DateTime().plusWeeks(1).plusDays(1).toDate()).findAll();

https://github.com/djrausch/BillTracker/blob/master/app/src/main/java/com/djrausch/billtracker/util/BillUtil.java#L46

Realm - A Mobile Database ReplacementField Annotations@PrimaryKeyTable PK@RequiredRequire a value, not null@IgnoreDo not persist field to disk

Realm - A Mobile Database ReplacementMigrationspublic class Migration implements RealmMigration { @Override public void migrate(DynamicRealm realm, long oldVersion, long newVersion) { RealmSchema schema = realm.getSchema();

if (oldVersion == 0) { //Add pay url to bill schema.get("Bill") .addField("payUrl", String.class); oldVersion++; } }}

https://github.com/djrausch/BillTracker/blob/master/app/src/main/java/com/djrausch/billtracker/models/Migration.java

Migrations work much like onUpgrade in Sqliteboxed types

Realm - A Mobile Database ReplacementEncryptionRealmConfiguration config = new RealmConfiguration.Builder(context) .encryptionKey(getKey()) .build();

Realm realm = Realm.getInstance(config);https://realm.io/docs/java/latest/#encryption

I havent used this yet.Android KeyStore

Realm - A Mobile Database ReplacementAuto Updates & Notificationsbills.addChangeListener(new RealmChangeListener() { @Override public void onChange(RealmResults element) { if (adapter.getItemCount() == 0) { noBillsLayout.setVisibility(View.VISIBLE); recyclerView.setVisibility(View.GONE); } else { noBillsLayout.setVisibility(View.GONE); recyclerView.setVisibility(View.VISIBLE); } }});

https://github.com/djrausch/BillTracker/blob/master/app/src/main/java/com/djrausch/billtracker/MainActivity.java#L132

Note, I am not using the new RealmResults. It is recommended to only use this to notify the UI of any data changes.

Realm - A Mobile Database ReplacementRxJava

getRealm().where(Bill.class).contains(uuid, billUuid).findFirst().asObservable().filter(new Func1() { @Override public Boolean call(Bill bill) { return bill.isLoaded(); }}).subscribe(new Action1() { @Override public void call(Bill bill) { setUI(bill);

if (bill.paidDates != null) { if (adapter == null) { adapter = new PaidDateRecyclerViewAdapter(ViewBillDetails.this, bill.getPaidDates()); recyclerView.setLayoutManager(new LinearLayoutManager(ViewBillDetails.this)); recyclerView.setAdapter(adapter); } } }});

https://github.com/djrausch/BillTracker/blob/master/app/src/main/java/com/djrausch/billtracker/AddOrEditBillActivity.java#L109https://realm.io/docs/java/latest/#rxjava

I am still new with RxJava so this isnt that advanced. Realm docs go into more details on this

Realm - A Mobile Database ReplacementOk, So How Do I use It?There is no schema set upSimply have your model classes extend RealmObjectpublic class Bill extends RealmObject { @PrimaryKey public String uuid; public String name; public String description; public int repeatingType = 0; public Date dueDate; public String payUrl; public RealmList notes; public RealmList paidDates; public boolean deleted = false; public int amountDue = 0;

https://github.com/djrausch/BillTracker/blob/master/app/src/main/java/com/djrausch/billtracker/models/Bill.java

POJOCan have public, private, protected methods as well

Realm - A Mobile Database ReplacementField TypesSupports standard field types including DateRealm supports the following field types: boolean, byte, short, int, long, float, double, String, Date and byte[]. The integer types byte, short, int, and long are all mapped to the same type (long actually) within Realm. Moreover, subclasses of RealmObject and RealmList