Android Paging Library と Retrofit

例えば、このパターン、Network only。

ここでは、PositionalDataSource を拡張するが、他のタイプのDataSource拡張でも同じ。

compositeDisposable のように viewModelScope をはるばる持ってきたにもかかわらず、


class RemoteDataSource(
  private val coroutineScope: CoroutineScope,
  private val service: RemoteService,
  private val s: String
) : PositionalDataSource<Item>() {

  @ExperimentalCoroutinesApi
  override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<Item>) {
    Timber.d("RemoteDataSource#loadInitial: ${Thread.currentThread().name}")
    ...

loadInitial() 内は、メインスレッドではなく、別スレッドで実行されている。


Timber.d("RemoteDataSource#loadInitial: ${Thread.currentThread().name}")


D/RemoteDataSource: RemoteDataSource#loadInitial: arch_disk_io_0
D/RemoteDataSource: RemoteDataSource#loadInitial: arch_disk_io_1
D/RemoteDataSource: RemoteDataSource#loadInitial: arch_disk_io_3

これらは、Android Architecture Component が作成した独自スレッド。

よって、同期なRetrofitの実行処理で良い、となる。

If I use enqueue with Retrofit 2.3 it will doesn't work but if i do a .execute() the LiveData is correctly triggered

Retrofit 2.3で、enqueue() を使用しても機能しませんが、execute() を実行するとLiveDataが正しくトリガーされます。

Android Paging Library LiveData> is triggered before the end of the api call

公式リファレンスにも記述はあるという。

To display data from a backend server, use the synchronous version of the Retrofit API to load information into your own custom DataSource object.

バックエンドサーバーからのデータを表示するには、同期バージョンのRetrofit APIを使用して、独自のカスタムDataSourceオブジェクトに情報をロードします。

Network only - Paging library overview

しかし、DiffUtilを使ったリフレッシュなアニメーションが実行されない。

「手間がかかる」でなく「沼にハマる」ことが最近は多くなった。

APIの仕様がおせっかいすぎやしないか。

しきいを下げようとして、余計に混乱させるばかり。

👉 あなたは Android Architecture Component をどう思いますか? 
👉 Fragment と Toolbar の歴史の話 - Qiita 


Kotlin で FizzBuzz の適当な記述

くだらない企業面接で今だに出題されてホワイトボードに記述させられるという。

👉 Fizz Buzz - Wikipedia 

ゴルフほど可視性を無視しないとしたら、ここらが適当なところと思う。





👉 Kotlin Playground: Edit, Run, Share Kotlin Code Online 

紙にアルファベットで書かせる企業もあるという。

👉 Kotlin で FizzBuzz 


【Kotlin】Properties and Fields: Getters, Setters

We clarified comparing basic Kotlin classes with Java classes about properties and fields.

Property


// Kotlin
class Human {
  val age = 20
}


// Decompiled to Java
public final class Human {
   private final int age = 20;

   public final int getAge() {
      return this.age;
   }
}


// Kotlin
class Human {
  var age = 20
}


// Decompiled to Java
public final class Human {
   private int age = 20;

   public final int getAge() {
      return this.age;
   }

   public final void setAge(int var1) {
      this.age = var1;
   }
}

Backing field


// Kotlin
class Human {
  var age = 20
    set(value) {
      field = value
    }
}


// Decompiled to Java
public final class Human {
   private int age = 20;

   public final int getAge() {
      return this.age;
   }

   public final void setAge(int value) {
      this.age = value;
   }
}

Backing property


// Kotlin
class Human {
  private var _age: Int = 20
  val age: Int
    get() {
      return _age
    }

  fun setAge(value: Int) {
    _age = value
  }
}


// Decompiled to Java
public final class Human {
   private int _age = 20;

   public final int getAge() {
      return this._age;
   }

   public final void setAge(int value) {
      this._age = value;
   }
}

Data class


// Kotlin
data class Human(val age: Int = 20)


// Decompiled to Java
public final class Human {
   private final int age;

   public final int getAge() {
      return this.age;
   }

   public Human(int age) {
      this.age = age;
   }

   // $FF: synthetic method
   public Human(int var1, int var2, DefaultConstructorMarker var3) {
      if ((var2 & 1) != 0) {
         var1 = 20;
      }

      this(var1);
   }

   public Human() {
      this(0, 1, (DefaultConstructorMarker)null);
   }

   public final int component1() {
      return this.age;
   }

   @NotNull
   public final Human copy(int age) {
      return new Human(age);
   }

   // $FF: synthetic method
   @NotNull
   public static Human copy$default(Human var0, int var1, int var2, Object var3) {
      if ((var2 & 1) != 0) {
         var1 = var0.age;
      }

      return var0.copy(var1);
   }

   @NotNull
   public String toString() {
      return "Human(age=" + this.age + ")";
   }

   public int hashCode() {
      return this.age;
   }

   public boolean equals(@Nullable Object var1) {
      if (this != var1) {
         if (var1 instanceof Human) {
            Human var2 = (Human)var1;
            if (this.age == var2.age) {
               return true;
            }
         }

         return false;
      } else {
         return true;
      }
   }
}

Below you can see how to decompile your class on Android Studio!!


[Tools]

  |

[Kotlin]

  |

[Show Kotlin Bytecode]

  |

[Decompile]



👉【Kotlin】バッキング・フィールド/プロパティ
👉 Properties and Fields: Getters, Setters, const, lateinit - Kotlin Programming Language