Re: Thread-safe atomic property for array
Re: Thread-safe atomic property for array
- Subject: Re: Thread-safe atomic property for array
- From: Trygve Inda <email@hidden>
- Date: Fri, 14 Aug 2015 17:14:36 -0700
- Thread-topic: Thread-safe atomic property for array
> On Aug 14, 2015, at 6:59 PM, Trygve Inda <email@hidden> wrote:
>
>> My main thread periodically downloads some data from a website. This is
>> extracted into an NSArray (non-mutable) and placed in a property:
>>
>> @property (atomic, retain) NSArray* myArray;
>
>> NSString* someString = [[hostObject myArray] objectAtIndex:2];
>>
>> How can I do this safely?
>>
>> The problem here is that if myArray is being replaced in the main thread
>> after [hostObject myArray] is called but before objectAtIndex:2 is called,
>> than myArray will disappear out from under the caller.
>
> One of the things that an atomic property's getter does, for an object pointer
> type, is retain the object within the synchronization and autorelease it
> before returning it. So, this is safe against that particular problem. The
> returned array will remain valid within the caller's scope.
>
That's good to hear. I couldn't find that documented anywhere. This seems
like the best solution - and the only place I have ever needed to use an
atomic property.
I can't think of a better way to do this sort of thing other than the Core
Data technique I mentioned in the original post.
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