Search code examples
objective-cclasswatchkitnsthreadwatchos-2

WatchKit thread issue between classes


I have two classes named InterfaceController and LoadInterfaceController.

I'm calling InterfaceController's uiChange function from my LoadInterfaceController:

InterfaceController *interfaceController = [InterfaceController alloc];
//[interfaceController performSelectorOnMainThread:@selector(uiChange) withObject:nil waitUntilDone:true];
[interfaceController uiChange];

The function is called, but the UI in InterfaceController isn't modified.

- (void)uiChange {
    NSLog(@"uiChange was called");
    //... make changes to the UI ...
}

If the function is called from a function originating from InterfaceController class the UI is changed, respectively.

I have tried calling uiChange on the main thread (as explained here), but the UI isn't responding. How may I specify the thread used for InterfaceController's UI?


Solution

  • The same issue as here. Do not initialise controllers on your own, let Watch do it as a user flow happens.

    I'd suggest adding into your architecture a Pub/Sub pattern. The NSNotificationCenter class is a good example that implements one. It allows parts of application to communicate between each other and it is used really often for controllers communication as well.

    Here is a good example of communication between an AppDelegate and controller that I provided answering to another question recently. But if you really need I could adopt it for your example.