• 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: NSUserDefaults & thread safety
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSUserDefaults & thread safety


  • Subject: Re: NSUserDefaults & thread safety
  • From: Derrick Bass <email@hidden>
  • Date: Wed, 25 Jan 2006 06:22:05 -0600

On Jan 25, 2006, at 5:47 AM, Jonathan del Strother wrote:

According to the documentation, NSUserDefaults isn't thread safe. Does this mean that any access to NSUserDefaults outside of the main thread is a really bad idea, or am I safe to restrict write- access to the main thread and let any thread have read-access?

In general "not thread safe" means that it is okay to use from different threads, as long as you make to sure to use some sort of synchronization so only one thread at a time is accessing the resource. For most purposes, all threads are equal; mutithreading issues come not from doing things off the main thread, but from the possibility that multiple threads might try to access the same resource at the same time and mess one another up. That said, there are a few things that are only safe from the main thread, but that should be documented (NSAppleScript and some parts of QuickTime are the two examples that come to mind).

You can often get away with reading from multiple threads without synchronization. But you can never guarantee it will work (because the reading routines might write some data to a cache, for example) unless the documentation explicitly says so. So it always best to use synchronization even if you will only ever be writing. And it is never okay to write from a thread if there could be other threads that are reading (what if they choose to read while something is half- written?) without using synchronization.

I think that enclosing user defaults code in @synchronized ([NSUserDefaults standardUserDefaults]) should work.

Derrick


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: NSUserDefaults & thread safety
      • From: Chris Hanson <email@hidden>
    • Re: NSUserDefaults & thread safety
      • From: "Adam R. Maxwell" <email@hidden>
References: 
 >NSUserDefaults & thread safety (From: Jonathan del Strother <email@hidden>)

  • Prev by Date: NSUserDefaults & thread safety
  • Next by Date: Re: TextView Autoresizing Mask problems
  • Previous by thread: NSUserDefaults & thread safety
  • Next by thread: Re: NSUserDefaults & thread safety
  • Index(es):
    • Date
    • Thread