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.
Topic: UI Frameworks SubTopic: UIKit Tags:
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.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
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) {
Topic: UI Frameworks SubTopic: SwiftUI Tags:
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 }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
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!
Topic: UI Frameworks SubTopic: SwiftUI Tags:
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.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
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.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
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 }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Apr ’22
Reply to "Compiler" card persists even after app code changed to use DocumentGroup
Remove .modelContainer(for: Card.self) the container for the document is set automatically by DocumentGroup.
Replies
Boosts
Views
Activity
Jun ’23
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.
Topic: UI Frameworks SubTopic: UIKit Tags:
Replies
Boosts
Views
Activity
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.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’23
Reply to Sample code doesn't compile as contextAction has been replaced
The sample was updated on 1st March 2023 to fix this and many other problems. If you download the sample again and drag the extracted folder into a Git app like Tower you can see all the changes (screenshot below). The commit message was "Updated the project sourcefor compatibility with macOS 13."
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’23
Reply to Is com.apple.AMPLibrayAgent not used by Apple Music.app?
com.apple.Music.plist library-url = file:///Users/username/Music/Music/Music%20Library.musiclibrary/
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
Mar ’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) {
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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 }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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!
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Nov ’22
Reply to NSBatchInsertRequest with managedObjectHandler not doing anything on macOS
I was trying the dictionaryHandler version on iOS and it doesn't insert anything and no SQL debug output about the insert. The init that takes dictionaries does work though.
Topic: App & System Services SubTopic: Core OS Tags:
Replies
Boosts
Views
Activity
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.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Nov ’22
Reply to Swift 2 kUTTypeMovie?
iOS 14.0+ import UniformTypeIdentifiers imagePickerController.mediaTypes = [UTType.movie.identifier]
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
Sep ’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!
Replies
Boosts
Views
Activity
Jul ’22
Reply to modifyRecordsCompletionBlock deprecated on iOS 15
I think we have to keep using modifyRecordsCompletionBlock because modifyRecordsResultBlock doesn't error on server record changed, it erroneously reports success. As of Xcode 14b1 iOS 16b1. See this post for more info.
Replies
Boosts
Views
Activity
Jun ’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.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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 }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Apr ’22