Code Monkey home page Code Monkey logo

Comments (12)

rid00z avatar rid00z commented on August 20, 2024 2

@fcogutierrez please take a look at this.
https://gist.github.com/programmation/6cc84cfce8e296596364

I've also done this in the past.
Task.Factory.StartNew(() => MyTask(), TaskCreationOptions.LongRunning);

from freshmvvm.

rid00z avatar rid00z commented on August 20, 2024

Great thanks. I'm just away at the moment but I will look at this asap.

from freshmvvm.

bokmadsen avatar bokmadsen commented on August 20, 2024

Will the @BNoohi branch be merged in, maybe as an expansion of the IPageModelCoreMethods interface to avoid breaking changes?

from freshmvvm.

BNoohi avatar BNoohi commented on August 20, 2024

@rid00z Waiting on your thoughts on this.

from freshmvvm.

rid00z avatar rid00z commented on August 20, 2024

@BNoohi I'm planning on look at this in the next 5 days, along with some other new features for FreshMvvm. :)

from freshmvvm.

rid00z avatar rid00z commented on August 20, 2024

@BNoohi thanks for your work in doing this, I very much appreciate it.

Hi, so I've actually taken a look at this and the side effects of using this isn't worth the pay off. There's a big ripple effect in the whole Framework/App. In addition we don't really want to be waiting at this point, we went to kick off a background task.

We can solve this problem by using the pattern below. What do you think?

    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.

BNoohi avatar BNoohi commented on August 20, 2024

Closed, but open to comment? @rid00z I don't think that pattern actually addresses the issue at hand, which is testing of the method and exception handling of the task.

I provided the code as is for option one, but what about option 2? What were your thoughts on that? It should be similar to what you suggested but allows for MSDN best practices and exception handling.

That is, change the method to an async Task but then the line that calls Init() we call with a Task.Run() allowing it to be a fire and forget. Same as it is now, only it can actually throw if something fails in that initialization.

from freshmvvm.

rid00z avatar rid00z commented on August 20, 2024

Actually that solution addresses all the issues. It's testable because you can test both the init method and the long running task. It works according to MSDN best practices, eg you not doing async void. How do you think it doesn't work according with MSDN practices?

Option 2 just moves the problem, as you need to await Task.Run anyway and if you don't await Task.Run then the logic ordering will not be in sync.

from freshmvvm.

BNoohi avatar BNoohi commented on August 20, 2024

You're right, with my point being with option 2 is to not await the Task when calling Init, they end up being similar.The only thing is with Init itself having a void type and the tasks set to forget, it's not easy testing that the Init method calls and handles the tasks. You're right that the task you're calling is testable, the issue is Init itself calling the tasks.

from freshmvvm.

fcogutierrez avatar fcogutierrez commented on August 20, 2024

Hello, what is the solution here? Because the RunForget method does not exist...

from freshmvvm.

papiermache avatar papiermache commented on August 20, 2024

newb question - so if a pagemodel needs to invoke an async method to get its data, how to do if Init itself is not async ?

from freshmvvm.

Ebsan avatar Ebsan commented on August 20, 2024

@papiermache Try putting the async calls in an overridden FreshBasePageModel.ViewIsAppearing() method.

protected override async void ViewIsAppearing (object sender, EventArgs e)

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.