我注入了托管上下文来保存和获取实体,我很满意。我的问题是如何获得与1对1关系相关的数据?
以下是代码,请参阅viewDidLoad中的注释:
import UIKit
import CoreData
class E2_VC: UIViewController {
var managedContext: NSManagedObjectContext!
var selectedE1: Entity_1!
var selectedE2: Entity_2!
// MARK: Properties
@IBOutlet weak var e2_a1: UITextField!
@IBOutlet weak var e2_a2: UITextField!
@IBOutlet weak var e2_a3: UITextField!
@IBOutlet weak var e4_a1: UITextField!
@IBOutlet weak var e4_a2: UITextField!
@IBOutlet weak var e5_a1: UITextField!
@IBOutlet weak var e5_a2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
print(selectedE1)
print(selectedE2)
e2_a1.text = selectedE2.attribute1
e2_a2.text = selectedE2.attribute2
e2_a3.text = selectedE2.attribute3
// I want to get something like "e4_a1.text = selectedE2.e4_to_e2.attribute1"
// but obviously it is not working like that, what am I missing here?
// I'm thinking about making a fetch request but I don't know how to
// make a fetch req that specifically belongs to the selectedE2 onbejct.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
// MARK: - Navigation
func navigateBack(){
let isCurrentNavContrl = presentingViewController is UINavigationController
if isCurrentNavContrl {
dismissViewControllerAnimated(true, completion: nil)
} else {
navigationController!.popViewControllerAnimated(true)
}
}
// MARK: - Save
@IBAction func save(sender: UIBarButtonItem) {
let newE2 = NSEntityDescription.entityForName("Entity_2", inManagedObjectContext: managedContext)
let newE4 = NSEntityDescription.entityForName("Entity_4", inManagedObjectContext: managedContext)
let newE5 = NSEntityDescription.entityForName("Entity_5", inManagedObjectContext: managedContext)
if selectedE1 != nil {
do {
let ent2 = Entity_2(entity: newE2!, insertIntoManagedObjectContext: managedContext)
let ent4 = Entity_4(entity: newE4!, insertIntoManagedObjectContext: managedContext)
let ent5 = Entity_5(entity: newE5!, insertIntoManagedObjectContext: managedContext)
ent2.attribute1 = e2_a1.text!
ent2.attribute2 = e2_a2.text!
ent2.attribute3 = e2_a3.text!
ent4.attribute1 = e4_a1.text!
ent4.attribute2 = e4_a2.text!
ent2.e2_to_e4 = ent4
ent4.e4_to_e2 = ent2
ent5.attribute1 = e5_a1.text!
ent5.attribute2 = e5_a2.text!
ent2.e2_to_e5 = ent5
try managedContext.save()
} catch let error as NSError {
print("Error: \(error) " + "description \(error.localizedDescription)")
}
} else {
do {
selectedE2.attribute1 = e2_a1.text!
selectedE2.attribute2 = e2_a2.text!
selectedE2.attribute3 = e2_a3.text!
try managedContext.save()
} catch let error as NSError {
print("Error: \(error) " + "description \(error.localizedDescription)")
}
}
navigateBack()
}
@IBAction func cancel(sender: UIBarButtonItem) {
navigateBack()
}
}
这条线说的是什么(我的意思是我不太明白什么是关系错误)?
"e1_to_e2" = "<relationship fault: 0x7fbf995e3800 'e1_to_e2'>";
这一个;我没有设置e2_to_e1。我认为核心数据会将其设置为我将其设置为e1_to_e2关系的反转。
"e2_to_e1" = nil;
以下是控制台上的 selectedE1 和 selectedE2 输出:
<iTMJ_CD_93.Entity_1: 0x7fbf9b04ef20> (entity: Entity_1; id: 0xd000000000080000 <x-coredata://F20092F3-5A36-49C3-9AAD-3F1E132CD538/Entity_1/p2> ; data: {
attribute1 = a;
attribute2 = s;
attribute3 = d;
"e1_to_e2" = "<relationship fault: 0x7fbf995e3800 'e1_to_e2'>";
})
<iTMJ_CD_93.Entity_2: 0x7fbf9962c670> (entity: Entity_2; id: 0xd000000000040002 <x-coredata://F20092F3-5A36-49C3-9AAD-3F1E132CD538/Entity_2/p1> ; data: {
attribute1 = e2a1;
attribute2 = e2a2;
attribute3 = e2a3;
date = nil;
"e2_to_e1" = nil;
"e2_to_e3" = "<relationship fault: 0x7fbf995e6940 'e2_to_e3'>";
"e2_to_e4" = "0xd000000000040004 <x-coredata://F20092F3-5A36-49C3-9AAD-3F1E132CD538/Entity_4/p1>";
"e2_to_e5" = "0xd000000000040006 <x-coredata://F20092F3-5A36-49C3-9AAD-3F1E132CD538/Entity_5/p1>";
})
这是我的数据模型: