deskside / emodiary

0 stars 0 forks source link

How to add/fetch/write Core Data in a SwiftUI app? #4

Closed deskside closed 1 year ago

deskside commented 1 year ago

Add core data

First, edit core data/entities/attributes. Add all the attributes that specific data need in your app.

Notes:

deskside commented 1 year ago

Fetch core data

You need to first import core data:

import SwiftUI
import CoreData

Inside View, you can need to add following codes:

struct ContentView: View {
    // No need to edit, just copy 
    @Environment(\.managedObjectContext) private var viewContext

    // Plz change the entity name to yours
    @FetchRequest(sortDescriptors: [])  private var emo: FetchedResults<EmoEachTime>

    ...
}

Use core data


var body: some View {
        NavigationView {
            List {
                ForEach(emo) { item in
                    NavigationLink {
                        Text("Item at \(item.timestamp!, formatter: itemFormatter)")
                    } label: {
                        Text(item.timestamp!, formatter: itemFormatter)
                    }
                }
                .onDelete(perform: deleteItems)
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    EditButton()
                }
                ToolbarItem {
                    Button(action: addItem) {
                        Label("Add Item", systemImage: "plus")
                    }
                }
            }
            Text("Select an item")
        }
    }

Add and delete core data


    private func addItem() {
        withAnimation {
            let newItem = EmoEachTime(context: viewContext)
            newItem.timestamp = Date()

            do {
                try viewContext.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }

    private func deleteItems(offsets: IndexSet) {
        withAnimation {
            offsets.map { emo[$0] }.forEach(viewContext.delete)

            do {
                try viewContext.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nsError = error as NSError
                fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
            }
        }
    }
deskside commented 1 year ago

Write core data

First define some variants to store user-input-values temporarily.

    // MARK: Core Data
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest(sortDescriptors: []) var everday: FetchedResults<Everyday>
    @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)]) var wakeupFeelings: FetchedResults<WakeupFeelings>
    @FetchRequest(sortDescriptors: [NSSortDescriptor(key: "name", ascending: true)]) var bedtimeBehaviours: FetchedResults<BedtimeBehaviours>
    @FetchRequest(sortDescriptors: [], predicate:NSPredicate(format: "wakeupTime >= %@ AND wakeupTime < %@", argumentArray: [DateAndString.todayStartPoint(), DateAndString.todayEndPoint()])) var today: FetchedResults<Everyday>

    @State private var nightDate = Calendar.current.date(byAdding: DateComponents(hour:-Constants.sleepDuration), to: Date()) ?? Date()
    @State private var upDate = Calendar.current.date(byAdding: DateComponents(minute:-Constants.wakeupType), to: Date()) ?? Date()

    @State var sleepDuration:DateComponents = DateComponents()
    @State var todayBB = [String]()
    @State var todayWF = [String]()
    @State var todaySC = [String]()

After finishing input values, user press the OK button to add all temporary data into core data, which means calling this function.

var body: some View {
    Button {
                        if today.count == 0 {
                            addEverydayData()
                            dismiss()
                        }else{
                            self.showingAlert = true
                        }

                    } label: {
                        Text("确认")
                    }
    }

func addEverydayData(){
        let everyday = Everyday(context: viewContext)
        everyday.id = UUID()
        everyday.fallAsleepTime = nightDate
        everyday.wakeupTime = upDate
        everyday.sleepDuration = calculateSleepDurationIntoDouble(dateComponent: sleepDuration)
        everyday.bedtimeBehaviours = todayBB
        everyday.wakeupFeelings = todayWF
        everyday.sleepingCondition = todaySC
        do{
            try viewContext.save()
        }catch{

        }
    }
deskside commented 1 year ago

Notify that Xcode has already added following codes in YourApp.swift to help you assess core data.

@main
struct EmoDiaryApp: App {
    let persistenceController = PersistenceController.shared

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(\.managedObjectContext, persistenceController.container.viewContext)
        }
    }
}