核心数据:重新设置多对多关系 [英] Core Data: re-setting to-many relationship

查看:127
本文介绍了核心数据:重新设置多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了您可以看到的模型:
http ://i.imagehost.org/0836/2009-11-08_14_37_41.png



我想存储关于声音类别和一些示例声音的信息每个类别。
类别有名称(NSString)和SoundsRelation(NSSet的NSSet,它代表声音)。



这里是问题:
例如一些类别包含几个与之相关的声音。假设声音数量为3.
所以如果我做

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

我会看到关于姓名和这三个声音的信息。类似这样的:

  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>
);

然后我想清除这类声音。我想将SoundsRelation设置为nil。



我这样做:

  [category setValue:nil forKeyPath:@SoundsRelation]; 

现在如果我这样做了

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

我会有:

  Name =Cat1; 
SoundsRelation =(
);

好吧,Cat1似乎没有声音。



现在我使用 [managedObjectContext save:] 方法和 QUIT APP 保存managedObjectContext。



当我重新启动我的应用程序并执行

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

我会有:

  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>
);

我看到我以前的声音!



现在,如果我覆盖SoundsRelation包含5个其他声音的其他NSSet:
[category setValue:otherSetWithFiveSounds forKeyPath:@SoundsRelation]; p>

并执行:
NSLog(@description:\\\
%@,category);



我看到:
Name =Cat1;
SoundsRelation =(
0x174e90,
0x174ef0,
0x174ab0,
0x1743b0,
0x1744b0
);



现在如果我保存,退出并重新启动,NSLogging后我看到:

  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& coredata:// 2E783972-3772-4CCA-9676-1D5F732D1FD2 / Sounds / p12> ;,
0x174ef0< x-coredata:// 2E783972-3772-4CCA-9676-1D5F732D1FD2 / Sounds / p13>,
0x174ab0 0x1743b0< x-coredata:// 2E783972-3772-4CCA-9676-1D5F732D1FD2 / Sounds / p15>,
0x1744b0< x-coredata:// 2E783972-3772-4CCA-9676-1D5F732D1FD2 / Sounds / p16>
);

我看到OLD SOUNDS + NEW SOUNDS!
为什么?

解决方案

这行:

  [category setValue:nil forKeyPath:@SoundsRelation]; 

不会从ManagedObjectContext中删除声音。它只是打破了类别对象和声音对象之间的链接。 CoreData不喜欢它,因为它在持久存储中创建了孤立的对象。当重新启动时,CoreData假定错误孤立对象,并将它重新分配给它们的原始父对象。



您应该使用显式的ManagedObjectContext deleteObject:`命令删除声音,您需要确保您为关系设置了适当的删除规则。


I have created model which you can see there: http://i.imagehost.org/0836/2009-11-08_14_37_41.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?

解决方案

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.

这篇关于核心数据:重新设置多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆