Possible Duplicate:
Atomic vs nonatomic properties
I just want to know what is the differneve between theses two lines of code :
@property(nonatomic, retain) NSString *str;
and
@property(atomic, retain) NSString *str;
Thanx, Regards, tek3
A Boolean value that indicates whether the entire operation fails when CloudKit can't update one or more records in a record zone.
not atomic, not relating to or containing atoms. 2. logic. relating to a proposition that has a logical constant.
Swift has no such specifier. In Objective-C the implementation of an atomic property allows properties to be safely read and written from different threads. For nonatomic properties, the underlying pointer of a read value could be released when a new value is being written at the same time.
Non atomic properties has no guarantee regarding the returned value . It can be the correct value, a partially written value or even some garbage value. As most things that are not safe — this comes with enhanced speed of accessing this properties.
Atomic properties are necessary in a reference counted multi threaded environment in order to stop objects from disappearing before a thread has a chance to retain them.
Consider the naive implementation of a get accessor:
@interface MyObject : NSObject
{
id myPropertyIVar;
}
-(id) myProperty;
@end
@implementation MyObject
-(id) myProperty
{
return myPropertyIvar;
}
// other stuff
@end
This is all fine except that if you release the instance of MyObject before retaining the returned value from -myProperty the returned value may well be deallocated. For this reason, it is safer to implement -myProperty like this:
-(id) myProperty
{
return [[myPropertyIvar retain] autorelease];
}
This is now completely safe in a single threaded environment.
Unfortunately, in a multithreaded environment there is a race condition. If the thread is interrupted at any time before the retain has incremented the retain count, either of the following will cause you to receive a garbage pointer:
For this reason, all accesses to the property must be protected by a lock. The get accessor looks something like this.
-(id) myProperty
{
// lock
return [[myPropertyIvar retain] autorelease];
// unlock
}
The set accessor is similarly protected and so is the release in -dealloc
The Apple docs explain all this very well. To learn about properties, including their atomicity, read this page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With