Skip to content

Instantly share code, notes, and snippets.

Avatar

Nicolas Bichon nicol3a

View GitHub Profile
View store-init.swift
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...
View EventStore.swift
class EventStore {
private let context: NSManagedObjectContext
init(context: NSManagedObjectContext) {
self.context = context
}
func insert(_ event: Event) {
EventManagedObject.insert(event, with: context)
}
View EventManagedObject.swift
@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() }
View crud-operations.swift
// 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)
View EventManagedObject.swift
@objc(EventManagedObject)
public class EventManagedObject: NSManagedObject, ManagedObjectConvertible {
typealias T = Event
func from(object: Event) {
title = object.title
date = object.date
}
func toObject() -> Event {
View ManagedObjectConvertible.swift
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)
View ManagedObjectConvertible.swift
/// 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.
///
View Event.swift
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
}
View ObjectConvertible.swift
/// 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 }
}