Last active
September 7, 2017 02:24
-
-
Save koromiko/ddd0b8638888a6339ee190952feeb53b to your computer and use it in GitHub Desktop.
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
//MARK: CRUD | |
func insertTodoItem( name: String, finished: Bool ) -> ToDoItem? { | |
guard let toDoItem = NSEntityDescription.insertNewObject(forEntityName: "ToDoItem", into: backgroundContext) as? ToDoItem else { return nil } | |
toDoItem.name = name | |
toDoItem.finished = finished | |
return toDoItem | |
} | |
func fetchAll() -> [ToDoItem] { | |
let request: NSFetchRequest<ToDoItem> = ToDoItem.fetchRequest() | |
let results = try? persistentContainer.viewContext.fetch(request) | |
return results ?? [ToDoItem]() | |
} | |
func remove( objectID: NSManagedObjectID ) { | |
let obj = backgroundContext.object(with: objectID) | |
backgroundContext.delete(obj) | |
} | |
func save() { | |
if backgroundContext.hasChanges { | |
do { | |
try backgroundContext.save() | |
} catch { | |
print("Save error \(error)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment