简体   繁体   中英

Core Data relationships (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)

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

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?

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

Thanks!

UPDATE:

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

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.

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

Hope that is clear enough! :)

Thanks

在此输入图像描述

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

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

在此输入图像描述

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

在此输入图像描述

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.

A code sample in my case:

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

A something similar than what you want

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM