Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KVO background threads

If I set up kvo observers on my main thread, but then on a background thread i change the value of the property being observed, does my main thread get interrupted immediately, no matter where it was?

Thanks!

like image 798
Wise Shepherd Avatar asked Feb 06 '12 01:02

Wise Shepherd


2 Answers

"If I set up kvo observers on my main thread..."

It is irrelevant what thread you used to set up a KVO observer. The KVO observer will be called back on the thread where the value was changed.

I can't stress this enough - KVO does not manage thread-safety for you!

The Receptionist Pattern is designed to solve this problem.

https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/ReceptionistPattern/ReceptionistPattern.html

Do not try and simply switch threads using GCD in observeValueForKeyPath:ofObject:change:context: without an intermediary object, or you will likely end up with a race condition against dealloc (see the "Deallocation Problem" - https://developer.apple.com/library/ios/technotes/tn2109/_index.html#//apple_ref/doc/uid/DTS40010274-CH1-SUBSECTION11 )

like image 171
Airsource Ltd Avatar answered Nov 11 '22 14:11

Airsource Ltd


No your main thread will not get interrupted, just like the case of the question you posted 2 minutes ago.

The only way a thread will really be interrupted, is when the program would receive a signal. This generally only happens if things go really wrong. (SIGSEGV, SIGABRT, etc)

like image 38
mvds Avatar answered Nov 11 '22 14:11

mvds