Last active
March 29, 2020 20:27
-
-
Save JulesMoorhouse/2c811a8d79d75429f89691f25900a03b to your computer and use it in GitHub Desktop.
Relationships - core data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let modelURL = | |
| NSBundle.mainBundle().URLForResource("myDevices", withExtension: "Momd")! | |
| return NSManagedObjectModel(contentsOfURL: modelURL)! | |
| }() | |
| lazy var persistentStoreCoordinator: | |
| NSPersistentStoreCoordinator = { | |
| let coordinator = NSPersistentStoreCoordinator | |
| (managedObjectModel: self.managedObjectModel) | |
| let url = self.applicationDocumentDirectory. | |
| URLByAppendingPathComponent("SingleViewCoreData.sqlite") | |
| var failureReason = "There was an error creating or loading the application's saved data." | |
| do { | |
| try coordinator.addPersistenStoreWithType (NSSQLiteStoreType, | |
| configuration: nil, URL: url, options: | |
| [NSMigratePersistentStoresAutomaticallyOption: true, | |
| NSInferMappingModelAutomaticallyOption: true]) | |
| } catch { | |
| var dict = [String: AnyObject]() | |
| dict[NSLocalisedDescriptionKey] = "Failed to initialize the application's saved data" | |
| dict[NSUnderlyingErrorKey] = error as NSError | |
| let wrappedError = NSError(domain: | |
| "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict) | |
| NSLog("Unresolved error \(wrappedError), \(wrappedError.userInfo)") | |
| abort() | |
| } | |
| return coordinator | |
| }() | |
| lazy var managedObjectContext: NSManagedObjectContext { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --- add relationships | |
| Device - Owner, Destination - Person, leave optional | |
| Persion - Devices, Destination - Device, Inverse - Owner, not optional, Type - to many | |
| Editor > Create NSManagedObjects Subclass, both | |
| In Devices.swift | |
| var deviceDescription: String { | |
| return "\(name) (\(deviceType))" | |
| } | |
| Remember again to remove the question mark from Person - name, devices. Device - all Strings |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment