-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTaskPersistence.swift
43 lines (35 loc) · 959 Bytes
/
TaskPersistence.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
//
// PersistenceHelper.swift
// TaskApplication
//
// Created by Michael Crump
// Copyright (c) 2015 Michael Crump. All rights reserved.
//
import UIKit
import CoreData
class TaskPersistence: NSObject {
var appDel: AppDelegate
var context: NSManagedObjectContext;
override init() {
appDel = (UIApplication.shared.delegate as! AppDelegate)
context = appDel.managedObjectContext
}
func construct()-> Task? {
return NSEntityDescription.insertNewObject(forEntityName: "Task", into: context) as? Task
}
func list() -> [Task] {
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Task")
request.returnsObjectsAsFaults = false;
let results: [Task] = try! context.fetch(request) as! [Task]
return results
}
func remove(instance: NSManagedObject) -> Bool {
context.delete(instance)
do {
try context.save()
} catch _ {
return false
}
return true
}
}