This file contains 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
class AppDelegate: UIResponder, UIApplicationDelegate { | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
// Initialize store | |
let context: NSManagedObjectContext = ... | |
let store = EventStore(context: context) | |
// Initialize root view controller | |
let viewController = EventListTableViewController(store: store) | |
// Initialize window... |
This file contains 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
class EventStore { | |
private let context: NSManagedObjectContext | |
init(context: NSManagedObjectContext) { | |
self.context = context | |
} | |
func insert(_ event: Event) { | |
EventManagedObject.insert(event, with: context) | |
} |
This file contains 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
@objc(EventManagedObject) | |
public class EventManagedObject: NSManagedObject, ManagedObjectConvertible { | |
static func fetchAll(from context: NSManagedObjectContext) -> [Event] { | |
let request = NSFetchRequest<EventManagedObject>(entityName: "EventManagedObject") | |
request.returnsObjectsAsFaults = false | |
do { | |
let eventManagedObjects = try context.fetch(request) | |
let events = eventManagedObjects.map { $0.toObject() } |
This file contains 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
// Get context | |
let context: NSManagedObjectContext = ... | |
// Create | |
let eventToInsert = Event(title: "I went to the hairdresser", date: Date()) | |
EventManagedObject.insert(object: eventToInsert, with: context) | |
// Read | |
let events: [Event] = EventManagedObject.fetchAll(from: context) |
This file contains 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
@objc(EventManagedObject) | |
public class EventManagedObject: NSManagedObject, ManagedObjectConvertible { | |
typealias T = Event | |
func from(object: Event) { | |
title = object.title | |
date = object.date | |
} | |
func toObject() -> Event { |
This file contains 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
extension ManagedObjectConvertible where T: ObjectConvertible, Self: NSManagedObject { | |
var identifier: String? { | |
return objectID.uriRepresentation().absoluteString | |
} | |
static func insert(_ object: T, with context: NSManagedObjectContext) { | |
guard object.identifier == nil else { return } | |
let managedObject = Self(context: context) | |
managedObject.from(object: object) |
This file contains 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
/// A `NSManagedObject` that wants to be converted from an `ObjectConvertible` object should implement the `ManagedObjectConvertible` protocol. | |
protocol ManagedObjectConvertible { | |
/// An object which should implement the `ObjectConvertible` protocol. | |
associatedtype T | |
/// A String representing a URI that provides an archiveable reference to the object in Core Data. | |
var identifier: String? { get } | |
/// Insert an object in Core Data. | |
/// |
This file contains 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
class Event: ObjectConvertible { | |
var title: String? | |
var date: Date? | |
private(set) var identifier: String? | |
init(title: String, date: Date?, identifier: String? = nil) { | |
self.title = title | |
self.date = date | |
self.identifier = identifier | |
} |
This file contains 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
/// An object that wants to be convertible in a managed object should implement the `ObjectConvertible` protocol. | |
protocol ObjectConvertible { | |
/// An identifier that is used to fetch the corresponding database object. | |
var identifier: String? { get } | |
} |