Created
August 8, 2019 18:46
-
-
Save ericlewis/6909ba0b57f3af4ba5681b7bd577d479 to your computer and use it in GitHub Desktop.
Demonstration of using Core Data with SwiftUI
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
// | |
// ContentView.swift | |
// CoreDataSwiftUISample | |
// | |
// Created by Eric Lewis on 8/8/19. | |
// Copyright © 2019 Eric Lewis, Inc. All rights reserved. | |
// | |
import SwiftUI | |
import CoreData | |
extension FetchedResults where Result: Item { | |
func delete(_ managedObjectContext: NSManagedObjectContext) -> (IndexSet) -> Void { | |
func delete(_ offset: IndexSet) { | |
var arr = Array(self) | |
arr.remove(atOffsets: offset) | |
let diff = arr.difference(from: self) | |
for change in diff { | |
switch change { | |
case let .remove(_, element, _): | |
managedObjectContext.delete(element) | |
case .insert(_, _, _): | |
print("nothing") | |
} | |
} | |
} | |
return delete | |
} | |
func move(_ managedObjectContext: NSManagedObjectContext) -> (IndexSet, Int) -> Void { | |
func move(_ offset: IndexSet, _ destination: Int) { | |
managedObjectContext.perform { | |
var arr = Array(self) | |
arr.move(fromOffsets: offset, toOffset: destination) | |
for i in 0..<arr.count { | |
arr[i].order = Int32(i) | |
} | |
} | |
} | |
return move | |
} | |
} | |
extension Item { | |
var checkbox: String { | |
isComplete ? "checkmark.square.fill" : "square" | |
} | |
var checkboxColor: Color { | |
isComplete ? .green : .secondary | |
} | |
} | |
struct Cell: View { | |
@Environment(\.managedObjectContext) var managedObjectContext | |
@ObservedObject var item: Item | |
init(_ item: Item) { | |
self.item = item | |
} | |
func toggleComplete() { | |
managedObjectContext.perform { | |
self.item.objectWillChange.send() | |
self.item.isComplete.toggle() | |
try? self.managedObjectContext.save() | |
} | |
} | |
var body: some View { | |
Button(action: toggleComplete) { | |
HStack { | |
Image(systemName: item.checkbox) | |
.foregroundColor(item.checkboxColor) | |
Text(item.title ?? "") | |
} | |
} | |
} | |
} | |
struct ContentView: View { | |
@Environment(\.managedObjectContext) var managedObjectContext | |
@FetchRequest(fetchRequest: listFetchRequest) var items: FetchedResults<Item> | |
func add() { | |
let item = Item(context: self.managedObjectContext) | |
item.title = "Test \(items.count)" | |
item.order = Int32(items.count) | |
} | |
var body: some View { | |
NavigationView { | |
List { | |
ForEach(items, id: \.objectID) { item in | |
Cell(item) | |
} | |
.onDelete(perform: items.delete(managedObjectContext)) | |
.onMove(perform: items.move(managedObjectContext)) | |
} | |
.navigationBarTitle("List") | |
.navigationBarItems(leading: EditButton(), trailing: | |
Button(action: add) { | |
Image(systemName: "plus") | |
} | |
) | |
} | |
} | |
static var listFetchRequest: NSFetchRequest<Item> { | |
let request: NSFetchRequest<Item> = Item.fetchRequest() | |
request.sortDescriptors = [NSSortDescriptor(key: "order", ascending: true)] | |
return request | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment