iPhone SDK: How to know when background task has completed? -


we trying background task working purpose of including activity indicator in workhouse screen. our understanding, requires 1 create background thread run on. understand no gui updates can performed on background thread.

given that, here general pattern of needs happen.

a.) pre-validate fields. make sure user did not enter invalid data b.) setup background task. c.) process results background task

this looks in code far:

-(ibaction)launchtask:(id)sender  {     //validate fields     [self validatefields];      /* operation queue init (autorelease) */     nsoperationqueue *queue = [nsoperationqueue new];      /* create our nsinvocationoperation call loaddatawithoperation, passing in nil */     nsinvocationoperation *operation = [[nsinvocationoperation alloc] initwithtarget:self                                                                             selector:@selector(backgroundtask)                                                                               object:nil];      /* add operation queue */     [queue addoperation:operation];     [operation release];       //to do: add post processing code here, how know when done???     confirmationviewcontroller *othervc;      //show confirm     //if (ui_user_interface_idiom() == uiuserinterfaceidiompad)     //{     //  othervc = [[confirmationviewcontroller alloc] initwithnibname:@"confirmationviewpad" bundle:nil];     //}     //else     {         othervc = [[confirmationviewcontroller alloc] initwithnibname:@"confirmationview" bundle:nil];     }      //to do: let's put in struct     othervc.strconfirmation = strresponse;     othervc.strcardtype = strcardtype;     othervc.strcardnumber = txtcardnumber.text;     othervc.strexpires = txtexpires.text;     othervc.strcustomeremail = txtemail.text;      [self.navigationcontroller pushviewcontroller:othervc animated:yes];     [othervc release];     othervc = nil;     } 

so far, works pretty except don't yet have way know when background task complete. when complete, can process results of background task. right now, doesn't work because there not synchronization two. how solve?

one other thing, noticed spinner displayed in status bar. thing doesn't seem going away after background task has completed? do?

thanks in advance.

your options are, briefly:

  • key value observe 'operationcount' property on nsoperationqueue , wait reach 0 (or, equivalently, 'operations' property , check count)
  • have operations fire off little notification they're done (probably on main thread performselectoronmainthread:...) , wait until correct number of notifications have been received.

[edit: see you've asked old sdk 3.0. in case, observe operations , check count because operationcount property postdates sdk 3.0]

there's no automatic system starting , stopping spinner in general case. you'll have talk yourself. however, neat thing spinner continues spinning if main thread blocked, if you're thread hopping purpose don't need to.

a spinner appears in status bar show data fetches, believe. if continues spinning still have url requests ongoing, whether or not you're waiting results.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -