views:

135

answers:

1

Hi,

I have created model which you can see there: http://i.imagehost.org/0836/2009-11-08%5F14%5F37%5F41.png

I want to store information about sound categories and some sample sounds for each category. Category has Name (NSString) and SoundsRelation (NSSet of NSData, which represents sounds).

Here is the problem: For example I have some category which contains several sounds associated with it. Assume number of sounds is 3. So if I do

NSLog(@"description: \n%@", category);

I will see information about Name and these three sounds. Something like this:

Name = "Cat1";
SoundsRelation =     (
    0x174e90 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p9>,
    0x174ea0 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p10>,
    0x174eb0 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p11>
);

Then I want to clear this category of sounds. I want to set SoundsRelation to nil.

I do:

[category setValue:nil forKeyPath:@"SoundsRelation"];

Now if I do

NSLog(@"description: \n%@", category);

I will have something like:

Name = "Cat1";
SoundsRelation =     (
);

Well, it seems that Cat1 doesn't have sounds associated with it.

Now I save my managedObjectContext using [managedObjectContext save:] method and QUIT APP.

When I relaund my app and do

NSLog(@"description: \n%@", category);

I will have:

Name = "Cat1";
SoundsRelation =     (
    0x174e90 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p9>,
    0x174ea0 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p10>,
    0x174eb0 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p11>
);

I see my previous sounds!

Now, if I override SoundsRelation with some other NSSet which contains 5 OTHER sounds: [category setValue:otherSetWithFiveSounds forKeyPath:@"SoundsRelation"];

And do: NSLog(@"description: \n%@", category);

I see: Name = "Cat1"; SoundsRelation = ( 0x174e90 , 0x174ef0 , 0x174ab0 , 0x1743b0 , 0x1744b0 );

Now if I save, quit and relaunch, after NSLogging my category I see:

Name = "Cat1";
SoundsRelation =     (
    0x174e90 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p9>,
    0x174ea0 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p10>,
    0x174eb0 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p11>,
    0x174e90 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p12>,
    0x174ef0 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p13>,
    0x174ab0 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p14>,
    0x1743b0 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p15>,
    0x1744b0 <x-coredata://2E783972-3772-4CCA-9676-1D5F732D1FD2/Sounds/p16>
);

I see OLD SOUNDS + NEW SOUNDS! Why? What should I do to completely override OLD relations to NEW relations?

+1  A: 

This line:

[category setValue:nil forKeyPath:@"SoundsRelation"];

Doesn't remove the sounds from the ManagedObjectContext. It just breaks the linkage between the category object and the sounds object. CoreData doesn't like that because it creates orphaned objects in the persistent store. When you restart, CoreData assumes that an error orphaned the objects and it reassigns them to their original parent.

You should use the explicit 'ManagedObjectContext deleteObject:` command to remove the sounds and you need to make sure you have the appropriate delete rule set for the relationship.

TechZen
Thank you for your reply, TechZen. But what about removeSoundsRelation:? When I use this method, records are repairing too. Does this method just breaks linkage too?
beefon