In ARC, I want an atomic property for multi-thread access:
@interface MyClass
@property (atomic, strong) NSString *myString;
@end
I also want a custom setter and getter for various reasons, like debugging or tracking.
Solution A, I override the setter and getter:
@implementation MyClass
@synthesize myString = _myString;
static int i;
- (void)setMyString:(NSString *)myString
{
_myString = myString;
NSLog(@"%d", i++);
}
- (NSString *)myString
{
return _myString;
}
@end
Solution B, I extend the class interface to have a different synthesized property:
@interface MyClass ()
@property (atomic, strong) NSString *myPrivateString;
@end
@implementation MyClass
@synthesize myPrivateString = _myPrivateString;
static int i;
- (void)setMyString:(NSString *)myString
{
_myPrivateString = myString;
NSLog(@"%d", i++);
}
- (NSString *)myString
{
return _myPrivateString;
}
@end
Is solution A atomic? Is solution B atomic? If none of them are atomic, should I use objc_setProperty and objc_getProperty, or should I use @synchronized(self), or is there a better locking mechanism to avoid locking self?
(sub-question: in case B, getting or setting _myPrivateString
directly is as safe as getting or setting self.myPrivateString
or not as safe?)