Why do some class instances seem to exist beyond their instance?

Discussion in 'Mac Programming' started by elzorro, Feb 9, 2010.

  1. elzorro macrumors newbie

    Nov 8, 2009
    I'm self teaching from books, so please someone help me with this strange concept:

    Why do are some class instances somehow… linked to one another? I can have an "instance" of NSManagedObjectContext in each view controller in an array, but when I manipulate the members of one Context, it changes the other's as well. This wouldn't apply to an instance of NSString that I add to a view controller, so why is it different for a class like NSManagedObjectContext?
  2. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    This is a complete and utter guess on that front but perhaps it is a singleton. It certainly sounds like it from the description.
  3. Sydde macrumors 68020


    Aug 17, 2009
  4. GorillaPaws macrumors 6502a


    Oct 26, 2003
    Richmond, VA
    The behavior does sound like a singleton, but NSManagedObjectContext (MOC) isn't designed to be used this way I don't think. I'm pretty sure that each thread is required to have it's own MOC, which wouldn't make sense if it were designed to be used as a singleton.
  5. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Why not print your different instances with %p to see if you do, in fact, have the same Object in different places? At least it would help narrow it down.

  6. Catfish_Man macrumors 68030


    Sep 13, 2001
    Portland, OR
    My CoreData is a bit fuzzy here, but isn't this just expected behavior? The CD layer is backed by some sort of persistent storage (sqlite, xml, etc...); if you change something, it'll reflect that change in the storage, and let everything else backed by that storage know that it should update to the new state.

    Presumably there's some caching going on for performance, but conceptually the write-notify-update pattern seems logical to me. CFPreferences works the same way if you're writing to other apps prefs.

Share This Page