Comments (8)
I mean, I want to be sure that the usage of the Init method what I am doing is the correct one, or if for example the correct method for this purpose is ViewIsAppearing.
Thanks.
from freshmvvm.
Did you find a good way to do this? Currently I'm just prefixing the Init method with async like so:
public override async void Init(object initData)
{
await AsyncMethod();
}
I'm also in the same boat and don't know if this is the best way to do it.
from freshmvvm.
With async void, no one will wait for it to finish, as it's a "fire-and-forget" way of doing things. Therefor it might not be finished when the view is loaded. You can try putting a "await Task.Delay" before the AsyncMethod and see if this is what you actually want.
To get something similar to a "Task Init", I would probably done the async job before PushPage, and then put a parameter with the result in the init method.
The problem with implementing a Task return type, it must actually return something. Therefor there will therefor be some unnecessary overhead.
However there might be some ways of implementing something like this, because the PushPageModel is itself async.
Today the "Init" happens in the last method (BindingPageModel) of: https://github.com/rid00z/FreshMvvm/blob/master/src/FreshMvvm/FreshPageModelResolver.cs
I guess one way to implement this, could be a interface on the model (or a different baseclass), with a Task Init method you can override. Then check this before calling the BindingPageModel method, and instead calling a similar method that support async.
The result will then be the same. No page is opened before the init is completed.
from freshmvvm.
await Init(data);
This could indeed result in the page load occurring before the data is initialized.
@oddbear has a good suggestion:
"To get something similar to a "Task Init", I would probably done the async job before PushPage, and then put a parameter with the result in the init method."
Option 2
edit
Thanks @oddbear
Updated to remove the deadlock, previously Init was blocking the context thread, waiting for MethodAsync to complete, and MethodAsync was waiting for the context to be free so it could complete.
You'll need to show a loading indicator on the screen while MethodAsync does its work, but this is definitely a better approach.
// Non-blocking
public void Init(object initData)
{
MethodAsync(initData).ContinueWith((tResult) => {
// Process fault
}, TaskContinuationOptions.OnlyOnFaulted);
}
// Just a sample async method.
public async Task MethodAsync(object initData)
{
await Task.Run(() => {
this.SomeMember = initData.ToString();
});
}
from freshmvvm.
We've discussed this before, over here: #24
It seems to get a few people, this work below but I'm wondering if I should actually make the init a Task.
public override void Init (object initData)
{
LongRunningTask ().RunForget ();
}
async Task LongRunningTask()
{
//show loading screen
await Task.Delay (5000);
Contacts = new ObservableCollection<Contact> (_databaseService.GetContacts ());
//stop loading screen
}
from freshmvvm.
There's also this.
public static void RunForget(this Task t)
{
t.ContinueWith((tResult) =>
{
//Console.WriteLine(t.Exception)
//TODO: Log to Xamarin insights
},
TaskContinuationOptions.OnlyOnFaulted);
}
from freshmvvm.
@C0D3Name that code will cause a deadlock for 5 seconds in most cases, if executed on the UI thread (depends on the SynchronizationContext).
@rid00z that code is really nice, and probably solves most problems. I would guess this is the most preferred way for the user to see some loading.
The big question is if the developer (and the user), want:
Scenario A: a loading indicator on the current page, and then immediately open up a new page when it's done.
Scenario B: open up a new page, with a loading indicator/spinner.
Your code would solve the B scenario.
Making the init a Task, could help (but probably most confuse) in the A scenario.
//The A scenario, should be the same result as "Task Init":
public async Task LoadSomePage()
{
//Loading logic:
var result = await SomeAsyncMethod();
await CoreMethods.PushPageModel<ModelName>(result);
//Stop loading logic!
}
A possibility can also be to have both a "Task Init" and "void Init", where the developer can override and use one, or both of them.
from freshmvvm.
My code sets the result of the Async method to a new ObservableCollection as in LongRunningTask
. But I now see no items in my UI since adding RunForget(), even though the ObservableCollection has a valid item in it. Has anyone seen any behaviour like this before?
from freshmvvm.
Related Issues (20)
- CoreMethods PopPageModel() throws exception when page contains CarouselView HOT 1
- using <Path> in XAML throws TinyIoCResolutionException
- ReverseInit not called HOT 1
- XF 5 MasterDetailPage to FlyoutPage HOT 1
- Correct way to handle Button Clicks and updating the model HOT 1
- Hamburger Menu is displayed in the Content Page in Landscape Orientation - Android HOT 1
- Project status and plans for .NET MAUI HOT 10
- Page not loaded properly for PopPageModel using navigation stack in FreshMVVM Xamarin Forms Android HOT 1
- SetBackButtonTextProperty doesn't work HOT 1
- How to intercept software back button press on FreshMVVM PageModel? HOT 3
- v4.0 Release notes?
- SIGABRT: Popped page does not appear on top of current navigation stack, please file a bug. IOS Xamarin HOT 2
- Can not run sample App in VS2019
- Fody Propertychanged on Viewmodels
- PushPageModel does not work after the app ran in the background
- Implement same bevaviour as at PRISM IDestructible HOT 4
- Update view after update bind property in ViewModel
- Cannot resolve IFreshNavigationService from a page loaded from Notifcation tap HOT 1
- FreshMVVM MAUI support
- Issue 'NavGraph cannot be null' with FreshMasterDetailNavigationContainer since MAUI 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from freshmvvm.