In-Memory Realms を使って API キャッシュした話 / Use In-Memory Realms

In-Memory Realms を使って API キャッシュした話 / Use In-Memory Realms

Cf724b865a0a76fa7183b70b3b2b208a?s=128

satoru.sasaki

June 29, 2018
Tweet

Transcript

  1. In-Memory Realms ࢖ͬͯ API Ωϟογϡͨ͠࿩ ΈͯͶͷMeetup #1 2018-06-27 satoru.sasaki

  2. ࣗݾ঺հ —ࠤʑ໦ ڿ (@caad1229) —2015೥10݄ೖࣾ —ࡢ೥ SNSϛΫγΟ -> ΈͯͶ ʹҠಈ

    —झຯ͸ϨίʔυͰ Jazz ௌ͘͜ͱ
  3. ΈͯͶΞϓϦͱrealm —ΈͯͶͰ͸ϢʔβͷϝσΟΞ৘ ใΛ realm ͰΩϟογϡ͍ͯ͠ Δ —ࠩ෼ಉظͱશମಉظͰ realm ʹ ಉظ

    —ىಈ࣌ɾ௨஌ड৴ɾΞοϓϩ ʔυ࣌
  4. ΈͯͶΞϓϦͱ Architecutre —Android ͷ৽نػೳ͸ MVVM Ͱ࣮૷ —iOS ͱઃܭΛἧ͑ΔͨΊʹ CleanArchitecture Λ࢖͍ͬͯ

    Δͱ͜Ζ΋͋Δ
  5. API ΩϟογϡΛ࢖͏ܦҢ —Ϣʔβͷ໨ʹ৮ΕΔػձ͕ଟ͍͕ɺߋ৽͕සൟͰͳ͍ —ը໘͕දࣔ͢Δͨͼʹ fetch ͢Δඞཁ͕ͳ͍ —Push ͳͲͰ࠷৽Λऔಘ͢Ε͹͍͍ఔ౓

  6. In-memory Realm ʹ͍ͭͯ —σʔλ͸ϝϞϦॻ͔ΕΔ —Realm ΠϯελϯεͷࢀরΧ΢ϯλ͕ 0 ʹͳΔͱσ ʔλ͕ফ͑Δ —ΞϓϦىಈதͰσʔλΛอ͍࣋ͨ͠৔߹͸

    Application ΫϥεͰ Realm ΠϯελϯεΛอ࣋
  7. In-memory Realm ͷಋೖ —Application Ͱ realm ΠϯελϯεΛอ࣋ open class CustomApplication

    : Application() { private var realm: Realm? = null : }
  8. In-memory Realm ͷಋೖ —onCreate Ͱੜ੒ͱอ࣋ override fun onCreate() { super.onCreate()

    val config = RealmConfiguration.Builder() .name("in-memory.realm") .inMemory() .build() // อ࣋͢Δ realm = Realm.getInstance(config) }
  9. In-memory Realm ͷಋೖ —onTerminate Ͱഁغ override fun onTerminate() { realmInMemory?.close()

    super.onTerminate() }
  10. cache ػߏͷͨΊͷઃܭ —MVVM ࢖͏

  11. Data ͷѻ͍ —Mapper Λ࢖ͬͯڞ௨ Object ʹ ͢Δ —Mapper ʹม׵͢Δ interface

    Mapper<E, D> { fun mapFromEntity(type: E): D fun mapToEntity(type: D): E }
  12. Cache ͷσʔλ —RealmObject —ϦϨʔγϣϯ͕؆୯ʹͰ͖Δ RealmList<T> Λ࢖͏

  13. Repository —Local ͱ Remote Λ࢖͏ —cache ͷอଘͱऔಘϩδοΫ —Local ͔Βऔಘͯ͠ɺͳ͚Ε ͹

    Remote ͔Β fetch ͢Δ —ڧ੍తʹ Remote ͔Βऔಘ΋ Ͱ͖ΔΑ͏ʹ͢Δ
  14. Repository

  15. Demonstration

  16. Demonstration Qiita هࣄҰཡΛऔಘ͢ΔΞϓϦ https://github.com/caad1229/apicache dagger2 okHttp3 retrofit2 rxJava2 realm Glide

  17. ՝୊ͱ͔ —expire ͷλΠϛϯά —ΩϟογϡΫϦΞͷλΠϛϯά —ϖʔδϯάɾϢʔβૢ࡞ʹΑΔςʔϒϧͷߋ৽

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