On Apr 17, 2008, at 5:36 PM, Chris Hanson wrote: I'd question why you're writing a synchronized accessor in the first place. Accessors are almost always the least correct place to introduce locking; you almost always want at least object-level granularity to your synchronization, if not object graph (or subgraph)-level locking. The reason for this is that accessor-level locking does nothing to enforce multiple-property invariants for your classes.
Consider a Person that has givenName, familyName, and fullName properties; the fullName is synthesized by returning the givenName and familyName. If one thread changes the givenName while another changes the familyName, and the properties are locked individually instead of against the whole object, then you can have a situation where fullName will return an inconsistent value.
This is why in Core Data, locking is implemented by having NSManagedObjectContext conform to NSLocking: It locks at the level of an object graph, rather than at the level of a particular object or one specific property of an individual object.
Chris:
Thanks for the response. I'm using @synthesize for most of my properties, but I have two properties that I have to handle dynamically. I was trying to basically replicate the behavior of creating a synthesized atomic property, but when I typed it up in e-mail, I overtyped "self" with "foo" accidentally (sorry, was moving too fast) - I'm actually using "self" in my real code.
I'm not sure I understand your first statement though . Aren't synthesized accessors atomic by default, and aren't atomic accessors synchronized in a similar manner? I'm currently at an early stage in development on this particular application - primarily writing model objects and the corresponding unit tests, and haven't spawned any threads yet. I would expect that I will do other synchronization later but, as I said, I'm just trying to replicate what I thought the behavior of the synthesized accessors was. Am I wrong in my understanding of how @synthesize works when nonatomic is not specified?
I really appreciate your help. Jeff
|