title: Inserting in local DB using Room doesn't work - Android
body: <p>When I fetch data from API (which works), I want to insert it into DB, which for some reason it doesn't work. I'm using Room library. (With Koin library for DI)</p>
  <p>Here is my code:</p>
  <p>Service for fetching data from API:</p>
  <pre><code>class PlayerServiceImplFb : PlayerService {
  
  override fun fetchAllPlayers(): Observable&lt;List&lt;PlayerFb&gt;&gt; {
      return Observable.create&lt;List&lt;PlayerFb&gt;&gt; {
          val ref = FirebaseDatabase.getInstance().getReference(&quot;/players&quot;)
          val matchList : MutableList&lt;PlayerFb&gt; = mutableListOf()
          ref.addListenerForSingleValueEvent(object: ValueEventListener {
              override fun onCancelled(error: DatabaseError) {
                  TODO(&quot;Not yet implemented&quot;)
              }
  
              override fun onDataChange(snapshot: DataSnapshot) {
                  val children = snapshot.children
                  children.forEach {
                      Log.d(&quot;PlayerServiceImpl&quot;, &quot;Added player ${it.getValue(PlayerFb::class.java)} to list&quot;)
                      matchList.add(it.getValue(PlayerFb::class.java)!!)
                  }
                  it.onNext(matchList)
                  it.onComplete()
              }
          })
      }
  }
  </code></pre>
  <p>}</p>
  <p>DAO:</p>
  <pre><code>@Dao
  abstract class PlayerDao {
  
  //DELETE ALL AND INSERT PLAYERS
  @Transaction
  open fun deleteAndInsertAll(entities: List&lt;PlayerEntity&gt;) {
      deleteAll()
      insertAll(entities).blockingAwait()
  }
  
  @Insert( onConflict = OnConflictStrategy.REPLACE )
  abstract fun insertAll(entities: List&lt;PlayerEntity&gt;): Completable
  
  @Query(&quot;DELETE FROM players&quot;)
  abstract fun deleteAll()
  
  //GET ALL PLAYERS
  @Query(&quot;SELECT * FROM players&quot;)
  abstract fun getAll(): Observable&lt;List&lt;PlayerEntity&gt;&gt;
  }
  </code></pre>
  <p>Repository (uses service and dao above via Koin DI):</p>
  <pre><code>class PlayerRepoImpl (
  private val remoteDataSource: PlayerService,
  private val localDataSource: PlayerDao
  ): PlayerRepo {
  
  override fun fetchAllPlayers(): Observable&lt;List&lt;PlayerUI&gt;&gt; {
      return remoteDataSource
          .fetchAllPlayers()
          .doOnNext {
              it.forEach {
                  Log.d(&quot;PlayerRepoImpl&quot;, it.username)
              }
              Log.d(&quot;PlayerRepoImpl&quot;,&quot;Pokusaj upisa u bazu&quot;)
              val entities = it.map {
                  PlayerEntity (
                      it.id,
                      it.username,
                      it.email,
                      it.profileImage,
                      it.universes
                  )
              }
  localDataSource.deleteAndInsertAll(entities)
          }
          .map {
              //koristi Resource ovde
              it.map {
                  PlayerUI(it.id, it.username, it.email, it.profileImage, it.universes)
              }
          }
  }
  }
  </code></pre>
  <p>ViewModel layer:</p>
  <pre><code>class PlayerViewModel (
  private val playerRepo: PlayerRepo
  ) : ViewModel(), PlayerContract.ViewModel {
  
  private val subscriptions = CompositeDisposable()
  
  override val playerStates: MutableLiveData&lt;PlayerState&gt; = MutableLiveData()
  
  override fun fetchAllPlayers() {
      val subscription = playerRepo
          .fetchAllPlayers()
          .subscribeOn(Schedulers.io())
          .observeOn(AndroidSchedulers.mainThread())
          .subscribe(
              {
                  Log.d(&quot;PlayerViewModel&quot;, &quot;Fetched all players from firebase&quot;)
                  it.forEach {
                      Log.d(&quot;PlayerViewModel&quot;, it.username)
                  }
                  playerStates.value = PlayerState.Success(it)
              },
              {
                  Log.d(&quot;PlayerViewModel&quot;, &quot;error&quot;)
              }
          )
      subscriptions.add(subscription)
  }
  }
  </code></pre>
  <p>Everytime I get second error lambda: &quot;PlayerViewModel&quot;, &quot;error&quot; printed out. But if I comment out
  localDataSource.deleteAndInsertAll(entities) in Repository, it works fine.
  Lost so much time on this... Im desperate. Thank you.</p>
  <p>Database model:</p>
  <pre><code>@Entity(tableName = &quot;players&quot;)
  data class PlayerEntity (
      @PrimaryKey val id: String,
      val username: String,
      val email: String,
      val profileImage: String,
      val universes: List&lt;String&gt;
  )
  </code></pre>
  <p>API response model:</p>
  <pre><code>data class PlayerFb (
      val id: String, //kopka me da li mi treba
      val username: String,
      val email: String, //kopka me da li mi treba
      val profileImage: String,
      val universes: List&lt;String&gt;
  ) {
      constructor() : this(&quot;&quot;, &quot;&quot;, &quot;&quot;, &quot;&quot;, listOf())
  }
  </code></pre>