• 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: NSDictionary @synchronize to read multiple objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSDictionary @synchronize to read multiple objects


  • Subject: Re: NSDictionary @synchronize to read multiple objects
  • From: Jens Alfke <email@hidden>
  • Date: Mon, 31 Mar 2014 22:25:56 -0700

On Mar 31, 2014, at 10:16 PM, Trygve Inda <email@hidden> wrote:

> 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.

Don’t expose the dictionary in mutable form. You can’t make that thread-safe unless all the clients voluntarily agree to do something like wrap their usage with @synchronized blocks using the dictionary as a parameter.

For example,

> @synchronize(myDict)
> {
>    myCopy = [[NSDictionary alloc] initWithDictionary:myDict copyItems:YES];
> }

If some other thread mutates myDict during this block, you’ll probably crash or corrupt the heap.

I’d recommend making your property an immutable NSDictionary instead. (I’ve found that exposing mutable collections as properties usually doesn’t work well. Not just because of threading, but because the owning object loses all control over what’s in the collection.)

—Jens
_______________________________________________

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: NSDictionary @synchronize to read multiple objects
      • From: Ken Thomases <email@hidden>
References: 
 >NSDictionary @synchronize to read multiple objects (From: Trygve Inda <email@hidden>)

  • Prev by Date: NSDictionary @synchronize to read multiple objects
  • Next by Date: Re: NSDictionary @synchronize to read multiple objects
  • Previous by thread: NSDictionary @synchronize to read multiple objects
  • Next by thread: Re: NSDictionary @synchronize to read multiple objects
  • Index(es):
    • Date
    • Thread