(2020) SwiftUI - CoreData - Part 1 of 2 - Create, Read, Update, Delete Data

preview_player
Показать описание
This video is part of a two part CoreData CRUD series. This series covers the integration of CoreData into your SwiftUI application! To store basic data you just need to finish the first two videos!

All the files will be uploaded when I complete the second video at the following link:

If you're looking for more tutorials covering different topics, here are a few updated tutorials I've compiled:

Intermediate Series:

Firebase + Firestore Series:

Fullstack Series - Instagram-Inspired End Product (Covers Firebase, Cocoapods, Clean UI Development, ImagePickers):

Basics for Beginners:

Spotify Clone (Beginner Series):

Vacation Planner (Intermediate Series):
Рекомендации по теме
Комментарии
Автор

Subbed, always happy to help another Chris out 😂 .

But seriously, loving your content man. Just gotta find that one video that really kickstarts the channel. Definitely deserve more views.

chrisaltec
Автор

Great videos -- I am a new subscriber. Would love to see how to decouple view logic from my business logic. Supposedly I can use controllers but I haven't figured out how to do that exactly yet.

coderanger
Автор

This is what I’m doing. The key step is reloading the view’s state but that makes SwiftUI’s @binding wrapper redundant.

GiveMeAnOKUsername
Автор

You might already have figured this out but the reason it doesnt find the entity in scope is because it has never been built yet. Command+B to build should fix that without needing to restart xcode. cheers

alessio.diimperio
Автор

Am I able to add all of this data on one view, and then save, and then after when I click on it in a list of a navigation view it shows?

prod.kashkari
Автор



App.swift
import SwiftUI
import CoreData

@main
struct RandomApp: App {

    @Environment(\.scenePhase) private var scenePhase

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

        .onChange(of: scenePhase) { phase in
            switch phase {
            case .active:
            case .inactive:
            case .background:
                saveContext()
            @unknown default:
                fatalError("Scene phase not supported.")
            }
        }
    }


    var leaderboard: NSPersistentCloudKitContainer = {
        let container = "Leaderboard")
        { (storeDescription, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

    func saveContext() {
        let context = leaderboard.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }
}

ContentView.swift
import SwiftUI
import CoreData

struct ContentView: View {

    var leaderboard

    @FetchRequest(entity: GameMO.entity(),
                  sortDescriptors: [NSSortDescriptor(key: "startDate", ascending: false)])
    var games: FetchedResults<GameMO>

    var body: some View {

        VStack {
Text("Players: ?? 0)")
            Button {
                startNewGame()
addPlayer()
                save()
}
}

    func startNewGame() {
        let newGame = GameMO(context: self.leaderboard)
        newGame.id = UUID()
        newGame.name = self.todaysDate()
        newGame.rules = nil
        newGame.startDate = Date()
        newGame.typeEnumWorkaround = .vanilla
    }

func save() {
        do {
            try self.leaderboard.save()
        } catch {
           
        }
    }
}

Ryan-srzv
Автор

I got :Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value in this line "let entity = "Patient", in: managedContext)!" for some reason

subclavian
welcome to shbcf.ru