7

When should I use the nonatomic, retain, readonly and readwrite properties in Objective-C?

For example:

@property(nonatomic, retain) NSObject *myObject;

If I use nonatomic and retain, does this mean that the object will be retained?

Ben Leggiero
  • 25,904
  • 38
  • 161
  • 267
kiran kumar
  • 1,333
  • 4
  • 21
  • 40
  • possible duplicate of [Atomic vs nonatomic properties](http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties) – David Gelhar Mar 02 '11 at 19:14
  • @David I don't think so. This question is more about properties in general. Not just `nonatomic` and `atomic`. –  Mar 02 '11 at 19:16

3 Answers3

10

First off, I wanted to promote the comment from David Gelhar to a full answer. The modifiers atomic and nonatomic have nothing to do with thread safety. See this question for more detail in that space.

The other items you listed can be addressed relatively simply. I'll hit them briefly and point you toward the documentation on property modifiers if you want more.

atomic vs nonatomic primarily ensures that complete values are returned from synthesized getters and that complete values are written by synthesized setters.

readwrite vs readonly determines whether a synthesized property has a synthesized accessor or not (readwrite has a setter and is the default, readonly does not).

assign vs retain vs copy determines how the synthesized accessors interact with the Objective-C memory management scheme. assign is the default and simply performs a variable assignment. retain specifies the new value should be sent -retain on assignment and the old value sent -release. copy specifies the new value should be sent -copy on assignment and the old value sent -release.

Community
  • 1
  • 1
David Schaefgen
  • 800
  • 7
  • 9
  • 1
    Also, care needs to be taken to use the setters and getters of the ivar, and not change the ivar directly, which circumvents the setters and getters. That is, use self.myValue = newValue, not myValue = newValue. – Hack Saw Mar 02 '11 at 19:57
  • Absolutely, regarding the use of `self.myValue = newValue` vs `myValue=newValue`. If you are going to use properties, you need to make sure you use dot syntax or send the messages to self. Accessing the ivar directly would undermine quite a bit. – David Schaefgen Mar 02 '11 at 20:08
2

If you use nonatomic, reading and writing the property will not be threadsafe. At this point I don't think it is something you need to worry about, but nonatomic access can be faster than atomic access which is why it is used here.

If you use retain, writing to the property will cause the outgoing value to be released and the incoming value retained, maintaining proper reference-count based ownership of the value.

fbrereto
  • 34,250
  • 17
  • 118
  • 176
1

nontomic Basically, if you say nonatomic, and you generate the accessors using @synthesize, then if multiple threads try to change/read the property at once, badness can happen. You can get partially-written values or over-released/retained objects, which can easily lead to crashes. (This is potentially a lot faster than an atomic accessor, though.)

atomic is the default behavior. nonatomic is thread safe. readonly Externally the property will be readonly.

readwrite property will have both the accessor, and the setter.

assign (default) — Specifies that the setter uses simple assignment. retain — Specifies that retain should be invoked on the object upon assignment. This attribute is valid only for Objective-C object types. (You cannot specify retain for Core Foundation objects)

copy — Specifies that a copy of the object should be used for assignment. The previous value is sent a release message. The copy is made by invoking the copy method. This attribute is valid only for object types, which must implement the NSCopying protocol.

iHS
  • 4,936
  • 3
  • 29
  • 48
  • 5
    atomic != "thread safe". It just means, well, atomic. Any multi-threaded algorithms that share common data need synchronization to avoid race conditions, even with atomic. – Bogatyr Mar 02 '11 at 19:38
  • 1
    Although, if you don't really care about exact ordering, if you have one writer and multiple readers in different threads, "atomic" is good enough, so in that sense it could be considered "thread safe" – Bogatyr Mar 02 '11 at 20:55
  • 1
    @Bogatyr: Yea..! we cannot say it is not thread safe, it doest "gurantee" thread safety, where we are having multiple setters in multiple threads. Thank you :) – iHS Mar 02 '11 at 21:01