Swift 2.1核心数据 - 保存具有一对多关系但不添加异构数据(如果已存在)的数据

时间:2016-04-01 01:50:29

标签: ios swift core-data

我有一个ViewController,用于创建新的配方数据。 要创建新配方,用户需要填写标题,成分等,并选择其类别。为此,我建立了具有一对多关系的类别和配方实体。

在创建功能中,我需要进行以下搜索,以查看类别实体是否已具有selectedCategory的值。

如果selectedCategory已经存在于Category实体中,我需要找到它的索引并将该类别分配给Recipe Entity,但我不知道我需要在这里编写什么代码。

for category in categories {
    if category == selectedCategory {

       /* if selectedCategory already exists in Category entity, 
          find its index and assign that category to Recipe entity.*/

    } else {
       category.name = selectedCategory
       recipe.category = category
    }
}

context.insertObject(recipe)

do {
    try context.save()
} catch {
    print("Could not save recipe")
}

1 个答案:

答案 0 :(得分:1)

我创建了一个支持Core Data的单一视图应用。

我在数据模型中添加了两个实体:

CategoryEntity具有属性" name"。 RecipeEntity具有属性" title"。

CategoryEntity与RecipeEntity有一个可选的多对一关系,名为" recipes"。它有一个级联删除规则(如果删除一个类别,所有相关的配方也会被删除)。

RecipeEntity与CategoryEntity有一个必需的一对一关系,名为" category"。

这些关系都是另一个的倒数。

以下代码添加了一个配方。它首先获取或创建类别,并将配方分配给它。

import UIKit
import CoreData

class ViewController: UIViewController {

    @IBOutlet weak var category: UITextField!
    @IBOutlet weak var recipeTitle: UITextField!

    @IBAction func createRecipe(sender: AnyObject) {

        // Apple's out of the box Core Data app exposes a managedObjectContext on the appDelegate, so we'll use it here
        if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {

            // we have to have a category name ...
            if let categoryName = category.text {

                // ... and a recipe title
                if let recipeTitle = recipeTitle.text {

                    // get the category to associate this recipe with 
                    let categoryEntity = obtainCategoryEntity(categoryName, context: appDelegate.managedObjectContext)

                    // create the recipe entity
                    let recipeEntity = (NSEntityDescription.insertNewObjectForEntityForName("RecipeEntity", inManagedObjectContext: appDelegate.managedObjectContext) as! RecipeEntity)

                    // assign the recipe to the category 
                    //  note that you set up relationships in Core Data by assigning entities, 
                    //  not indexes or id fields
                    recipeEntity.category = categoryEntity

                    // set the recipe's title
                    recipeEntity.title = recipeTitle

                    // save it!
                    do {
                        try appDelegate.managedObjectContext.save()
                        NSLog("saved context")
                    } catch let error as NSError {
                        NSLog("failed to save context with error \(error)")
                    } catch {
                        fatalError()
                    }
                }
            }
        }
    }

    func obtainCategoryEntity(name: String, context: NSManagedObjectContext) -> CategoryEntity {

        // this function gets or creates a category entity
        let fetchRequest = NSFetchRequest(entityName: "CategoryEntity")
        fetchRequest.predicate = NSPredicate(format: "name == %@", name)

        //find existing category, if it exists
        if let results = (try? context.executeFetchRequest(fetchRequest)) as? [CategoryEntity] {
            if results.count > 0 {
                // we really should only ever have one match, so return it
                return results[0]
            }
        }

        //category did not exist, so create it
        let categoryEntity = NSEntityDescription.insertNewObjectForEntityForName("CategoryEntity", inManagedObjectContext: context) as! CategoryEntity

        // set the category name
        categoryEntity.name = name

        // return it, but don't save it here - let the save later take care of it
        return categoryEntity
    }
}