Android Studio/kotlin

Suspend & CoroutinScope

kangchaewon 2023. 9. 7. 18:54
    suspend fun getImageUrl(userEmail: String?): String? {
        return try {
            val querySnapshot = MyApplication.db.collection("users")
                .whereEqualTo("userEmail", userEmail)
                .get().await()

            if (!querySnapshot.isEmpty) {
                val userDocument = querySnapshot.documents[0]
                userDocument.getString("imageUrl")
            } else {
                null
            }
        } catch (exception: Exception) {
            Log.e("MyFeedAdapter", "Error getting user document: $exception")
            null
        }
    }

}
CoroutineScope(Dispatchers.Main).launch {
    imageUrl =  MyApplication.getImageUrl(MyApplication.email).toString()
    imageView = binding.userProfile
    if( imageUrl != null){
        Glide.with(requireContext())
            .load(imageUrl)
            .into(binding.userProfile)
    }
}

CoroutineScope로 묶어서 비동기로 처리 => 업데이트 시 이미지가 업데이트 된 프로필 이미지로 설정 가능