Group Group Group Group Group Group Group Group Group

iOS Concurrency with GCD and Operations - Part 11: | Ray Wenderlich

Learn how to create thread-safe objects to prevent inconsistent state, and how to avoid other concurrency problems.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3648-ios-concurrency-with-gcd-and-operations/lessons/11

Great course! Download materials for Part 11 seem to be missing.

thanks! sorry I didn’t see this until now, the site has forwarded some comments as emails so I thought it would send all of them.

I’ll get the materials uploaded asap.

Thank you for adding the materials to your excellent course.

Hello audrey It looks fine but If I am using the same mechanism in the Person class It is not giving me the expected result. Please find the code below.

open class Person {
private var firstName: String
private var lastName: String

let queue = DispatchQueue(label: "com.raywenderlich.person.isolation", attributes: .concurrent)

public init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}

open func changeName(firstName: String, lastName: String) {

queue.async(flags: .barrier) {

    randomDelay(maxDuration:  0.2)
    self.firstName = firstName
    randomDelay(maxDuration:  1)
    self.lastName = lastName

}

}

open var name: String {
return queue.sync{
return “(self.firstName) (self.lastName)”
}
}
}

works for me, for example:

Current threadsafe name: Charlie Cheesecake
Current threadsafe name: Delia Dingle
Current threadsafe name: Freddie Frost
Current threadsafe name: Freddie Frost
Current threadsafe name: Gina Gregory
Final threadsafe name: Gina Gregory

As long as the first and last name start with the same letter, it’s working

hi audrey Thanks for the answer but one thing I have observed It is giving expected result only when we added this line. usleep(UInt32(10_000 * idx)) I didn’t get that.can we make class thread safe with out adding this line?

hi Rakesh: that line is in the for-loop to change the names, not in the Person class. It’s there to slow down execution, so the race condition appears. But it’s not actually necessary: I can get the jumbled names without it, because of the randomDelay in the changeName method.

For the thread safe Person, I get the correct results even if I comment out the usleep line.

Hello Mam, when I run the program normally I just got the expected result given below:

Current threadsafe name: Charlie Cheesecake
Current threadsafe name: Delia Dingle
Current threadsafe name: Eva Evershed
Current threadsafe name: Freddie Frost
Current threadsafe name: Gina Gregory
Final threadsafe name: Gina Gregory

but If I am commenting Line usleep(UInt32(10_000 * idx)) the I am getting results given below:
Current threadsafe name: Gina Gregory
Current threadsafe name: Gina Gregory
Current threadsafe name: Gina Gregory
Current threadsafe name: Gina Gregory
Current threadsafe name: Gina Gregory
Final threadsafe name: Gina Gregory

that is something which is confusing me Please help

your computer is so fast, it has already reached the last name change before it started printing, so you only get the last name printed, for each loop iteration.

my MacBook must be slower than yours … it’s a few years old :wink:

Dear audrey,

you said in the video, barrier can also be used for lazy var. I’m not quite clear with it, could you give me an example?

thanks

sorry for the delay!

what I said wasn’t about barrier, but about sync — sorry, should’ve been more clear about that. You would use sync on an internal queue to ensure you initialised a lazy var at most once. Previously, you would use dispatch_once, but that’s gone away now.

Hi, when would you advise making a class thread-safe? Should you always aim for it? or only do it if need be?

hi Zac: I would do it only if necessary, that is, if your app changes objects of that class from concurrent threads. Or if TSan finds a race condition. Here’s an endorsement of TSan from a dev I respect:

Attaching a Swift 5 Xcode 11 version of exercise files for this lecture, could probably help those who don’t have Xcode 10 to be able to run the demo. Did nothing but used the default migration tool, and can verify that the end product works just fine (albeit with 2-3 warnings)
Won’t mind if this is made downloadable directly from the lecture video page -
11_ConcurrencySolutions.zip (1.8 MB)

1 Like

@akashlal Please check out the updated version of the course when you get a chance:

https://www.raywenderlich.com/9461083-ios-concurrency-with-gcd-and-operations

I hope it helps!

1 Like