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

potatotips #56 (iOS/Android開発Tips共有会)

shogo.yamada
November 15, 2018
1.1k

potatotips #56 (iOS/Android開発Tips共有会)

shogo.yamada

November 15, 2018
Tweet

Transcript

  1. ϓϩϑΟʔϧ 5XJUUFS ;0;0ςΫϊϩδʔζ ։ൃ෦ ࢁాঘޗ Copyright © ZOZO Technologies, Inc.

    All Right Reserved. !ZTIPHP (JUIVC !ZTIPHP ݸਓͰ͸'MVUUFSʹϋϚ͍ͬͯΔ
  2. Copyright © ZOZO Technologies, Inc. All Right Reserved. ,PUMJO$PSPVUJOFͬͯͳʹʁʁ XJLJQFEJBͰ$PSPVUJOFΛݕࡧ͢Δͱ

    αϒϧʔνϯ͕ΤϯτϦʔ͔ΒϦλʔϯ·ͰΛҰͭͷॲཧ୯Ґͱ͢Δͷʹର ͠ɺίϧʔνϯ͸͍ͬͨΜॲཧΛதஅͨ͠ޙɺଓ͖͔ΒॲཧΛ࠶։Ͱ͖Δɻ ઀಄ࣙDP͸ڠௐΛҙຯ͢Δ͕ɺෳ਺ͷίϧʔνϯ͕தஅɾܧଓʹΑΓڠௐ ಈ࡞Λߦ͏͜ͱʹΑΔɻ
  3. Copyright © ZOZO Technologies, Inc. All Right Reserved. ,PUMJO$PSPVUJOFͬͯͳʹʁʁ "OESPJEͰ͸

    ɾඇಉظॲཧͷ଴ͪ߹Θͤ ɾλΠϜΞ΢τͤ͞Δ ɾ͍ΖΜͳඇಉظॲཧΛҰؾʹࢭΊΔ ͞·͟·ͳඇಉظॲཧΛ؅ཧ͢Δ΋ͷͱߟ͍͍͑ͯ
  4. Copyright © ZOZO Technologies, Inc. All Right Reserved. ,PUMJO$PSPVUJOFͬͯͳʹʁʁ 3Y+BWBͷ୅ସ͑Ͱ͸ͳ͍

    3Y+BWB σʔλϑϩʔΛએݴతʹॻ͘ϦΞΫςΟϒϓϩάϥϛϯά $PSPVUJOF தஅՄೳܭࢉΠϯελϯε
  5. Copyright © ZOZO Technologies, Inc. All Right Reserved. ࢖͍ํ fun

    main() { GlobalScope.launch { delay(1000L) println("World!") } println("Hello,") Thread.sleep(2000L) }
  6. Copyright © ZOZO Technologies, Inc. All Right Reserved. ࢖͍ํ 5JNFPVU

    fun main() = runBlocking { //sampleStart withTimeout(1300L) { repeat(1000) { i -> println("I'm sleeping $i ...") delay(500L) } } //sampleEnd }
  7. ࢖͍ํ ଴ͪ߹Θͤ fun main() = runBlocking<Unit> { //sampleStart val time

    = measureTimeMillis { println("The answer is ${concurrentSum()}") } println("Completed in $time ms") //sampleEnd } suspend fun concurrentSum(): Int = coroutineScope { val one = async { doSomethingUsefulOne() } val two = async { doSomethingUsefulTwo() } one.await() + two.await() } suspend fun doSomethingUsefulOne(): Int { delay(1000L) // pretend we are doing something useful here return 13 } suspend fun doSomethingUsefulTwo(): Int { delay(1000L) // pretend we are doing something useful here, too return 29 }
  8. Copyright © ZOZO Technologies, Inc. All Right Reserved. // retorofit

    implementation 'com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2' // coroutines implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1" implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1" ˞,PUMJO͔ΒͷઃఆͰ͢ ઃఆ "OESPJEͰͲ͏࢖͏ͷ͔
  9. Copyright © ZOZO Technologies, Inc. All Right Reserved. interface MainService

    { companion object { const val baseUrl = “URL" } @GET(“/id“) fun getUser(): Deferred<Response<Main>> } 3FUSPpUͱҰॹʹ࢖͏ "OESPJEͰͲ͏࢖͏ͷ͔
  10. Copyright © ZOZO Technologies, Inc. All Right Reserved. fun getUserInfo()

    = Retrofit.Builder() .baseUrl(MainService.baseUrl) .addConverterFactory(MoshiConverterFactory.create()) .addCallAdapterFactory(CoroutineCallAdapterFactory()) .build().create(MainService::class.java) } 3FUSPpUͱҰॹʹ࢖͏ "OESPJEͰͲ͏࢖͏ͷ͔
  11. Copyright © ZOZO Technologies, Inc. All Right Reserved. "OESPJEͰͲ͏࢖͏ͷ͔ GlobalScope.launch

    { val info = repository.getUserInfo() val userRequest = info.getUser() val userResponse = userRequest.await() if (!userResponse.isSuccessful) { ɹɹɹɹɹɹɹɹɹ// ΤϥʔॲཧΛ࣮ߦ͢Δ return@launch } userModel.set(userResponse.body() ?: return@launch) } 3FUSPpUͱҰॹʹ࢖͏
  12. Copyright © ZOZO Technologies, Inc. All Right Reserved. ·ͱΊ ɾ$PSPVUJOFͱ͸ʮؔ਺ͷ్தͰதஅͤͨ͞Γɺ

    ࠶։ͨ͠Γ͢Δ΍ͭʯ ɾ3Y+BWBͷ୅ସ͑Ͱ͸ͳ͍ ɾ3FUSPGJUͱҰॹʹ࢖͑·͢