I have this suspend fun:
suspend fun loadUser(): Result<Boolean> {
return withContext(Dispatchers.IO) {
runCatching {
val result = userLoadHelper.loadUser().getOrNull()
result?.let {
eventEmitter.emit(FHEvent.UserNameLoaded(result.username))
val imagePath = imageLoader.downloadImage(result.avatarUrl, AVATAR_PATH, "avatar.jpg")
imagePath?.let { path ->
val drawableAvatar = Drawable.createFromPath(path)
eventEmitter.emit(FHEvent.UserAvatarLoaded(drawableAvatar))
Result.success(true)
} ?: Result.success(false)
} ?: Result.success(false)
}.getOrElse { exception ->
Result.failure(exception)
}
}
}
The problem is, when I call this line:
eventEmitter.emit(FHEvent.UserNameLoaded(result.username))
I’ve got an exception:
kotlinx.coroutines.flow.internal.ChildCancelledException: Child of the scoped flow was cancelled
And I don’t understand, why? My emitter code:
class EventBus @Inject constructor() : IEventProvider, IEventEmitter {
private val _events = MutableSharedFlow<FHEvent>()
override val events: SharedFlow<FHEvent> = _events.asSharedFlow()
override suspend fun emit(event: FHEvent) {
_events.emit(event)
}
}