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

**URL:** <https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794>\
**Category:** 🎓 Courses & Articles\
**Created:** [December 6, 2016, 2:37pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794 "2016-12-06T14:37:58Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [December 6, 2016, 2:37pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/1 "2016-12-06T14:37:58Z")

</div>

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](https://www.raywenderlich.com/3648-ios-concurrency-with-gcd-and-operations/lessons/11)

---

<div class="post-metadata">

**Author:** ![awaitz](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/awaitz/32/81494_2.png) [@awaitz](https://forums.kodeco.com/u/awaitz)\
**Post date:** [December 6, 2016, 9:14pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/2 "2016-12-06T21:14:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [December 18, 2016, 11:03pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/3 "2016-12-18T23:03:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![donwlarson](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/donwlarson/32/81322_2.png) [@donwlarson](https://forums.kodeco.com/u/donwlarson)\
**Post date:** [December 20, 2016, 5:29pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/4 "2016-12-20T17:29:03Z")

</div>

Thank you for adding the materials to your excellent course.

---

<div class="post-metadata">

**Author:** ![lohanrakesh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lohanrakesh/32/81494_2.png) [@lohanrakesh](https://forums.kodeco.com/u/lohanrakesh)\
**Post date:** [January 17, 2017, 2:14am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/5 "2017-01-17T02:14:08Z")

</div>

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)”  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [January 17, 2017, 7:30am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/6 "2017-01-17T07:30:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lohanrakesh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lohanrakesh/32/81494_2.png) [@lohanrakesh](https://forums.kodeco.com/u/lohanrakesh)\
**Post date:** [January 17, 2017, 5:22pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/7 "2017-01-17T17:22:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [January 17, 2017, 10:10pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/8 "2017-01-17T22:10:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lohanrakesh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lohanrakesh/32/81494_2.png) [@lohanrakesh](https://forums.kodeco.com/u/lohanrakesh)\
**Post date:** [January 18, 2017, 3:24pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/9 "2017-01-18T15:24:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [January 19, 2017, 4:03am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/10 "2017-01-19T04:03:17Z")

</div>

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 😉

---

<div class="post-metadata">

**Author:** ![animation](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/animation/32/59787_2.png) [@animation](https://forums.kodeco.com/u/animation)\
**Post date:** [February 8, 2017, 9:56am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/11 "2017-02-08T09:56:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [February 18, 2017, 5:24am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/12 "2017-02-18T05:24:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![notoriouspig](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/notoriouspig/32/79544_2.png) [@notoriouspig](https://forums.kodeco.com/u/notoriouspig)\
**Post date:** [May 18, 2017, 2:18pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/13 "2017-05-18T14:18:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![audrey](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/audrey/32/111607_2.png) [@audrey](https://forums.kodeco.com/u/audrey)\
**Post date:** [May 24, 2017, 7:55am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/14 "2017-05-24T07:55:51Z")

</div>

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:

![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/7/a/7aab0df568a65122ed827d0bb96e1b50381930a3.png)

---

<div class="post-metadata">

**Author:** ![akashlal](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/akashlal/32/103097_2.png) [@akashlal](https://forums.kodeco.com/u/akashlal)\
**Post date:** [April 26, 2020, 4:27pm UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/15 "2020-04-26T16:27:31Z")

</div>

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](https://forums.kodeco.com/uploads/short-url/kxsyiz1JUBUlfp4QUyXLNHU9bRv.zip) (1.8 MB)

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [July 3, 2020, 12:54am UTC](https://forums.kodeco.com/t/ios-concurrency-with-gcd-and-operations-part-11-ray-wenderlich/23794/17 "2020-07-03T00:54:02Z")

</div>

@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](https://www.raywenderlich.com/9461083-ios-concurrency-with-gcd-and-operations)

I hope it helps!
