Kodeco Forums

Grand Central Dispatch Tutorial for Swift 3: Part 1/2

Get up to speed with concurrency and parallelism in this in-depth two-part tutorial on Grand Central Dispatch.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/740-grand-central-dispatch-tutorial-for-swift-3-part-1-2

Hi Christine,

Great tutorial! Explained very clearly and concise. I have one question: How can I tell the PhotoManager Class is a Singleton? How the singleton is defined? Thanks!

I have a question about this Singleton also. Does it need a private initializer for PhotManager?

Yes you should use a private init purely out of good practice so a unknown programmer cannot instantiate it by accident the wrong way. Also the way this tutorial writes the Singleton is a bit clunky. We tend to write a Singleton like this in Swift “static let shared = PhotoManager()”.

Thanks for the tutorial. Great work!

Why all the closures in the tutorial do not avoid Strong Reference Cycle?

Ex:

DispatchQueue.global(qos: .userInitiated).async { // 1
  let overlayImage = self.faceOverlayImageFromImage(self.image)
  DispatchQueue.main.async { // 2
    self.fadeInNewImage(overlayImage) // 3
  }
}

should be:

DispatchQueue.global(qos: .userInitiated).async { **[weak self]** in // 1
  **guard let strongSelf = self else { return }**
  let overlayImage = **strongSelf**.faceOverlayImageFromImage(self.image)
  DispatchQueue.main.async { // 2
    **strongSelf**.fadeInNewImage(overlayImage) // 3
  }
}

Regards,

2 Likes

@quanguyen Exactly my thoughts too. I am pretty sure we have to add [weak self].

@lwang3rock I’d agree with @override777 here. When the tutorial’s updated, we can make those changes, specifically it would look like this inside of the class definition:

private override init() { }
static let sharedManager = PhotoManager()

Thanks for the catch @quanguyen , I can make the suggested change if I batch in any updates.

I do not see any reference cycle here. Can you explain why there is?

Hi, I still dont get what’s the difference between execute task async with barrier flag and execute task sync as you did with write and read photos, can you help me to understand?

Hi Christine,

Thanks for the great stuff!!.
By going through this tutorial, I assume that DispatchQueue.main is same as DispatchQueue.global(qos: .userInteractive). Because you have mentioned that qos - .userInteractive runs on main thread. Is that so??

I was doing some research on this as it’s an interesting question. I actually don’t know but suspected that they are different. I saw you ask this on StackOverflow, were you able to come to a conclusion?

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]