Post

Replies

Boosts

Views

Activity

Reply to State loops in UIViewRepresentable
MKMapView has different delegate behaviour from normal, it's delegate methods fire when setting its properties. So to workaround that you can disable the delegate when you set the property, e.g. uiView.delegate = nil uiView.centerCoordinate = centerCoordinate uiView.delegate = context.coordinator You also must update the coordinator's parent so it can access the latest binding (which by the way is just a pair of get and set closures): context.coordinator.parent = self Note if you look at PaymentButton.swift in Fruta they set a closure rather than a parent. So if doing it that way you could set the closure nil, set the centerCoordinate then set the closure again.
May ’23
Reply to SwiftUI onAppear called inconsistently based on the presence of listStyle
I just tested the OP's code in Xcode 14.3 and iPhone 14 Pro Simulator running iOS 16.4 and the problem appears to be fixed. I believe the old behaviour was because the onAppear was tied to the underlying UICollectionViewCell and if you are familiar with UIKit those cells are reused for performance reasons and as you scroll they all only appear once. I'd be interested to know what Xcode/iOS version this was changed in. I usually read all the release notes and didn't notice this mentioned. Recorded using Simulator File->Record Screen, stop, right click preview, save as animated GIF.
May ’23
Reply to Displaying Core Data entity in Table (iOS 16)
You can bind the table to the fetch request's sort descriptors there is no need to have additional state, e.g. Table(stats, sortOrder: $stats.sortDescriptors) { When the users clicks on a table column the fetch and the table will update automatically. Unfortunately there is an issue where if the View containing the FetchRequest is re-init then the sortDescriptors get reset to their default. I'm not sure if that is a design flaw in @FetchRequest or we are supposed to make our own @State for the sort in the view above and pass it in as a binding and use the value in the @FetchRequest and using the binding to the binding in the Table, e.g. @Binding var sortDescriptors: [SortDescriptor<Item>] init(sd: Binding<[SortDescriptor<Item>]>) { _items = FetchRequest(sortDescriptors: sd.wrappedValue) _sortDescriptors = sd } ... Table(stats, sortOrder: $sortDescriptors) {
Jan ’23
Reply to How to sort SwiftUI Table with Data from Core Data FetchResult
There is no extra state for the sort required, you can bind directly to the fetch's sort descriptors, e.g. Table(clubs, selection: $selectedClubs, sortOrder: $clubs.sortDescriptors) { The results and the table will update automatically. Unfortunately there is an issue where if the View containing the FetchRequest is re-init then the sortDescriptors get reset to their default. I'm not sure if that is a design flaw in FetchRequest or we are supposed to make our own @State for the sort in the view above and pass it in as a binding and use the value in the FetchRequest and using the binding to the binding in the Table, e.g. @Binding var sortDescriptors: [SortDescriptor<Item>] init(sd: Binding<[SortDescriptor<Item>]>) { _items = FetchRequest(sortDescriptors: sd.wrappedValue) _sortDescriptors = sd }
Jan ’23
Reply to Seemingly incorrect data when navigationDestination closure runs.
This is a very common problem experienced by those new to Swift and haven't fully learned closures yet. Usually its experienced with sheet but its same thing with navigationDestination. If you want a closure to have the latest value you need to add it to the "capture list", e.g. .navigationDestination(for: Int.self) { [data] index in The explanation is when the closure is created it is using the old value, which is before it is actually used, so by adding it to the capture list you are stating you want the closure to be recreated when that value changes. As for why the closure is called 3 times, it is annoying but if you take a look at how many times the ForEach closure is seemingly needlessly called that is even worse!
Nov ’22
Reply to iOS 16 crash on dismiss sheet with navigationView
I just don't think SwiftUI's state-driven design will ever be able to reliably manipulate UIKit's event-driven navigation components like UINavigationController and UISplitViewController. Especially because these have some of their own adaptive behaviour that will undermine the SwiftUI state. We probably need new versions of these components with all their hacks removed.
Nov ’22
Reply to Web Push and Push notifications
I was wondering the same thing. Also when watching the video it when he kept saying "it really is push" it seemed to me it isn't push its just for user notification. If it was real push then we could use it for async updates to data shown on a webpage, e.g. like how we use silent push on iphone to trigger a sync. He said Safari doesn't support silent push!
Jul ’22
Reply to multiple background requests
Use one session, according to Use Background Sessions Efficiently I noticed if you init a session with a config with the same identifier you will actually get the previous instance. I assume you have to set a taskDescription on the task to be able to identify it in .backgroundTask() on the Scene, where presumably you have to retrieve all the completed tasks from the session and deal with the results.
Jun ’22
Reply to SwiftUI EditButton problem in Xcode 12 beta
The problem is ForEach with id: \.self for a dynamic array of value types is a major mistake unfortunately made by many developers, anyone know why? The documentation states that id needs to be "The key path to the provided data’s identifier." E.g. id: \.uniqueIdentifier, or preferably conform your model struct to Identifiable and implement either let id = UUID() or var id: String { return a calculated id from other vars }
Apr ’22