Accessing an instance variable from two separate threads at the same time is not safe. You need some kind of synchronization mechanism such as property or @synchronized
block. For example:
@property(strong, readwrite) Widget* widget;
(Note the lack of nonatomic.)
And access the value via the property:
@implementation MyClass
-(void)handleData:(NSData*)data {
//...do a bunch of handleData then...
dispatch_async(dispatch_get_main_queue(), ^{
[self.widget someMethodWithData:data];
});
}
-(void)foo {
//...do a bunch of foo then...
self.widget = nil;
}
@end
Of course, if foo is called asynchronously, your self.widget could return nil. If this is not the behavior you want, your proposed local-variable solution is the way to go.