Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

elzorro

macrumors newbie
Original poster
Nov 8, 2009
6
0
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?
 
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?

This is a complete and utter guess on that front but perhaps it is a singleton. It certainly sounds like it from the description.
 
This is a complete and utter guess on that front but perhaps it is a singleton. It certainly sounds like it from the description.

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

-Lee
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.