Post

Replies

Boosts

Views

Activity

How to sort my Core Data entities using the new query properties from AppIntents
TL;DR How to create an NSSortDescriptor using a PartialKeyPath instead of a KeyPath? Or How to convert a PartialKeyPath to a KeyPath? Details I'm using the new query properties from AppIntents to filter my app's data in Shortcuts, but I'm unable to map the sorting attribute it gives me to the sorting attribute Core Data expects in the predicate. Here's my EntityPropertyQuery implementation: extension ArtistQuery: EntityPropertyQuery { static var sortingOptions = SortingOptions { SortableBy(\ArtistEntity.$name) } static var properties = QueryProperties { Property(\ArtistEntity.$name) { EqualToComparator { NSPredicate(format: "name = %@", $0) } ContainsComparator { NSPredicate(format: "name CONTAINS %@", $0) } } } func entities(matching comparators: [NSPredicate], mode: ComparatorMode, sortedBy: [Sort<ArtistEntity>], limit: Int?) async throws -> [ArtistEntity] { Database.shared.findArtists(matching: comparators, matchAll: mode == .and, sorts: sortedBy.map { NSSortDescriptor(keyPath: $0.by, ascending: $0.order == .ascending) }) } } And my findArtists method is implemented as follows: static func findArtists(matching comparators: [NSPredicate], matchAll: Bool, sorts: [NSSortDescriptor]) -> [EArtist] { ... } As we can see in the entities(matching:) function, I'm using the by attribute from the sortedBy parameter to create the NSSortDescriptor, but it doesn't work because the NSSortDescriptor init expects a KeyPath, not a PartialKeyPath: Cannot convert value of type 'PartialKeyPath<ArtistEntity>' to expected argument type 'KeyPath<Root, Value>' So, can I create an NSSortDescriptor using a PartialKeyPath instead of a KeyPath? Or maybe converting a PartialKeyPath to a KeyPath?
1
1
1.7k
Aug ’22
Opening a Safari URL from an interactive widget with AppIntents
My app allows users to store URLs for future reference. It also has widgets that display these saved items. I want to make these widgets interactive by opening the linked content in Safari without having to open the main app. The issue is that UIApplication.shared.open is not available in app extensions. And this is a problem since widgets are app extensions. Are there alternative methods to accomplish this scenario, or is it currently unsupported?
1
1
1.5k
Jul ’23
SwiftUI: unexpected behavior with two List side by side and a NavigationView
I have a screen with two List side by side, inside a NavigationView. The layout is rendered correctly, and I can independently scroll both lists. The problem is that, when I scroll the first list, it goes behind the navigation bar without triggering the effect of applying a background color to it. Here's a gif showing what's going on: And this is the code I'm using for this view: struct ContentView: View { var body: some View { NavigationView { HStack(spacing: 0) { List { Section(header: Text("Header left")) { ForEach(0..<600) { integer in Text("\(integer)") } } } .listStyle(InsetGroupedListStyle()) .frame(minWidth: 0, maxWidth: .infinity) List { Section(header: Text("Header right")) { ForEach(0..<400) { integer in Text("\(integer)") } } } .listStyle(InsetGroupedListStyle()) .frame(minWidth: 0, maxWidth: .infinity) } .navigationTitle("Example") } .navigationViewStyle(StackNavigationViewStyle()) } } Would this be a SwiftUI bug? If not, how can I make the first list correctly interact with the navigation bar when scrolling?
0
0
911
Aug ’21
How to reload widgets from the share sheet, via an Action Extension
My app has an action extension that allow users to collect and save links throughout the system. It also has a widget to show these links in the Home Screen. When adding links from the main app, I call WidgetCenter.shared.reloadAllTimelines() to reload all widgets, and everything works as expected. But when I add links from the action extension, widgets are not reloaded, even after calling WidgetCenter.shared.reloadAllTimelines(). Only when I go to the main app is that widgets do reload. How can I refresh my widgets for changes made via the Action Extension from the share sheet?
0
0
863
Dec ’21
SwiftUI: How to change List selected item color
iPadOS uses a different selection color when an external keyboard is connected. But the problem is that it doesn't change the text color to white, making it difficult to read: A simple List with NavigationLink produces this behavior by default: var body: some View { List { ForEach(searchResults) { item in NavigationLink(destination: ContentDetailView(item: item)) { ListItemView(item: item) } } } } I tried to improve text legibility by changing all Text colors to white when the cell is selected. But this doesn't work because the text becomes even more unreadable when no external keyboard is connected. Is there a way to change the selection color when an external keyboard is connected? Or maybe detect when an external keyboard is connected to manually change the text color for this specific case?
0
1
1.4k
Mar ’22
Shortcuts editor doesn't recognize boolean EntityQueryProperty
My AppEntity has a Bool property, and I would like to query by this property using EntityPropertyQuery. I'm creating an EntityQueryProperty for it with an EqualToComparator as the following (partial code for brevity): // My AppEntity struct AlbumAppEntity: AppEntity, Identifiable { @Property(title: "Hidden") var isHidden: Bool // Other properties } // My EntityPropertyQuery static var properties = QueryProperties { Property(\AlbumAppEntity.$isHidden) { EqualToComparator { isHidden in if isHidden { return NSPredicate(format: "isHidden == YES") } else { return NSCompoundPredicate(type: .or, subpredicates: [NSPredicate(format: "isHidden = nil"), NSPredicate(format: "isHidden == NO")]) } } } } But the problem is that Shortcuts does not recognize it as a boolean parameter in the shortcuts editor. It recognizes it as a numeric field, which returns the error AppIntents.EntityPropertyQueryError error 2 when run: I also tried different NSPredicate, but the result is always the same: Property(\AlbumAppEntity.$isHidden) { EqualToComparator { NSPredicate(format: "isHidden == %d", $0) } } Property(\AlbumAppEntity.$isHidden) { EqualToComparator { NSPredicate(format: "isHidden == %@", NSNumber(value: $0)) } } How can I query using boolean properties in a way that Shortcuts recognizes it and provides appropriate fields in the shortcuts editor?
0
1
1.3k
Aug ’22
'Runs as Current User' on tvOS causes Core Data to reset
My app uses Core Data to store user data. It doesn't have login or profile mechanisms, because user data is tied to the user's Apple ID and synced via iCloud using CloudKit. I've recently added the User Management capability, and checked Runs as Current User to allow each Apple TV user to have access to its own data. Everything works as expected: I press and hold the TV button to select a different user in Control Center; The system shows a switching user UI; And the app is relaunched using the new user's Apple ID and data from iCloud. The problem is that switching users apparently causes the local Core Data managed database to be reset, removing all data from the previous user. This is inconvenient because every time we switch users, the newly selected user has to wait for CloudKit to download all its data back into the app. And it takes a while, especially for users that have more than a few hundred items in the database. Ideally, tvOS should maintain the user data on-device, and only switch to a different database according to the active user. This way data would not need to be downloaded from scratch every time a user is switched. Is there a setting I can configure or a checkbox I can check to enable this behavior, or is this something not possible yet on tvOS?
0
0
629
Dec ’23
macOS Sequoia: “App” would like to access data from other apps when launched from Xcode
Since updating to macOS Sequoia, I see this dialog every time I launch my SwiftUI macOS app from Xcode: Users who installed the app from the App Store don’t see it. And this didn’t happened in previous macOS versions. Could launching it from Xcode be triggering some extra access requirement? How can I stop this dialog from appearing every time I launch my app? It’s very disruptive to the debugging process.
0
0
474
Nov ’24