Very similar issue is already discussed here. The problem at hand and what I am trying to achieve is to call a function on a given object in the thread it is created at. Here is the complete case:
- an instance of class A is created in a given
NSThread
(Thread A) (not the main one). The instance keeps its creatingNSThread
as a member variable. an instance of class B has one of its member functions executing in another
NSThread
- Thread B, and wants to call a function of A in A's creation thread. Thus B's currently executing function issues the following call:[_a performSelector: @(fun) onThread: _a.creationThread withObject: nil waitUntilDone: NO];
If the creation thread of A's instance is not the main one, fun
never gets called. If the creation thread is the main one it is always called. First I was thinking whether the thread that created A's instance has been destroyed and the pointer points to an invalid thread but actually calling any functions on the thread object (Thread A) produces valid results and no crashes. Also checking the object is valid according to this check. Any suggestions?
Update:
What I'm doing is creating a timer on a background thread:
_timer = [NSTimer timerWithTimeInterval:60.0 target:self selector:@selector(fun:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
This code is the one starting the timer. The timer is not started in any specific background thread. It just happens that the function that creates the timer could be called in any thread. Thus the timer should be invalidated in the exactly same one as the NSTimer documentation states: "you should always call the invalidate method from the same thread on which the timer was installed."