Code Monkey home page Code Monkey logo

Comments (8)

fcogutierrez avatar fcogutierrez commented on July 19, 2024

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.

Ebsan avatar Ebsan commented on July 19, 2024

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.

oddbear avatar oddbear commented on July 19, 2024

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.

CarahBeck avatar CarahBeck commented on July 19, 2024

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.

rid00z avatar rid00z commented on July 19, 2024

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.

rid00z avatar rid00z commented on July 19, 2024

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.

oddbear avatar oddbear commented on July 19, 2024

@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.

UnreachableCode avatar UnreachableCode commented on July 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.