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

matching_dev1

Ryo.Nitami
November 14, 2018

 matching_dev1

Ryo.Nitami

November 14, 2018
Tweet

More Decks by Ryo.Nitami

Other Decks in Technology

Transcript

  1. Paging Library ʹ͓͚ΔΞΫγϣϯΛ ϑοΫʹͨ͠Ϧετߋ৽ 2018/11/14 Matching Dev Meetup: iOS /

    Android Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 1
  2. Who? — Ryo Nitami — Android Application Engineer @ playmotion,

    Inc. — Twitter: bird_tummy — GitHub: rnitame Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 2
  3. Ϋϩεϛʔ — "͢Εҧ͍Λ࿀ͷ͖͔͚ͬʹ" — Ґஔ৘ใΛ࢖ͬͨϚονϯάαʔϏε — 10/30 ʹ Android ΞϓϦΛϑϧϦχϡʔΞϧ͠·ͨ͠ʂ

    — Architecture Components ΋Γ΋Γ + Kotlin 100% — ͜Ε͔ΒΞοϓσʔτ͕Μ͹Γ·͢ Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 3
  4. Paging Library — 2017 ೥ͷ I/O Ͱൃද͞ΕͨɺϖʔδϯάʹಛԽͨ͠ϥΠϒ ϥϦ — "The

    Paging Library makes it easier for you to load data gradually and gracefully within your app's RecyclerView." — 2018/11/14 ݱࡏɺ2.1.0-beta01 Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 4
  5. ΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ ৽ — ඇදࣔϢʔβʔϦετͳͲ — ʮϘλϯΛԡ͢ → API Λୟ͘ →

    View Λߋ৽͢Δʯͱ͍͏ྲྀΕ͕ ༷ʑͳ৔໘Ͱൃੜ Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 7
  6. PagedList ͸ MutableList Ͱ͸ͳ͍ʂ — googlesamples ͷ issue Ͱ͸ Room

    ࢖͓ͬͯίϝϯτ͕ ͋Δ2 — ΫϩεϛʔͰ͸͜ͷํ๏ͰରԠ 2 https://github.com/googlesamples/android-architecture-components/issues/281 Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 10
  7. class UserBoundaryCallback( private val helper: UserPagingRequestHelper ) : PagedList.BoundaryCallback<User>() {

    @MainThread override fun onZeroItemsLoaded() { } @MainThread override fun onItemAtFrontLoaded(itemAtFront: User) { } @MainThread override fun onItemAtEndLoaded(itemAtEnd: User) { super.onItemAtEndLoaded(itemAtEnd) // API Λୟ͍ͯ get data helper.loadAfterIfNotRunning(before = itemAtEnd.time) } } Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 12
  8. @Dao interface UserDao { @Query("SELECT * FROM users") fun allUsers():

    DataSource.Factory<Int, User> @Insert(onConflict = OnConflictStrategy.REPLACE) fun save(users: List<User>): List<Long> } Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 13
  9. private val config = PagedList.Config.Builder() .setEnablePlaceholders(false) .setInitialLoadSizeHint(LOAD_COUNT) .setPageSize(LOAD_COUNT) .build() private

    val helper = UserPagingRequestHelper().start() // ͜ͷ LiveData Λ Activity / Fragment Ͱ observe ͯ͠ submitList val users = LivePagedListBuilder(userDatabase.dao().allUsers(), config) .setBoundaryCallback(UserBoundaryCallback(helper)) .setFetchExecutor(executor) .build() fun updateUsers() { // DB ʹอଘ } fun deleteUser(userId: Long) { // DB ͷϢʔβʔΛߋ৽ͭͭ͠ɺ֘౰ API ΋ୟ͘ } Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 14
  10. ·ͱΊ — Paging Library ͱ Room Λ࢖ͬͯɺΞΫγϣϯΛϑοΫʹ ͨ͠Ϧετߋ৽ͷ࣮૷Λͨ͠ — PagedList

    Λ͍͡ΕΕ͹͍͍ͳͱࢥ͏͕ɺPaging Library ࣗମͷ໾ׂΛߟ͑Δͱ͜͏͢Δͷ͕ࣗવʁ — ࠓޙͷΞοϓσʔτʹظ଴ Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 15