Post

Replies

Boosts

Views

Activity

GeometryReader cause FetchedResults object's view not updated after object's value changed.
Steps to reproduce: click any item in the list to get into its detail view click the + button to increase the count value of the item go back to the item list [ISSUE 1] the count value of the item is not updated in the list view now click the item you just operated to go back its detail view [ISSUE 2] the item's draft value is reverted back to the origin value (Or you can say it's the value displayed in the list view) If you remove the GeometryReader, it works normally, without that 2 issues. The Result I want: The list view's count value updated after going back to the list view. The draft value should be the same as the item at second entry after updating count. import SwiftUI import CoreData struct ContentView: View { @Environment(\.managedObjectContext) private var viewContext @FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)], animation: .default) private var items: FetchedResults<Item> var body: some View { NavigationView { List { ForEach(items) { item in // Not Work GeometryReader { geo in NavigationLink(destination: SubView(item: item)) { HStack { Text("\(item.timestamp!.ISO8601Format())") Spacer(minLength: 0) Text(String(item.count)) } } } // Works // NavigationLink(destination: SubView(item: item)) { // HStack { // Text("\(item.timestamp!.ISO8601Format())") // Spacer(minLength: 0) // Text(String(item.count)) // } // } } } } } } struct SubView: View { @ObservedObject var item: Item @State private var draft: TmpItem init(item: Item) { self.item = item self._draft = State(wrappedValue: TmpItem(count: Int(item.count))) } var body: some View { VStack { Text("\(item.timestamp!.ISO8601Format())") Text("item: \(item.count)") Text("draft: \(draft.count)") Button("+") { draft.count += 1 item.count = Int16(draft.count) try! viewContext.save() } .buttonStyle(.borderedProminent) } } @Environment(\.managedObjectContext) private var viewContext } struct TmpItem { var count: Int } #Preview { ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) } It's base on the defualt demo code from Xcode 15.1.0 for iOS app with core data. Added a count field (Int16) to the Item object.
1
0
316
Dec ’23
Double sheet can not be dismissed
There is a constructure using double sheet in my app, which open a sheet to edit a item(depth 1), and it can open another sheet to edit the field in the item (depth 2). In my case, I have encountered a bug that if I go down to the second sheet(depth 2), and then I dismiss the second sheet(depth 2), then I can't dismiss the first sheet(depth 1) here is the sample code, I test it in Xcode 14.1 RC2 and ios16.1 on both simulator and physical devices. import SwiftUI import CoreData struct ContentView: View { @State private var isSubPresented = false @Environment(\.dismiss) var dismiss var body: some View { Text("Main") .sheet(isPresented: $isSubPresented) { VStack { SubView() Button("dismiss") { // dismiss() // not work at all isSubPresented = false } .padding() } } .onTapGesture { self.isSubPresented = true } } } struct SubView: View { @State private var isPresented: Bool = false @Environment(\.dismiss) var dismiss var body: some View { Button("into Sub edit") { self.isPresented = true } .sheet(isPresented: $isPresented) { Form { Button("dismiss") { dismiss() } } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext) } }
1
0
359
Oct ’22
refreshable is passed to sub Form unexpectedly
I set up a List of item, and a sheet for each entry in the list to display an editing form when I tap on the entry of the list. Then I added the refreshable modifier on the list, and the List now can be pull to refresh, that's good. But I found the Form in the sheet of the entry now also can be pull to refresh, which is not what I want. Is it a bug or did I get it wrong? And also, how can I make the Form not refreshable, because there is not a modifier like .refreshable(false) . Thanks in advance. here is a sample code, I test it on xcode 14.1 RC and iOS 16.1 (20B72) on 14 Pro Max simulator. import SwiftUI struct ContentView: View {   var body: some View {     NavigationView {       List(1..<10) { row in         SubView(label: "Row \(row)")       }       .refreshable {       }     }   } } struct SubView: View {   var label: String       @State private var isPresented: Bool = false   var body: some View {     Text(label)       .onTapGesture {         self.isPresented = true       }       .sheet(isPresented: $isPresented) {         Form {           Text(label)         }       }   } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }
0
0
427
Oct ’22