Imagine that I have a subview of some UIKit objects (for example, UIActivityIndicatorView
Does not matter). There is also a selector in this scene, which is called doSomething
, which in some way manages the UIKit object (in our example this indicator can start or stop the view).
I NSInvocationOperation
(from the code part of the view) initWithTarget: self selector: @selector (doSomething) with the object: zero
. Then add it to NSOperationQueue
. And all works fine.
How? This should be a new thread and a non-thread-protected UIKit object! Why was there no error (and no crashes)?
NSInvocationOperation class A
NSOperation
Subclass is
In a non-concurrent operation, such that, the operation object does not create a separate thread to run the task. Thus, when the start
method of a non-contemporary operation is called, the operation is executed immediately in the current thread. As long as someone gives control of the start
method of the object, the task is completed.
However, this behavior changes by using NSOperationQueue
. NSOperationQueue always executes operations simultaneously; To perform a non-concurrent operation simultaneously, a separate thread is required, and NSOperationQueue
provides this thread.
This means that if you execute your NSInvocationOperation
directly, you will be able to access your UIKit object thread-safe (operation will execute the same thread). In your case, if you are using NSOperationQueue
, you should schedule work that uses the UIKit object on the main thread, using NSObject from your invoice selector.
Comments
Post a Comment