开发者

iphone Asynch downloading and handling multiple processes

开发者 https://www.devze.com 2023-03-15 20:42 出处:网络
I have a basic architectural question about how to handle asynchronous downloading or really any program that has a chain of events, and each event doesn\'t start until a prior event finishes, however

I have a basic architectural question about how to handle asynchronous downloading or really any program that has a chain of events, and each event doesn't start until a prior event finishes, however long it takes.

If I am going to download data, and then perform an action after the download completes, but in the meanwhile I wish to do something else (like show a progress indicator, a splash screen or something else), would it make sense to design the program like so:

-(void)thisMethodStartsTheDownloading{
       //start the download using NSURLConnection (I'm fine with these details)

       //show a UIView for a splash screen image or do anything else you want to do
 }


 -(void)thisMethodRunsWhenDownloadIsFinished{
        //hide or remove the splash screen (if applicable)
        //do whatever I need to do with the downloaded data
        //this meth开发者_开发百科od does not run on its own unless called when NSURLConnection is done
  }


  - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        //do whatever I need to do with my downloaded data

    [self performSelector:@selector(thisMethodRunsWhenDownloadIsFinished)];
       // or [self thisMethodRunsWhenDownloadIsFinished];
   }

  - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
       //incrementally appendData as it comes in from the download process
          [receivedData appendData:data];
   }

I want to make sure I understand the structure of a program that jumps around between methods as each method completes.

Basically, I presume that when you have a chain of processes, but each part of that chain can't run unless the prior chain is finished, you break up your chain into different methods like I've done here, linking them to each other by calling the next one in the chain when the current one finishes. Is that the ideal/standard way of doing this?


Yes.

A standard and robust way to architect a program is to organize all code as a response to an event. Your terminology ("chain of processes") is unfamiliar to me, but it sounds like you are asking if this is the way to translate a sequence or script (how you want things to go) into an event-based environment.

The truth is, sometimes the data doesn't arrive from the network, or not all of it does, or it takes a long time. So thinking of "download data" as a step in a multistep process hides a lot of problems that a real app needs to deal with.

If your step-by-step process is this:

  1. Show Progress Bar
  2. Start Download
  3. Loop: get data, update progress bar
  4. Hide Progress Bar
  5. Do thing with data

So yes, you translate it to events and reactions:

  • The app finished launching: show progress bar, start a download
  • First data arrived: initialize buffer, set progress bar to zero
  • More data arrived: add to buffer, updated progress bar
  • The connection was broken: display alert (try again, cancel)
  • User tapped try again: start download
  • User tapped cancel: remove progress bar, show something else
  • The download is complete: hide progress bar, do something else

While the first way is easier to follow as a narrative, the second way does a better job of handling problems and edge cases (for example, a HTTP redirect causes a "first data arrived" event to happen twice).


Take a look at https://github.com/jdg/MBProgressHUD.

You have the idea correct. You simple chain each call to the next, similar how work a UINavigationController.

Using MBProgressHUD with something like http://allseeing-i.com/ASIHTTPRequest/ will give you almost all the things you will need for this kind of task.

Take a look in how is done using the standar libs on:

https://github.com/jdg/MBProgressHUD/blob/master/Demo/Classes/HudDemoViewController.m

0

精彩评论

暂无评论...
验证码 换一张
取 消