Why Initialize method on ViewModels is in the Main Thread? #4859
-
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Good thing you noticed. I noticed it too in some cases. The As you can see there is In my case it happened when internet connection was bad on Android and the UI got frozen because in the implementation of In general, I assume that My solution was simple. And it is the only right solution if you want to initialize on background: public override Task Initialize() {
Task.Run(BackgroundInitialize);
return Task.CompletedTask;
}
protected virtual Task BackgroundInitialize() {
return Task.CompletedTask;
}Here is some good reading about It is still better but it won't save from such a case: So I will repeat that the only right way to initialize is to use Good luck. :) |
Beta Was this translation helpful? Give feedback.
Good thing you noticed. I noticed it too in some cases.
The
Initializetask gets created here: https://github.com/MvvmCross/MvvmCross/blob/release/9.2.0/MvvmCross/ViewModels/MvxDefaultViewModelLocator.cs#L170And then it gets awaited here: https://github.com/MvvmCross/MvvmCross/blob/release/9.2.0/MvvmCross/Navigation/MvxNavigationService.cs#L301
As you can see there is
ConfigureAwait(false)that supposes to switch the context. But it only gets switched on innerawait. And you haveawait CreateFilters()that gets awaited. But if there is not any innerawaitinside yourCreateFiltersor there are some long running operations before anyawaitthen you are still on the main thread. The same gβ¦