user2964075
user2964075

Reputation: 33

iOS update UI from another class/thread

I have a main "class"/view controller called "MainUI.m" that displays the UI for my app. Inside this view controller, i declared an instance of another class called "DoActivity.m" so I can call one of its methods called "getFeed" inside my MainUI. Before the method ends, it must update an image from my MainUI. And then the method repeats again. My problem is that the app crashes whenever I update the UI inside the method:

MainUI* mu = [[MainUI alloc] init];   

-(void)getFeed{
//some tasks here

mu.imageView.image= imageInDoActivity.image;
}

EDIT

The error says something like

Thread 1: Program recieved signal:'EXC_BAD_ACCESS' 

and is highlighted on this line of code:

mu.imageView.image= imageInDoActivity.image;

edit

I have a MainUI property in DoActivity:

@property MainUI* mu;

But in my DoActivity constructor, i did not initialise the said property. Instead, before calling [self.doAct getFeed] in MainUI where doAct is

@property DoActivity* doAct;

I set

self.doAct.mu = self;

in MainMU so that DoActivity would know that it should update the UI of the class that called it.

How do I fix this?

Upvotes: 0

Views: 1237

Answers (1)

IluTov
IluTov

Reputation: 6862

You need to do UI stuff on the main thread:

-(void)getFeed{
    //some tasks here

    dispatch_async(dispatch_get_main_queue(), ^{
        mu.imageView.image= imageInDoActivity.image;
    });
}

I'm not sure though if this is really the cause of your crash. A crash-log would be really helpful.

Upvotes: 2

Related Questions