SwiftUI/CoreData/Master/Detail(带编辑)/Xcode 11 - Beta 5 [英] SwiftUI / CoreData / Master / Detail (with editing) / Xcode 11 - Beta 5

查看:25
本文介绍了SwiftUI/CoreData/Master/Detail(带编辑)/Xcode 11 - Beta 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在开发示例应用.目标是从 Master 中的 CoreData 中提取一个列表,然后单击一个列表以转到详细信息,您可以在其中编辑信息并保存.当您在细节中编辑名称"时,它不仅会更新细节以反映更改,而且还会反映母版上的更改.我尝试了很多方法来实现这一点,但到目前为止还没有找到答案.

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)
       }
   }
}

推荐答案

正如所承诺的,这里是使用 CoreData 的示例 SwiftUI 程序的链接.当前版本在 GM 版本上编译和运行.它不使用新的 @FetchRequest 属性包装器,因为它不能满足我的所有需求.我写了一个 CoreDataDataSource 类,它做的事情基本上是一样的,而且还有很多.

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/CoreData/Master/Detail(带编辑)/Xcode 11 - Beta 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆