Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Realm for Android

Realm for Android

Realm for Android

Start up
#potatotips 10

6dd0483f1353a4a359e92633cfd65c64?s=128

Daichi Furiya (Wasabeef)

October 22, 2014
Tweet

Transcript

  1. Realm for Android potatotips #10 CyberAgent, Inc. ߱໼ େ஍

  2. About Me Wasabeef (D.Furiya) @wasabeef_jp wasabeef http://wasabeef.jp

  3. Realm SQLiteΛஔ͖׵͑Δ໨తͰ࡞ΒΕͨ Mobile DataBase

  4. Prerequisites • SDK >= Android 2.3 (9) • Android Studio

    >= 0.8.6 • JDK version >= 7
  5. ग़యɿhttp://realm.io/news/realm-for-android

  6. ग़యɿhttp://realm.io/news/realm-for-android

  7. ग़యɿhttp://realm.io/news/realm-for-android

  8. Ͳ͏΍Β଎͍Β͍͠

  9. Setup compile 'io.realm:realm-android:0.71.+'

  10. Models public class User extends RealmObject { private String name;

    private int age; ! @Ignore private int sessionId; ! // Standard getters & setters generated by your IDE… public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getSessionId() { return sessionId; } public void setSessionId(int sessionId) { this.sessionId = sessionId; } }
  11. Writes // Obtain a Realm instance Realm realm = Realm.getInstance(this,

    "wasabeef.realm"); ! realm.beginTransaction(); ! // Create a new object User user = realm.createObject(User.class); user.setName("Wasabeef"); user.setEmail(“chip@wasabeef.jp"); ! realm.commitTransaction();
  12. Queries ! // Build the query looking at all users:

    RealmQuery<User> query = realm.where(User.class); ! // Add query conditions: query.equalTo("name", "Wasabeef"); query.or().equalTo(“name”, "Chip"); // Execute the query: RealmResults<User> resultAll = query.findAll(); ! // Or alternatively do the same all at once (the "Fluent interface"): RealmResults<User> result = realm.where(User.class) .equalTo("name", "Wasabeef") .or() .equalTo("name", "Chip") .findAll();
  13. Sorting ! // Query RealmResults<User> result = realm.where(User.class).findAll(); ! //

    Asc RealmResults<User> sortedAscending = result.sort(“age"); ! // Desc RealmResults<User> sortedDescending = result.sort("age", RealmResults.SORT_ORDER_DECENDING);
  14. Deletion ! // All changes to data must happen in

    a transaction realm.beginTransaction(); ! // remove single match result.remove(0); result.removeLast(); ! // Delete all matches result.clear(); ! realm.commitTransaction()
  15. RealmObject & RealmResults ThreadؒͰͷड͚౉͠ͷ૝ఆ͸ͯ͠ͳ͍ͷͰ Realm.getInstance()ͱRealmQuery() Λ௨ͯ͠࠶औಘ͢Δඞཁ͕͋Δ

  16. Migration ! RealmMigration………… ! ActiveAndroidͱ͔SugarORMͱ͔ͱ ൺ΂ͯ͸͍͚ͳ͍… ! ࣗ෼ͰTableͷ࠶ߏஙͷॲཧΛॻ͔ͳ͍ͱ…

  17. ๊͑ͯΔ໰୊఺ 1. Setter/Getter͔͠ॻ͚ͳ͍ 2. ENUM͸Supportͯ͠ͳ͍ 3. RealmObjectΛGSONͰSerializeग़དྷͳ͍ 4. Migration͕Πέͯͳ͍

  18. Latest 0.71.0 (2014.10.22)

  19. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ