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
lazy var managedObjectModel: NSManagedObjectModel = { | |
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
https://www.youtube.com/watch?v=uJuLk1niBYA | |
--- 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 |
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
import Foundation | |
import CoreData | |
extension Device { | |
@nonobjc public class func fetchRequest() -> NSFetchRequest<Device> { | |
return NSFetchRequest<Device>(entityName: "Device") | |
} | |
@NSManaged public var deviceType: String | |
@NSManaged public var name: String | |
@NSManaged public var owner: Person? | |
} |
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
import CoreData | |
import UIKit | |
@objcMembers | |
class DeviceDetailTableViewController: UITableViewController { | |
var managedObjectContext: NSManagedObjectContext! | |
var device Device? | |
@IBOutlet weak var nameTextField: UITextField! | |
@IBOutlet weak var deviceTypeTextField: UITextField! | |
@IBOutlet weak var deviceOwnerLabel: UILabel! | |
override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
if let device = device { | |
nameTextField.text = device.name | |
deviceTypeTextField.text = device.deviceType | |
if let owner = device.owner { | |
deviceOwnerLabel.text = "Device owner: \(owner.name)" | |
} else { | |
deviceOwnerLabel.text = "set device owner" | |
} | |
} | |
} | |
override func viewWillDisappear(animated: Bool) { | |
// need to add a device? | |
if device == nil { | |
if let name = nameTextField.text, deviceType = deviceTypeTextField.text, | |
entity = NSEntityDescription.entityForName("Device", | |
inManagedObjectContext: managedObjectContext) where !name.isEmpty | |
&& !deviceType.isEmpty { | |
device = Device(entity: entity, insertIntoManagedObjectContext: | |
managedObjectContext) | |
device?.name = name | |
device?.deviceType = deviceType | |
} | |
} | |
} | |
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexpath: | |
NSIndexPath) { | |
if indexPath.section == 1 && indexPath.row == 0 { | |
if let personPicker = | |
storyboard?.instatiateViewControllerWithIdentifier("People") as? | |
PeopleTableViewController { | |
// more personPicker setup code here | |
personPicker.pickerDelegate = self | |
personPicker.selectedPerson = device?.owner | |
navigationController?.pushViewController(personPicker, animated: true) | |
} | |
} | |
} | |
tableView.deselectRowAtIndexPath(indexPath, animated: true) | |
} | |
} | |
extension DeviceDetailTableViewController: PersonPickerDelegate { | |
func didSelectPerson(person: Person) { | |
device?.owner = person | |
do { | |
try managedObjectContext.save() | |
} catch { | |
print("Error saving the managed object context!") | |
} | |
} | |
} |
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
import UIKit | |
import CoreData | |
protocol PersonPickerDelegate: class { | |
func didSelectPerson(person: Person) | |
} | |
@objcMembers | |
class PeopleTableViewController: UITableViewController { | |
var managedObjectContext: NSManagedObjectContext! | |
var people = [Person]() | |
// for person select mode | |
weak var pickerDelegate: PersonPickerDelegate? | |
var selectedPerson: Person? | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment