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

matching_dev1

2df5079e89d2e51aeb9203b3410ea61b?s=47 Ryo.Nitami
November 14, 2018

 matching_dev1

2df5079e89d2e51aeb9203b3410ea61b?s=128

Ryo.Nitami

November 14, 2018
Tweet

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. Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 5

  6. ΫϩεϛʔͰͷར༻ྫ — Ϧετʹͳ͍ͬͯΔͱ͜Ζ͸͢΂ ͯ Paging Library Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 6

  7. ΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ ৽ — ඇදࣔϢʔβʔϦετͳͲ — ʮϘλϯΛԡ͢ → API Λୟ͘ →

    View Λߋ৽͢Δʯͱ͍͏ྲྀΕ͕ ༷ʑͳ৔໘Ͱൃੜ Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 7
  8. ͸·ͬͨͱ͜Ζ — PagedList ͸ཁૉͷ௥Ճ΍ߋ৽ɺ࡟আ͕Ͱ͖ͳ͍ Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 8

  9. PagedList ͸ཁૉͷ௥Ճ΍ߋ৽ɺ࡟আ͕Ͱ͖ͳ͍ — issue tracker Ͱ͸ʮόά͡Όͳͯ͘ػೳ͕଍Γ͍ͯͳ͍ͩ ͚ͩʂʯͱ͍͏ٞ࿦͕1 1 https://issuetracker.google.com/issues/74916992 Paging

    Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 9
  10. PagedList ͸ MutableList Ͱ͸ͳ͍ʂ — googlesamples ͷ issue Ͱ͸ Room

    ࢖͓ͬͯίϝϯτ͕ ͋Δ2 — ΫϩεϛʔͰ͸͜ͷํ๏ͰରԠ 2 https://github.com/googlesamples/android-architecture-components/issues/281 Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 10
  11. Room ܦ༝ͰϦετදࣔ Paging Library ʹ͓͚ΔΞΫγϣϯΛϑοΫʹͨ͠Ϧετߋ৽ 11

  12. 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
  13. @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
  14. 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
  15. ·ͱΊ — Paging Library ͱ Room Λ࢖ͬͯɺΞΫγϣϯΛϑοΫʹ ͨ͠Ϧετߋ৽ͷ࣮૷Λͨ͠ — PagedList

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