• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How to implement readonly property
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to implement readonly property


  • Subject: Re: How to implement readonly property
  • From: Tom Davie <email@hidden>
  • Date: Mon, 12 Nov 2012 13:06:22 +0000

On 12 Nov 2012, at 12:56, "Gerriet M. Denkmann" <email@hidden> wrote:

> I have a property:
>
> @property (readonly)  NSDictionary *someDictionary;
>
> This property should be computed on demand, and should be accessible by several threads.
>
> My current implementation is:
>
> - (NSDictionary *)someDictionary;
> {
> 	static NSDictionary *someDictionary;
> 	static dispatch_once_t justOnce;
> 	dispatch_once( &justOnce, ^
> 		{
> 			// create a temp dictionary (might take some time)
> 			someDictionary = temp;
> 		}
> 	);
>
> 	return someDictionary;
> }
>
> The first thread which needs someDictionary will trigger its creation. Ok.
>
> But what happens when another thread wants to access someDictionary while it is still being created? I guess it will receive just nil.
> This would be not correct; it really should wait until the dictionary is ready.
>
> How to achieve this? Use a lock? Use @synchronize?

This is completely the wrong way to implement a property.  The static variable will be shared between all instances.  Here's how you should be doing a lazy loaded var:

@implementation MyClass
{
    NSDictionary *_someDictionary
}

- (NSDictionary *)someDictionary
{
    static dispatch_once_t justOnce;
    dispatch_once(&justOnce, ^
        {
            someDictionary = [[NSDictionary alloc] initWithObjectsAndKeys: …… nil];
        });
    return someDictionary;
}

In answer to your threading question.  If multiple threads ask for the dictionary at once, the second one to hit the dispatch_once will block until the first one has finished the dispatch_once block, and then continue to execute (without touching the contents).  Thus, both threads will receive the same dictionary (assuming it's the same instance it's called on), and it will be allocated only once.

Tom Davie
_______________________________________________

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


  • Follow-Ups:
    • Re: How to implement readonly property
      • From: Marco Tabini <email@hidden>
References: 
 >How to implement readonly property (From: "Gerriet M. Denkmann" <email@hidden>)

  • Prev by Date: Re: How to implement readonly property
  • Next by Date: Re: How to implement readonly property
  • Previous by thread: Re: How to implement readonly property
  • Next by thread: Re: How to implement readonly property
  • Index(es):
    • Date
    • Thread