-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoreDataStack.swift
64 lines (54 loc) · 1.62 KB
/
CoreDataStack.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// CoreDataStack.swift
// Dday
//
// Created by 한수진 on 2021/08/29.
//
import Foundation
import CoreData
class CoreDataStack {
static let shared = CoreDataStack()
private init() {}
var managedObjectContext: NSManagedObjectContext {
return self.persistentContainer.viewContext
}
var workingContext: NSManagedObjectContext {
let context = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
context.parent = self.managedObjectContext
return context
}
// MARK: - Core Data stack
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "DdayModel")
container.loadPersistentStores(completionHandler: { storeDescription, error in
if let error = error as NSError? {
// RaiseError.raise()
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext() {
self.managedObjectContext.performAndWait {
if self.managedObjectContext.hasChanges {
do {
try self.managedObjectContext.save()
// appPrint("Main context saved")
} catch {
print(error)
// RaiseError.raise()
}
}
}
}
func saveWorkingContext(context: NSManagedObjectContext) {
do {
try context.save()
print("Working context saved")
saveContext()
} catch (let error) {
print(error)
// RaiseError.raise()
}
}
}