核心数据关系(swift) [英] Core Data relationships (swift)

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

问题描述

我正在建立一个需要核心资料关系的应用程式:

I'm building an app that requires a core data relationship as such:

entityA <<---> entityB  (e.g. any given entityA can hold many entityB objects)

我想要能够存储entityB对象在任何给定的entityA对象。

I have two tableviews with entityA list items in which I want to be able to store entityB objects in any given entityA object.

我是第一次使用与核心数据的关系(并且相当新的swift),并想学习如何使这项工作。有没有任何人有任何快速教程,我会考虑看看或任何其他资源,可能会帮助我学习。

I'm new to using relationships with core data (and fairly new to swift) and would like to learn how to make this work. Does anyone have any swift tutorials in mind that would be good for me to take a look at or any other resources that may help me learn?

对不起,如果我的问题不' t很有意义,请我澄清,如果你需要。

Sorry if my question doesn't make much sense, ask me to clarify if you need.

谢谢!

更新:

这里有一些更具体的内容我想学习。

Here's a bit more specificity on what I'm wanting to learn.

假设我有一个实体Person(属性可能包括名称,年龄等)和一个tableview,我的应用程序用户可以在其中添加一个人。 (这个我已经建立并知道如何做适当的)但是,现在我想添加实体Meal(属性可能包括食品),Meal是自己的tableview,我可以通过选择人想加一顿饭。每个人可以有一个以上的餐,但每餐只能有一个人。

Lets say I have the entity "Person" (attributes may include name, age, etc.) and a tableview in which my app users can add a person to. (this I have established and know how to do appropriately) But, now I want to add the entity "Meal" (attributes may include food items), and Meal is a tableview of its own that I can access by choosing the person that I want to add a meal to. Each person can have more than one meal, but there can only be one person per meal.

问题是:为了实现这个目的,我的核心数据模型,fetchRequests等会是什么样子?

The question is: what would my core data model, fetchRequests, etc look like in order to accomplish this?

希望那是很清楚! :)

Hope that is clear enough! :)

感谢

这是我的功能的代码片段创建一顿饭:

Here's a code snippet of my function for creating a meal:

    func createMeal() {
    let entityDescription = NSEntityDescription.entityForName("Meal", inManagedObjectContext: managedObjectContext!)
    let meal = Meal(entity: entityDescription!, insertIntoManagedObjectContext: managedObjectContext)

    meal.mealName = mealNameTxt.text
    meal.mealItem1 = mealItem1Txt.text

    managedObjectContext?.save(nil)
}


推荐答案

让我们举个例子,你有一个分支,分支有很多规格。首先,您需要转到您的xcdatamodel并创建数据实体。

Well, it's pretty simple. Let's have an example, you have a branch and the branch has lots of specifications. Firstly you need to go to your xcdatamodel and create your data entities

然后您打开编辑器(表样式)并在您的分支机构中建立关系

Then you open you editor (table style) and make the relation in your branch entity

>

之后,您需要在您的分支规则中设置关系类型。

After that you will need to set up the relation typo in your branchSpecs too

就是这样!您刚刚创建了CoreData实体之间的关系。您需要做的是生成子类对象

And that's it! You have just created a relationship between your CoreData entities. All you need to do is to generated the subclassed objects

现在你已经设置好了。你会在你的分支类中找到一个NSSet *对象,它保存该分支的数据相关规范。此外,您将找到一个称为addSpecsObject的方法,您可以使用它来存储specs对象。

And now you're all set. You will find a NSSet * object in your branch class that holds the data related specs of that branch. Also your will find a method called addSpecsObject that you can use to store the specs object.

我的代码示例:

Branch * branch = [NSEntityDescription insertNewObjectForEntityForName:@"Branch"
                                                  inManagedObjectContext:managedObjectContext];
    branch.name = obj.name;
    branch.lattitude = obj.latitude;
    branch.longitude = obj.longitude;
    branch.dispalyedDescription = obj.dispalyedDescription;


    for (FLBranchesSpecs * spec in obj.branchSpecs) {
        BranchSpecs * branchSpec = [NSEntityDescription insertNewObjectForEntityForName:@"BranchSpecs"
                                                        inManagedObjectContext:managedObjectContext];
        branchSpec.type = @(spec.type);
        branchSpec.key = spec.key;
        branchSpec.value = spec.value;
        [branch addSpecsObject:branchSpec];
    }

    NSError *error;
    if (![managedObjectContext save:&error])
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);

类似于您想要的东西

let person: AnyObject = NSEntityDescription.insertNewObjectForEntityForName("Person", inManagedObjectContext: self.managedObjectContext!)
    //do you code assignment here
    for meal in listOfMeals{
        person.addMealObject(meal)
    }
    var error: NSError?
    self.managedObjectContext?.save(&error)

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

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