What thread is running the Swift hasSet {} property?

I defined the property in Swift as:

var foo : String {
  didSet {
    doSomething()
  }
}

Therefore, every time I set foohow foo = "bar", mine didSetwill be executed.

However, I cannot find where the thread is running didSet .

For example, what happens in this case:

dispatch_async(some_queue, { () -> Void in
     self.foo = "bar"
})

This will lead to code execution didSet. Will it run didSeton some_queueor always on main_queue?

+4
source share
1 answer

Property observers willSetand are didSetdirectly called from the setter method, immediately before / after saving the new value, so all this is done in one thread.

, didSet backtrace, :

* thread #2: tid = 0x4df3, 0x000000010024f9eb swtest2`swtest2.foo.didset : Swift.String(oldValue="foo") + 27 at main.swift:6, queue = 'myQueue', stop reason = breakpoint 1.1
  * frame #0: 0x000000010024f9eb swtest2`swtest2.foo.didset : Swift.String(oldValue="foo") + 27 at main.swift:6
    frame #1: 0x000000010024fb91 swtest2`swtest2.foo.setter : Swift.String(newValue="bar") + 177 at main.swift:4
    frame #2: 0x000000010024fe3a swtest2`swtest2.(closure #1) + 42 at main.swift:13
    ...
+8

Source: https://habr.com/ru/post/1618255/


All Articles