Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is observeValueForKeyPath always called from the main thread?

Is observeValueForKeyPath always called from the main thread?

I'm logging calls with

-(void) observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    NSLog(@"KVO: isMainThread %d", [NSThread isMainThread]);
    // ...
}

and it seems to be printing 1 every time, but I was unable to find any guarantee of this in the docs. Can anyone confirm this is the case?

like image 632
bcattle Avatar asked Sep 05 '14 22:09

bcattle


People also ask

Can get_persistentdatapath be called from the main thread?

ArgumentException: get_persistentDataPath can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

Can iconifywindow() function be called from main thread?

This function must only be called from the main thread. Possible errors include NotInitializedand PlatformError. IconifyWindow(Window*) This function iconifies (minimizes) the specified window if it was previously restored. If the window is already iconified, this function does nothing.

What is the default priority of main thread in Java?

The default priority of Main thread is 5 and for all remaining user threads priority will be inherited from parent to child. For each program, a Main thread is created by JVM (Java Virtual Machine).

Which function can only be called from the main thread?

This function must only be called from the main thread. Possible errors include NotInitialized. GetMonitorNameRaw(Monitor*) This function returns a human-readable name, encoded as UTF-8, of the specified monitor. The name typically reflects the make and model of the monitor and is not guaranteed to be unique among the connected monitors.


1 Answers

In general, no.

You receive observeValueForKeyPath:ofObject:change:context: on the thread which changed the value. The setter method that changes the value sends the message to all observers after updating the value and before returning.

If you only call the setter on the main thread, then you will only observe the change on the main thread.

like image 194
rob mayoff Avatar answered Oct 11 '22 11:10

rob mayoff