如何在Core Data中保存实体并应用关系? [英] How do you save a entity in Core Data and apply a relationship?

查看:112
本文介绍了如何在Core Data中保存实体并应用关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到苹果文档/教程,如何让用户动态添加对象和应用必要的关系,但需要一些帮助什么教程是好的或什么苹果文档将有所帮助。

我的核心数据问题的一个例子:2个实体摄影师照片,其中摄影师照片之间存在1对多关系。当用户添加照片时,我需要能够指定摄影师照片

解决方案

首先,您需要为照片创建一个新实体:

  NSManagedObject * photoObject = [NSEntityDescription 
insertNewObjectForEntityForName:[entity name]
inManagedObjectContext:context];

如果你有一个子类,NSManagedObject可以替换为你的Photo对象或w / e。
您将为其设置所有属性。要设置摄影师,您将设置它像任何其他属性。如果您没有摄影师的参考资料,可以这样查询:

  NSFetchRequest * query = [[NSFetchRequest alloc] initWithEntityName:@Photographer]; 
[query setPredicate:[NSPredicate predicateWithFormat:@id ==%@,photographerId]];
NSArray * queryResults = [context executeFetchRequest:bcQuery error:& error];

您的查询结果将包含摄影师对象。

  [photoObject setPhotographer:[queryResults objectAtIndex:0]]; 

或如果您使用NSManagedObject:

  [photoObject setValue:[queryResults objectAtIndex:0] forKey:@photographer]; 

CoreData最酷的是它是关系型的。你不能真正把它看作一个典型的DBMS。您以非常面向对象的方式创建对象并设置它们之间的关系。没有主键/外键来照顾,这是所有在后台由CoreData完成。



编辑:



您可以选择创建NSManagedObject的子类,以更容易地访问实体的属性和关系。确保在CoreData模型浏览器中指定此子类;在侧边栏中,您将类字段更改为您的新子类。



这里是一个NSManagedObject子类的示例:

  //接口
@interface照片:NSManagedObject

#pragma mark - 属性

@property (非原子,强)NSNumber * id;
@property(nonatomic,strong)NSString * name;

#pragma mark - 关系

@property(nonatomic,strong)NSSet * someToManyRelationship;
@property(nonatomic,strong)摄影师*摄影师;

@end

//实现
@implementation照片

@dynamic id,name;
@dynamic someToManyRelationship,摄影师;

@end

希望这有助于!


I have tried to find apple documentation/tutorials out there on how to let a user dynamically add an object and apply the necessary relationship, but need some help with what tutorials are good or what apple documentation will help.

An example of my Core Data problem: 2 Entities of Photographer and Photo, where there is a 1-to-Many relationship between a Photographer and Photo. When the user adds a Photo, I need to be able to specify which Photographer took that Photo.

解决方案

So first you'll create a new entity for photo:

    NSManagedObject *photoObject = [NSEntityDescription 
           insertNewObjectForEntityForName:[entity name]
                    inManagedObjectContext:context];    

The NSManagedObject can be replaced with your Photo object or w/e if you have a subclass made. You will set all the attributes for it. To set the photographer you will set it as you would any other attribute. If you don't have a reference for the Photographer, you can query it like this:

    NSFetchRequest *query = [[NSFetchRequest alloc] initWithEntityName:@"Photographer"];
    [query setPredicate:[NSPredicate predicateWithFormat:@"id == %@", photographerId]];
    NSArray *queryResults = [context executeFetchRequest:bcQuery error:&error];

Your query result will have the photographer object. You can then set it on your photoObject as any other attribute and the link will be created automatically.

     [photoObject setPhotographer:[queryResults objectAtIndex:0]];

or if you are using NSManagedObject:

    [photoObject setValue:[queryResults objectAtIndex:0] forKey:@"photographer"];

The cool thing with CoreData is that it is relational. You can't really think of it as a typical DBMS. You create object and set relationships between them in a very object-oriented way. There is not primary-key/foreign-key to take care of, that is all done in the background by CoreData.

EDIT:

You can choose to create a subclass of NSManagedObject to more easily access attributes and relationships for your entity. Make sure to specify this subclass in the CoreData model explorer; in the sidebar, you will change the "class" field to your new subclass.

Here is an example of a NSManagedObject subclass:

//Interface
@interface Photo : NSManagedObject

#pragma mark - Attributes

@property (nonatomic, strong) NSNumber *id;
@property (nonatomic, strong) NSString *name;

#pragma mark - Relationships

@property (nonatomic, strong) NSSet     *someToManyRelationship;
@property (nonatomic, strong) Photographer *photographer;

@end

//Implementation
@implementation Photo

@dynamic id, name;
@dynamic someToManyRelationship, photographer;

@end

Hope this helps!

这篇关于如何在Core Data中保存实体并应用关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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