NSDictionary @synchronize to read multiple objects
NSDictionary @synchronize to read multiple objects
- Subject: NSDictionary @synchronize to read multiple objects
- From: Trygve Inda <email@hidden>
- Date: Mon, 31 Mar 2014 22:16:07 -0700
- Thread-topic: NSDictionary @synchronize to read multiple objects
I have an object with a dictionary property defined as:
@property (nonatomic, retain) NSMutableDictionary* myDict;
This gets set when it is received via an AppleEvent from another
application.
Multiple threads in the receiving app need to read (the threads never write,
only read) from the Dictionary like:
[self setValueOne:[[myDict objectForKey:kValueOneKey] intValue]];
[self setValueTwo:[[myDict objectForKey:kValueTwoKey] intValue]];
I need to be able to set a new myDict and not have it happen between the
reading of different values from the dictionary by other threads.
Should this be done best with a @synchronize around a copy:
@synchronize(myDict)
{
myCopy = [[NSDictionary alloc] initWithDictionary:myDict copyItems:YES];
}
[self setValueOne:[[myCopy objectForKey:kValueOneKey] intValue]];
[self setValueTwo:[[myCopy objectForKey:kValueTwoKey] intValue]];
[myCopy release];
This would seem to ensure that valueOne and valueTwo were obtained from the
same instance of a given dictionary and that the dictionary did not change
in between the reading and setting of these two dictionary content objects.
I need to avoid valueOne and valueTwo coming from two different instances of
myDict.
Thanks,
Trygve
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden