views:

272

answers:

1

I am uploading a bunch of files from the iPhone to a web service. I want to have a UIAlertView on screen with a UIActivityIndicatorView inside. I got that to work fine. However, I want to update the title of the UIAlertView as each file gets uploaded. ("Uploading file 1...", "Uploading file 2...", etc.)

I know that you can't just set the title in a loop with synchronous web requests as the UI run loop will never get called. I tried using NSTimer to fire off the web requests but since each request duration is unpredictable, that doesn't work. (The message could update before the request is actually finished.)

I really want to upload each file synchronously, one at a time, as the iPhone bandwidth is pretty limited. I just can't figure out a mechanism to say 'once this synchronous operation finishes, let the UI update for a tick, then do another synchronous operation.'

+1  A: 

OK here's the approach I took to solve this:

  1. Create the UIAlertView
  2. Start an asynchronous web request to post the item, set the delegate to self
  3. In the connectionDidFinishLoading: method, check to see if there are more items to post. If there are, update the title and kick off another async request. If not, dismiss the alert.

It means you have to maintain a few class variables to track which request is uploading but it works perfectly and doesn't involve any complicated threading or anything else.

jsd