0

I am new to objective-c, currently I am writing code that downloads a file from server and then do some operation to the downloaded file. I am confused about dispatch queue and dispatch group.

So I have a function called dowanloadZip, inside this downloadZip function, I am using another function called startHttp to fire the request, but this function is async, I need to wait until the completion block finishes then I can continue checking if the file is complete, do some stuff to it etc. Basically, I need to wait until the completion block finishes then I can continue to do other stuff. My code looks something like this:

-(void)downloadZip:(NSString *url){
  ...
  urlRequest = [reqeustMangaer urlStrig:url]
  [requestManager startHttp:urlReuqest completionBlock:^(resposne){
     //check response here
  }]  
}

Someone told me to use dispatch queue, but some other people said to use dispatch group, and I found some code that actually use both. The code I read was using dispatch global queue first, and then inside the queue it's using a dispatch group enter. So should I create a dispatch queue first, then enter the group inside the completion block? Or just use the dispatch group? (I want it to be on background thread). I am also wondering, If I need to call the downloadZip function somewhere else, what should I do?

Anna
  • 443
  • 9
  • 29
  • 1
    A dispatch group is mostly useful when you have a number of things happening at the same time and need to know when all of them are finished. For a single async request, the normal thing is to just do the work in the completion block. – Phillip Mills Jan 30 '19 at 19:00
  • @PhillipMills I know, but I have a lots of stuff to do after the file is downloaded and I don't want to put that all in the completion block. I want to wait until this task finished and then continue. – Anna Jan 30 '19 at 19:03
  • 1
    If it's easier to maintain, put it in its own method and call that from the completion block. There's really no difference between using the completion block and waiting for completion except waiting blocks a thread, which has no real value and can be a problem. – Phillip Mills Jan 30 '19 at 19:20

0 Answers0