Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using the Initialize event in a view model since upgrade to Version 6.0 #2808

Closed
1 of 7 tasks
JimWilcox3 opened this issue Apr 17, 2018 · 8 comments
Closed
1 of 7 tasks

Comments

@JimWilcox3
Copy link
Contributor

https://github.com/JimWilcox3/MvvmCrossInitializeTest

Steps to reproduce 📜

  1. The project will hang up if you run it as is. The View is never displayed.

  2. If you comment out the ColorList = await SomeData.GetColors(); in the Initialize, the View is displayed but the colors are not loaded.

  3. If you un-comment the Task in the constructor, the colors will load and the View will be displayed.

Expected behavior 🤔

You should be able to use an async Task to load data in the Initialize of the View Model.

Actual behavior 🐛

It makes the call to load the data then hangs and never shows the View.

Configuration 🔧

Version: 6.0

Platform:

  • 📱 iOS
  • 🤖 Android
  • 🏁 WPF
  • 🌎 UWP
  • 🍎 MacOS
  • 📺 tvOS
  • 🐒 Xamarin.Forms
@martijn00
Copy link
Contributor

What if you use .ConfigureAwait(false);?

@JimWilcox3
Copy link
Contributor Author

Where exactly would I use this? I do want to await the calls in Initialize. I was thinking this was the whole purpose of having an async Initialize to do the heavy loading. It worked fine under version 5.x. I can use a workaround if that's what it will take.

@martijn00
Copy link
Contributor

A good read on this is: https://medium.com/bynder-tech/c-why-you-should-use-configureawait-false-in-your-library-code-d7837dce3d7f

It's not a workaround, it's what you are supposed to do.

@JimWilcox3
Copy link
Contributor Author

It is puzzling that this app has been working for years and now it has to be configured differently and only in the Initialize of a ViewModel. I had to add the ConfigureAwait(false) in the Initialize and in the call to the data service, but it started working.

If you don't mind me asking, what is it about the Initialize event that makes you have to do this now?

@nickrandolph
Copy link
Contributor

I think this is only true of the first view model. The first navigation is forced to be done synchronously.
If you’re using the splashscreen you can do some initialisation logic before navigating to the first view model which would allow you to complete the losing of that data asynchronously.

@JimWilcox3
Copy link
Contributor Author

@nickrandolph it seems you are correct that it is only on the first View Model. After that, the other View Models work as expected. I am using a SplashScreen. Can you tell me what sort of initialization logic I could do to make the first View Model Initialization be asynchronous?

@nickrandolph
Copy link
Contributor

@JimWilcox3 check out my post https://nicksnettravels.builttoroam.com/post/2018/04/19/MvvmCross-Initialize-method-on-the-first-view-model.aspx
I'm going to close this issue as there are work arounds and this change was by design

@JimWilcox3
Copy link
Contributor Author

@nickrandolph Thanks for posting that. It was helpful and informative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants