SwiftUI TabView with List not refreshing after objected deleted from / added to Core Data

谁都会走 提交于 2020-07-08 00:34:03

问题


Description:

When an object in a list (created from a fetchrequest) is deleted from a context, and the context is saved, the list does not properly update.

Error:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value (Thrown on line 5 below)

struct DetailView: View {  
    @ObservedObject var event: Event  

    var body: some View {  
        Text("\(event.timestamp!, formatter: dateFormatter)")  
            .navigationBarTitle(Text("Detail"))  
    }  
}  

Steps to reproduce:

  1. Create a new Master Detail App project with SwiftUI and Core Data.

  2. In the ContentView, set the body to a TabView with the first tab being the prebuilt NavigationView, and add a second arbitrary tab.

struct ContentView: View {  
    @Environment(\.managedObjectContext)  
    var viewContext     

    var body: some View {  
        TabView {  
            NavigationView {  
                MasterView()  
                    .navigationBarTitle(Text("Master"))  
                    .navigationBarItems(  
                        leading: EditButton(),  
                        trailing: Button(  
                            action: {  
                                withAnimation { Event.create(in: self.viewContext) }  
                        }  
                        ) {  
                            Image(systemName: "plus")  
                        }  
                )  
                Text("Detail view content goes here")  
                    .navigationBarTitle(Text("Detail"))  
            }  
            .navigationViewStyle(DoubleColumnNavigationViewStyle())  
            .tabItem { Text("Main") }  

            Text("Other Tab")  
                .tabItem { Text("Other Tab") }  
        }  
    }  
}  
  1. Add a few items. Interact with those items in any way.
  2. Change tabs.
  3. Change back to Main Tab.
  4. Attempt to delete an item.

回答1:


I found a pure SwiftUI working solution:

/// This View that init the content view when selection match tag.
struct SyncView<Content: View>: View {

    @Binding var selection: Int

    var tag: Int

    var content: () -> Content

    @ViewBuilder
    var body: some View {
        if selection == tag {
            content()
        } else {
            Spacer()
        }
    }
}

You can use it then in this way:

struct ContentView: View {  
    @State private var selection = 0  

    var body: some View {  
        TabView(selection: $selection) {  

            SyncView(selection: $selection, tag: 0) {  
                ViewThatNeedsRefresh()  
            }  
            .tabItem { Text("First") }  
            .tag(0)  

            Text("Second View")  
                .font(.title)  
                .tabItem { Text("Second") }  
                .tag(1)  
        }  
    }  
}  

You can use the SyncView for each view that needs a refresh.



来源:https://stackoverflow.com/questions/60859895/swiftui-tabview-with-list-not-refreshing-after-objected-deleted-from-added-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!