fisher
fisher

Reputation: 1296

Executing operations one after the other

I am struggeling with something and I would like to ask you if you could point me in the right direction.

I have four tasks I want to complete, -one after the other.

  1. Fetch html-code from web
  2. Parse this code and save to core data storage
  3. Use this data and batch save to calendar
  4. Upload the parsed data to my own web server.

I have written all the code for this and it executes fine. However, at times it struggles as some of the code is executed before the other has finished.

Example:

func startProcess () {

    fetchHTMLFromWeb()

    parseHTML()

    saveToCalendar()

    //Sometimes uploadToWeb() starts before saveToCalendar() is finished
    uploadToWeb()

}

I have tried reading up on GCD, but it is a rather complex subject and I am finding it hard to grasp it.

Can you recommend any good readups on this subject?

Thank you very much!

Upvotes: 1

Views: 374

Answers (1)

LastMove
LastMove

Reputation: 2482

You can use the GCD to execute all your stuffs in the background queue.

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  self.startProcess();
});

with that, startProcess will start on the background queue/thread. So you can

In the fetchHtmlFromWeb method just call parseHtml(), when the fetch is ended. hope it helps.

Upvotes: 1

Related Questions