建立两个实体之间的关系?

时间:2017-02-27 04:57:06

标签: swift xcode core-data

我在tableview中有事件名称,我喜欢保存用户的响应(yes = 1,no = 2,来自动作控制器)以及事件名称。所以我创建了2个实体

EventSchedule (eventName: String, eventTime: Transformable) 

one to one关系eventSchedule

EventDec (eventStatus: Integer16)

one to one反向关系eventDec

我做了以下

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
            let alert = UIAlertController(title: "title", message: "msg", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "yes", style: .default) { _ in

let eventDesc = NSEntityDescription.insertNewObject(forEntityName: "EventDec", into: context)
let eventSch = NSEntityDescription.insertNewObject(forEntityName: "EventSchedule", into: context)

//How do I add relationship property to the above objects so that I can store event name along with response in database?

            eventDesc.setValue(1, forKey: "eventStatus")
            do
            {
                try context.save()
                print("Record Saved Successfully")
                print(eventDesc)
            }
            catch{}
        })
        alert.addAction(UIAlertAction(title: "no", style: .default) { _ in

 let eventDesc = NSEntityDescription.insertNewObject(forEntityName: "EventDec", into: context)
    let eventSch = NSEntityDescription.insertNewObject(forEntityName: "EventSchedule", into: context)

//How do I add relationship property to the above objects so that I can store event name along with response in database?

            eventDesc.setValue(2, forKey: "eventStatus")
            do
            {
                try context.save()
                print("Record Saved Successfully")
                print(eventDesc)
            }
            catch{} 
        })

        present(alert, animated: true, completion: nil)
}

2 个答案:

答案 0 :(得分:2)

// ... Your code...

let eventDesc = NSEntityDescription.insertNewObject(forEntityName: "EventDec", into: context)

// insert eventDesc into coredata
eventDesc.setValue(2, forKey: "eventStatus")

// relation & inverse relation
eventSch.eventDec = eventDesc
eventDesc.eventSchedule = eventSch

// rest of your code, as is...

让我知道它是怎么回事。

修改

您可以获得如下eventSch对象:

do {
    let fetchRequest = NSFetchRequest(entityName: "EventSchedule")
    fetchRequest.fetchLimit = 1
    fetchRequest.predicate = NSPredicate(format: "name == %@", your_event_name)
    var objects: [EventSchedule]

    // objects[0] is your object here.
    try objects = context.executeFetchRequest(fetchRequest) as! [EventSchedule]

} catch {
    print("whatever")
}

答案 1 :(得分:2)

您可以将didSelectRowAt替换为以下内容(基于聊天中的讨论):

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){

    let eventDesc = NSEntityDescription.insertNewObject(forEntityName: "EventDec", into: context) as! EventDec
    var eventSch: EventSchedule?
    do {
        let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "EventSchedule")
        fetchRequest.fetchLimit = 1
        fetchRequest.predicate = NSPredicate(format: "eventName == %@", self.eventTitle)
        var objects: [EventSchedule]

        try objects = context.fetch(fetchRequest) as! [EventSchedule]
        eventSch = objects[0] as EventSchedule
        print(eventSch)

    } catch {
        print("whatever")
    }

    eventSch.eventDec = eventDesc
    eventDesc.eventSchedule = eventSch

    let alert = UIAlertController(title: "title", message: "msg", preferredStyle: .alert)

    alert.addAction(UIAlertAction(title: "yes", style: .default) { _ in
        eventDesc.setValue(1, forKey: "eventStatus")

        do {
            try context.save()
        } catch{
            print("Try again")
        }
    })

    alert.addAction(UIAlertAction(title: "no", style: .default) { _ in
        eventDesc.setValue(2, forKey: "eventStatus")

        do {
            try context.save()
        } catch {
            print("Try again")
        }
    })
    present(alert, animated: true, completion: nil)
}