How to use Core Data with @FetchRequest in SwiftUI | Continued Learning #14
@FetchRequest(
entity: FruitEntity.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \FruitEntity.name, ascending: true)],
animation: .default)
private var fruits: FetchedResults<FruitEntity>
...
private func saveItems() {
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
persistenceManager
에게 데이터 패치를 요청하는 FetchRequest
는 무슨 엔티티를 무슨 순서대로 가지고 올 것인지 정보를 입력받는다.save
하기만 하면 현재 로컬 상의 데이터가 '그대로' 들어간다. 즉 CRUD
모두 동일한 save
를 사용하는 것!import SwiftUI
import CoreData
struct ContentView: View {
@Environment(\.managedObjectContext) private var viewContext
@FetchRequest(
entity: FruitEntity.entity(),
sortDescriptors: [NSSortDescriptor(keyPath: \FruitEntity.name, ascending: true)],
animation: .default)
private var fruits: FetchedResults<FruitEntity>
@State private var textFieldText: String = ""
var body: some View {
NavigationView {
VStack(spacing: 20) {
TextField("Add fruit here...", text: $textFieldText)
.font(.headline)
.frame(maxWidth: .infinity)
.frame(height: 55)
.padding(.horizontal)
.padding(.leading)
Button(action: {
addItem()
}, label: {
Text("Submit")
.foregroundColor(.white)
.font(.headline)
.frame(maxWidth: .infinity)
.frame(height: 55)
.background(Color.blue.cornerRadius(10))
})
.padding(.horizontal)
List {
ForEach(fruits) { fruit in
Text(fruit.name ?? "")
.onTapGesture {
updateItem(fruit: fruit)
}
}
.onDelete(perform: deleteItems)
}
}
.listStyle(.plain)
.navigationTitle("Fruits")
Text("Select an item")
}
}
private func addItem() {
withAnimation {
let newFruit = FruitEntity(context: viewContext)
newFruit.name = textFieldText
saveItems()
textFieldText = ""
}
}
private func updateItem(fruit: FruitEntity) {
withAnimation {
let currentName = fruit.name ?? ""
let newName = currentName + "!"
fruit.name = newName
saveItems()
}
}
private func saveItems() {
do {
try viewContext.save()
} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
}
private func deleteItems(offsets: IndexSet) {
withAnimation {
guard let index = offsets.first else { return }
let fruitEntity = fruits[index]
viewContext.delete(fruitEntity)
saveItems()
}
}
}