Skip to content

Instantly share code, notes, and snippets.

@koromiko
Last active September 7, 2017 02:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koromiko/ddd0b8638888a6339ee190952feeb53b to your computer and use it in GitHub Desktop.
Save koromiko/ddd0b8638888a6339ee190952feeb53b to your computer and use it in GitHub Desktop.
//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