Here's atomic/nonatiomic properties:
@property (nonatomic,strong) NSString* firstName;
@property (atomic,strong) NSString* lastName;
With setters/getters:
-(void)setFirstName:(NSString *)fname{
NSLog(@"set self.firstname: %@ ",fname);
self.firstName = [fname uppercaseString];
}
-(NSString *)firstName{
NSLog(@" get self.firstname: %@ ",self.firstName);
return self.firstName;
}
-(void)setLastName:(NSString *)lName{
NSLog(@"set self.laststname: %@ ",lName);
self.lastName = lName.uppercaseString;
}
-(NSString *)lastName{
NSLog(@" get self.lastname: %@ ",self.firstName);
return self.lastName;
}
When trying to modify/acces from asynchronous queued block, it's crashed:
-(void)viewWillAppear:(BOOL)animated
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
self.firstName = @"f1";
self.lastName = @"l1";
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
self.firstName = @"f2";
self.lastName = @"l3";
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
self.firstName = @"f3";
self.lastName = @"l3";
});
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),^{
NSLog(@"first : %@ \t last : %@",self.firstName,self.lastName);
});
}
@end
With the mentioned code, the questions are:
- Why its crashing.
- What the difference between atomic/nonatomic properties when dealing with multiple threads