Joe Dixon
Joe Dixon

Reputation: 1597

MonoTouch FinishedLaunching Method App Gets Killed

My application gets killed due to on FinishedLaunching method is the heavy lifting of my app, my question is how can populate my application of data that comes from a web service without user interaction, i mean the data must come from the WS to the device so the application is usable.

Is there any way to create a new thread or a backgroundworker so FinishedLaunching can return, but the iphone on the background still be contacting the web service and downloading the needed data??

an example would be very useful.

Upvotes: 2

Views: 1018

Answers (2)

patridge
patridge

Reputation: 26565

In a similar vein to @Dimitris' answer, though I am addicted to the syntactic sugar you get this way, you can spin off a lambda with the (Task Parallel Library)[http://msdn.microsoft.com/en-us/library/dd460717.aspx]. Tasks also have all sorts of fantastic continuation, context, and error handling opportunities.

using System.Threading.Tasks;

Task.Factory.StartNew(() => {
    FetchDataFromWS();
});

I had similar code in my Main.cs FinishedLoading method. When I hit a breakpoint in a debug run and then got side-tracked and ran off to lunch, it was still waiting on me when I got back because FinishedLoading had long since returned. Fair warning: I haven't done anything real-world with this yet.

Upvotes: 1

Dimitris Tavlikos
Dimitris Tavlikos

Reputation: 8170

You can use C# asynchronous invocation, works in MonoTouch the way it should:

new Action(this.FetchDataFromWS).BeginInvoke(null, null);

But the best practice would be to get the data from the web service when your main view loads, not in FinishedLaunching.

Upvotes: 5

Related Questions