Inovize
Inovize

Reputation: 233

Perform Method on Main Thread Issue

Hi I am trying to call a method from another view controller and it is working but not being displayed on the main screen. The log shows that the method is working but more likely on a new instance of my controller.

Here is the method in my ViewController Class

//.h First
{
- (void)updateCells;
}

//.m First
- (void)updateCells
{
//Code in here
}

And in the method from another (second) view controller that calls the method:

ViewController *viewController = [[ViewController alloc] init];

    dispatch_async(dispatch_get_main_queue(), ^{
        [viewController updateCells];
    });

I tried the dispatch_async to have it perform the updateCells on the screen but the method is called and nothing inside it is displayed. Should I not create a new viewController instance and if so what should I do instead? Thanks I am new to how calling methods from different classes works.

Upvotes: 0

Views: 78

Answers (1)

rob mayoff
rob mayoff

Reputation: 385500

If you want your neighbor's dog to roll over, do you go to the pound, get a new dog, yell "Roll over!" at it, then shoot it in the head, then go to your neighbor's house and expect to find that her dog is rolling over? Because that's what you're doing here.

If you want some existing view controller to updateCells, you need a reference to that existing view controller so you can send it the updateCells message.

What you're doing instead is creating a new view controller, sending it the updateCells message, and then letting it be deallocated. Why would you expect that to have any effect on an existing view controller?

How “you” (meaning, the method that needs to send the updateCells method) get a reference to the existing view controller depends entirely on your specific app. If you need help figuring out how “you” should get a reference to that existing view controller, edit your question to include details like the actual class names of the view controllers involved (since that makes it much easier to talk about them), and how the existing instances of those classes get created.

Upvotes: 1

Related Questions