Hello everyone,
In the Core Data book we are given the Core Data Stack in chapter 3. This allow us to inject managed context to save and fetch entities and I’m comfortable with that. My question is how can I get data that is connected with 1-to-1 relationship?
Here is the code:
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 is specifically belong to the selectedE2 onbejct.
}
// ...
Here is the selectedE1 and selectedE2 output on the console:
<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>";
})
Here is my data model: