Skip to content

Instantly share code, notes, and snippets.

@programming086
Last active August 7, 2019 18:13
Show Gist options
  • Save programming086/38217c9b380243a7121eda33c8876333 to your computer and use it in GitHub Desktop.
Save programming086/38217c9b380243a7121eda33c8876333 to your computer and use it in GitHub Desktop.
Incorrect example for Yandex iOS course `Создаёте заметку, копируете ее, меняете поле цвета в одной очереди асинхронно, из другой очереди читаете/пишете цвет из копии заметки`
import Foundation
import UIKit
struct Note {
enum Importance: UInt {
case unimportant = 0, normal, important
}
let uid: String
var title: String
var content: String
var color: UIColor
var importance: Importance
var selfDestructDate: Date?
init(uid: String = UUID().uuidString, title: String, content: String, color: UIColor = .white, importance: Importance, destructDate: Date? = nil) {
self.uid = uid
self.title = title
self.content = content
self.importance = importance
self.color = color
selfDestructDate = destructDate
}
}
var a = Note(title: "Note 1", content: "Text 1", importance: .normal)
var b = a
DispatchQueue.global().async {
a.color = .red
a.title = "Note changed 1"
a.content = "Text changed 1"
a.importance = .important
a.selfDestructDate = Date()
DispatchQueue.main.async {
sleep(2)
print(">> a \(a)")
}
}
DispatchQueue.global(qos: .utility).async {
sleep(3)
print(">> utility: b \(b)")
}
DispatchQueue.global(qos: .background).async {
print(">> background: b \(b)")
}
@programming086
Copy link
Author

programming086 commented Aug 7, 2019

OUTPUT

>> background: b Note(uid: "21F1387A-A815-46D7-AB45-BFF30FCC465E", title: "Note 1", content: "Text 1", color: UIExtendedGrayColorSpace 1 1, importance: __lldb_expr_11.Note.Importance.normal, selfDestructDate: nil)
>> a Note(uid: "21F1387A-A815-46D7-AB45-BFF30FCC465E", title: "Note changed 1", content: "Text changed 1", color: UIExtendedSRGBColorSpace 1 0 0 1, importance: __lldb_expr_11.Note.Importance.important, selfDestructDate: Optional(2019-08-07 18:08:59 +0000))
>> utility: b Note(uid: "21F1387A-A815-46D7-AB45-BFF30FCC465E", title: "Note 1", content: "Text 1", color: UIExtendedGrayColorSpace 1 1, importance: __lldb_expr_11.Note.Importance.normal, selfDestructDate: nil)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment