- The background task should operate transparently. It would only alert the user if the operation timed out,
- The user had to wait for the operation to complete, but user is given the option to cancel the background operation.
The Behaviors
First, let's discuss the desired behaviors in a little more detail. Behavior #1 listed above is for tasks that are low priority and don't require the user to wait for it to complete. The task is kicked off in the background, and the main thread retains control for the user to do whatever they need. They don't even know there is something going on in the background (except for the spinning network access icon in the status bar). The only feedback the user may get is if the operation timed out, meaning it failed. Each task created is given a timeout value. If the operation is not complete by this timeout period, then an alert message pops up that displays something like this:Instantiating
Since the BackgroundTask class supports two different behaviors, it also supports two different initializers. This first one initializes the object for behavior #1, where the task runs the background without any user interaction:BackgroundTask* task = [[BackgroundTask alloc] initWithTarget:self
selector:@selector(executeEmailPage:)
argument:myparent.page
timeout:40.0
alertMsg:@"Attempt to email page timed out"];
The target/selector arguments specify the method to run in the new thread. The argument is optional data you can pass to the above method. Timeout specifies how long to give that thread before putting up an alert. And the final argument is the alert message to put up when the operation times out.
This next initializer is for behavior #2, where the user can cancel the operation:
BackgroundTask* task = [[BackgroundTask alloc]
initWithTarget:self
selector:@selector(backgroundRefresh)
argument:nil
waitBeforeAlert:0.5
title:@"Refreshing data"
msg:@"Standby while refreshing pages"];
The target, selector, and argument parameters are the same as the first initializer. The waitBeforeAlert parameter specifies how long to give the operation to complete before putting up the modal box. The title and msg parameters allow you to configure the text displayed in the modal dialog.
Usage
To use the BackgroundTask object after you create it is very simple:// start the background thread
[task start];
// release it since we don't need it anymore
[task release];
the link to the zip file is broken - any other link that I can use?
ReplyDeleteGreat sharing!
The link in the article works for me. Also try http://sites.google.com/site/osmorphis/BackgroundTask.zip?attredirects=0&d=1
ReplyDelete