Close coroutine actor on ViewModel

It’s necessary close coroutine actor on Android ViewModel that are using viewModelScope to call that actor?

In the document said:

When the inbox channel of the actor is closed it sends a special “close token” to the actor. The actor still processes all the messages that were already sent and then “ for (msg in channel) ” loop terminates and the actor completes.

And

When actor’s parent job is cancelled, then actor’s job becomes cancelled. It means that “ for (msg in channel) ” and other cancellable suspending functions throw CancellationException and actor completes without processing remaining messages.

So, imho, I think it’s not necessary to call close() for the lifecycle aware purpose.

1 Like