Hi, I'm developing an application for iPhone where the persistence is managed with CoreData. I've introduced a singleton to initialize and manage the NSManagedObjectContext, this is how I've defined the singleton:
static Persistence *singletonPersistence = nil; @implementation Persistence + (void)initSingletonWithDataStoreName:(NSString*)aDataStoreName { [singletonPersistence release]; singletonPersistence = nil; singletonPersistence = [[Persistence alloc] initWithDataStoreName:aDataStoreName]; } + (Persistence*)singleton { return singletonPersistence; } In my NSManagedObjects I've introduced a static constructor that associate the object with the context of the singleton: + (id)persistentFeed { return [NSEntityDescription insertNewObjectForEntityForName:@"Feed" inManagedObjectContext:[Persistence singleton].managedObjectContext]; } I've written some tests with SenTestCase: in setUp a new db is created and in tearDown is removed. In a new test where several managed objects are associated each other I've the following error: Unknown.m:0: error: -[FeedTest testUpdateEntries] : Illegal attempt to establish a relationship 'feed' between objects in different contexts (source = <Entry: 0x411da20> (entity: Entry; id: 0x411daa0 <x-coredata:///Entry/t508D0501-EC29-423E-A87E-E9410DD4ACD721> ; data: { authorUsername = "jean_1971_2"; feed = 0x4118040 <x-coredata:///Feed/t508D0501-EC29-423E-A87E-E9410DD4ACD720>; publishTime = 2010-02-15 19:20:17 +0100; text = "Xxxxx\nxxxxx\nxxxxx\nxxxxx"; }) , destination = <Feed: 0x4118560> (entity: Feed; id: 0x16d0f0 <x-coredata:///Feed/t508D0501-EC29-423E-A87E-E9410DD4ACD717> ; data: { feedId = "tag:twitter.com,2007:Status"; title = "Twitter / jean_1971 with friends"; updateTime = "2010-02-26T10:30:20+00:00"; user = 0x4118d10 <x-coredata:///User/t508D0501-EC29-423E-A87E-E9410DD4ACD718>; })) It seems as the managed objects are built with different context but for each object I've used the context referenced in the singleton. Debugging I've found out that the reference of the static variable that keep the singleton (singletonPersistence) changes in the following manner: 1 - at the start of the test is called the method initSingletonWithDataStoreName and the variable take a value, ex. 0x41001d0 2 - the method singleton is called several times and the static variable always return that value 3 - Suddenly (without a call to initSingletonWithDataStoreName) the value to singleton changes, ex. 0x11ac70, and then is used a different context 4 - at the end of the test the value of the static variabile return the former How is possible this behaviour? I thought a static variable in a module may be changed only in the module. Any suggestion? Thanks. Jean _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com