Trần Kim Dự
Trần Kim Dự

Reputation: 6102

Objective-C : access properties outside of main UI Thread

When programming GUI on iOS, properties often declares as nonatomic because we often just access those properties on main UI thread (as on Android).

@interface TKDViewController : UIViewController
    @property (nonatomic, strong) NSMutableArray *tableData; 
@end

So, my question is: if I change the properties to atomic, so I can access outside of main UI thread, right? If I do so, will I meet some problems?

@interface TKDViewController : UIViewController
    @property (atomic, strong) NSMutableArray *tableData; 
@end

thanks :)

Upvotes: 2

Views: 457

Answers (2)

Basheer_CAD
Basheer_CAD

Reputation: 4919

Using atomic with IBOutlet will give you a thread safe setters, and using nonatomic will give you unsafe setters.
So, my question is: if I change the properties to atomic, so I can access outside of main UI thread, right? If I do so, will I meet some problems?
Yes, you will get some problems, unless you call [view setNeedsDisplay] on the main thread (after doing some changes on the background) to update the UI (or avoid accessing it on a background thread). Atomic is just a mutex lock.

Upvotes: 2

blazejmar
blazejmar

Reputation: 1090

Atomic and nonatomic mutators on properties affect read/write behavior. With atomic properties each read/write is synchronized (so only one operation at a time is allowed).

You can access nonatomic properties from background thread and in most cases it won't break anything. You have to be careful with changing properties of UI objects. These changes have to be done on main thread to take effect.

Upvotes: 3

Related Questions