title: firestore, coroutine and flow
body: <p>firebase method is working on worker thread automatically. but I have used coroutine and callbackflow to implement firebase listener code synchronously or get return from the listener.
  below is my code that I explained</p>
  <p><strong>coroutine await with firebase for one shot</strong></p>
  <pre><code>override suspend fun checkNickName(nickName: String): Results&lt;Int&gt; {
  
  
      lateinit var result : Results&lt;Int&gt;
  
      fireStore.collection(&quot;database&quot;)
          .document(&quot;user&quot;)
          .get()
          .addOnCompleteListener { document -&gt;
  
  
              if (document.isSuccessful) {
  
                  val list = document.result.data?.get(&quot;nickNameList&quot;) as List&lt;String&gt;
  
                  if (list.contains(nickName))
                      result = Results.Exist(1)
                  else
                      result = Results.No(0)
  
                  //document.getResult().get(&quot;nickNameList&quot;)
              }
              else {
  
              }
          }.await()
  
      return result
  }
  </code></pre>
  <p><strong>callbackflow with firebase listener</strong></p>
  <pre><code>override fun getOwnUser(): Flow&lt;UserEntity&gt; = callbackFlow{
  
      val document = fireStore.collection(&quot;database/user/userList/&quot;)
          .document(&quot;test!!!!!&quot;)
  
      val subscription = document.addSnapshotListener { snapshot,_ -&gt;
  
          if (snapshot!!.exists()) {
  
              val ownUser = snapshot.toObject&lt;UserEntity&gt;()
  
              if (ownUser != null) {
                  trySend(ownUser)
              }
          }
      }
  
      awaitClose { subscription.remove() }
  
  }
  </code></pre>
  <p>so I really wonder these way is good or bad practice and its reason</p>