-
Notifications
You must be signed in to change notification settings - Fork 15
/
CoreDataStore.swift
42 lines (35 loc) · 903 Bytes
/
CoreDataStore.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// CoreDataStore.swift
// Appt
//
// Created by Agustin Mendoza Romo on 5/30/17.
// Copyright © 2017 AgustinMendoza. All rights reserved.
//
import Foundation
import CoreData
class CoreDataStore {
static var instance: CoreDataStore = CoreDataStore(dataModel: "AppointmentModel")
var persistentContainer: NSPersistentContainer
var context: NSManagedObjectContext {
return persistentContainer.viewContext
}
init(dataModel: String) {
persistentContainer = NSPersistentContainer(name: dataModel)
persistentContainer.loadPersistentStores { (desc, error) in
if let error = error as NSError? {
print("Persist container failed", error.localizedDescription)
}
}
}
func save() {
guard context.hasChanges else {
return
}
do {
try context.save()
}
catch {
print("Saving didn't work!", error)
}
}
}