核心数据在代码中的简单关系 [英] Core Data simple relationship in code
问题描述
我是一个可可新手试图创建一个iPhone应用程序,包括Core Data。
我的问题是:我有一个小应用程序运行,我在表格视图中显示的播放列表,可以添加和删除条目。
我在PlayerAppDelegate中有这些:
playlistManagedObjectModel
playlistListManagedObjectContext
playlistListPersistentStoreCoordinator
添加实体:
播放列表*播放列表=(播放列表*)[NSEntityDescription
insertNewObjectForEntityForName:@Playlist
inManagedObjectContext:playlistListManagedObjectContext];
现在我想添加一个名为Song的子级别,
添加了播放列表属性:songRelation
添加了歌曲属性:playlistRelation
设置此关系后,我现在无法再创建没有收到警告的播放列表。问题是,it想要一首歌,但我不知道如何。
我找不到一个地方有一个例子,如何添加一个新的播放列表在这种情况下,即当is与另一个实体的关系必须被添加。
我需要创建这些:
songManagedObjectModel
songListManagedObjectContext
songListPersistentStoreCoordinator
或者是通过播放列表实体可以访问的歌曲实体? p>
这样可能是:
添加播放列表
添加歌曲
设置relation属性(如何实现)
或????
我真的有很多话,可能误解了一些基本的东西,因为没有例子....
Rgds
PM
Petter,
在PlayList模型对象上应该有一个方法:
- (void)addSongObject:(Song *)value;
这是一个动态生成的方法,将在PlayList的歌曲关系中插入一个歌曲值。如果你没有生成PlayList模型对象,因为添加了Song关系,那个方法就不会存在了。
传递给addSongObject方法的Song对象应该使用ManagedObjectContext来创建,如下所示:
Song * song =(Song *)[NSEntityDescription insertNewObjectForEntityForName:@SonginManagedObjectContext:playlistListManagedObjectContext];
杰克
I am a cocoa newbie trying to create an iPhone app including Core Data.
My problem is this: I have a little app running now with a single entity called Playlist that I display in a table view and can add and delete entries in.
I have these in my PlayerAppDelegate:
playlistManagedObjectModel
playlistListManagedObjectContext
playlistListPersistentStoreCoordinator
Adding entity with:
Playlist *playlist = (Playlist *)[NSEntityDescription
insertNewObjectForEntityForName:@"Playlist"
inManagedObjectContext:playlistListManagedObjectContext];
Now I want to add a sublevel called Song with a to-many relation.
Playlist attribute added: songRelation Song attribute added: playlistRelation
I have created this entity and set up the relations both ways, clicking of the Optional flag as I want to have at least one Song in a Playlist.
After setting this relation I can now no longer create a Playlist without getting a warning. The problem is that "it" wants a Song created too, but I dont know how.
I cant find a single place with an example on how to add a new playlist in this case, ie when is has a relation to another entity that has to be added too.
Do I need to create these:
songManagedObjectModel
songListManagedObjectContext
songListPersistentStoreCoordinator
or is the Song entity accessible via the playlist entity somehow?
Something like this perhaps:
Add Playlist
Add Song
Set up "relation" attributes (how?)
Save to persistent store
Or????
I have really googled a lot and has probably misunderstood something basic here since there are no examples available....
Rgds PM
Petter,
There should be a method on the PlayList model object that is something like:
- (void) addSongObject:(Song *)value;
It is a dynamically generated method that will insert a Song value into the relationship for songs in PlayList. If you have not generated the PlayList model object since adding the Song relationship, that method will not be there. So, make sure you generate model classes for any changed entities.
The Song object that you pass to the addSongObject method should be created using the ManagedObjectContext like:
Song *song = (Song *)[NSEntityDescription insertNewObjectForEntityForName:@"Song" inManagedObjectContext:playlistListManagedObjectContext];
Jack
这篇关于核心数据在代码中的简单关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!