SwiftUI / CoreData / Master / Detail (with editing) / Xcode 11 - Beta 5

swiftui -- core data
swiftui update core data
swiftui core data github
swiftui core data manager
swiftui core data onmove
swiftui mvvm core data
swiftui database

Working on a sample app. The goal is to have a list pulled up from CoreData in a Master, and then click on one to go to a detail, where you can edit the information and save. When you edit the "name" in the detail, it not only updates the detail to reflect the change, but it also reflects the change on the master as well. I've tried numerous ways to accomplish this, but so far have not come up with an answer.

// Code generation is turned OFF in the xcdatamodeld file

public class EntityName: NSManagedObject, Identifiable {
   @NSManaged public var name: String
   @NSManaged public var active: Bool
}

extension EntityName {
    static func allEntityNameFetchRequest() -> NSFetchRequest<EntityName> {
        let request: NSFetchRequest<EntityName> = EntityName.fetchRequest() as! NSFetchRequest<EntityName>
        request.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true)]
        return request
   }
}


struct MasterView: View {

    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(fetchRequest: EntityName.allEntityNameFetchRequest()) var allEntityNames: FetchedResults<EntityName>


    var body: some View {
        NavigationView {
            List {
                ForEach(self.allEntityNames) { entityName in
                NavigationLink(destination: DetailView(entityName: entityName)) {
                    VStack(alignment: .leading) {
                        Text(entityName.name)
                            .font(.headline)
                        Text(String(entityName.active))
                            .font(.subheadline)
                    }
                }
            }
        }
    }
    .onAppear() {
        // Just want to populate the Core Data to have a few to work with
        if self.allEntityNames.count == 0 {
            for _ in 1...3 {
                let newEntry = EntityName(context: self.managedObjectContext)
                newEntry.name = "New Entry"

                try! self.managedObjectContext.save()
            }
         }
      }
   }
}

struct DetailView: View {

   var entityName = EntityName()

   var body: some View {
       VStack {
           Text("Name: \(entityName.name)")
           Text("Active: \(String(entityName.active))")

           // What I'd like to do now:
              //TextField("", text: $entityName.name)
              //Toggle(isOn: $entityName.active)
       }
   }
}

As promised, here is a link to sample SwiftUI program that uses CoreData. The current version compiles and runs on the GM release. It does not use the new @FetchRequest property wrapper because it does not suit all of my needs. I wrote a CoreDataDataSource class that does basically the same thing plus a lot more.

https://github.com/Whiffer/SwiftUI-Core-Data-Test

SwiftUI Editing the information in the Detail of a Master/Detail, The goal is to have a list pulled up from CoreData in a MasterView, and then click on one to go to a DetailView where you can edit the information and save. When​  In this Core Data with SwiftUI tutorial, you’ll refactor an app to add persistence and prevent the nightmare of losing your data when the app restarts. Along the way, you’ll learn to: Set up Core Data in a project. Use SwiftUI’s data flow to access what you need in the Core Data framework. Define and create new model objects using Core Data.


You need to bind the entity in DetailView to the entity passed in from MasterView. Declare it like @Binding var entityName: EntityName

Xcode 11 Release Notes, Xcode 11 is available in the Mac App Store and includes SDKs for iOS 13, macOS SwiftUI previews and inspectors are only available when running on macOS The Core Data data model editor has added support for derived attributes when details about constraints including a filter, hover highlighting in the editor, and  Learn SwiftUI, Dark Mode, Sign in with Apple, Swift Package Manager and SF Symbols on the new Xcode 11 and Swift 5.1 4.6 (433 ratings) Course Ratings are calculated from individual students’ ratings and a variety of other signals, like age of rating and reliability, to ensure that they reflect course quality fairly and accurately.


As @trapper explained, the entityName has to be bound the the MasterView.

Updates are reflected when you add @ObservedObject in the DetailView as follows:

@ObservedObject var entityName:EntityName

Using Core Data With SwiftUI – An Introduction – Slacker News, In the release notes for iOS Beta 5, Apple seems to be leading us there: NSManagedObject now conforms to ObservableObject . The new @  SwiftUI tables require a binding to an array storing the entirety of your model objects in memory. For small datasets, the tradeoff of convenience for performance makes sense. But for datasets with tens/hundreds of thousands of values, the old-school approach to rendering tables through queries to a datasource still seems like the way to go.


Getting Started with Core Data Tutorial, In Xcode 10, only the Master-Detail App and Single View App templates have the Use Core Data checkbox. The idea for this sample app is  EDIT: Apparently it says Beta only in the offline docs on Xcode 11. And so, you need to make sure … the compiler doesn't hit this code … when you are working in Xcode. Add Progress View, Label, Button. To use the SwiftUI design canvas Xcode 11 must running on macOS 10.


Multiple Managed Object Contexts with Core Data Tutorial , If common Core Data phrases such as managed object subclass and persistent such as swell height or period, and rate the session from 1 to 5. the entry in edit mode, where you can change or view the details of a surf session. measure your app's performance with various Xcode tools and then pick  This answer seems to work in my recent project by replacing the default ContentView_Previews struct, though others are questioning whether it pulls persistent data. Credit goes to @ShadowDES - in the Master/Detail template project in Xcode Beta 7. I'm able to CRUD anything using Canvas (XCode Version 11.3 (11C29)) and it seems to run flawlessly.


Getting Started With NSPersistentCloudKitContainer, While Xcode auto-generates the Core Data + CloudKit stack for you, it does not enable iCloud for you. To enable 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 In Xcode 10, only the Master-Detail App and Single View App templates have the Use Core Data checkbox. The idea for this sample app is simple: There will be a table view with a list of names for your very own “hit list”.