Post

Replies

Boosts

Views

Activity

How to check for cancellation of background task
When using the old withTaskCancellationHandler(operation:onCancel:isolation:) to run background tasks, you were notified that the background task gets cancelled via the handler being called. SwiftUI provides the backgroundTask(_:action:) modifier which looks quite handy. However how can I check if the background task will be cancelled to avoid being terminated by the system? I have tried to check that via Task.isCancelled but this always returns false no matter what. Is this not possible when using the modifier in which case I should file a bug report? Thanks for your help
0
0
259
Mar ’25
MusicKit: Best way to check if all tracks of albums are added to library.
I prefer to use the album fetched from the library instead of the catalog since this is faster. If doing so, how can I check if all tracks of an album are added to the library. In this case I'd like to fetch the catalog version or throw an error (for example when offline). Using .with(.tracks) on the library album fetches the tracks added to the library. The trackCount property is referring to the tracks that can be fetched from the library. The isComplete property is always nil when fetching from the library. One possible way is checking the trackNumber and discCount properties. However this only detects that not all tracks of an album are added to the library if there is a song not added ahead of one that is. I'd like to be able to handle this edge case as well. Is there currently a way to do this? I'd prefer to not rely on the apple music catalog for this since this is supposed to work offline as well. Fetching and storing all trackIDs when connected and later comparing against these would work, but this would potentially mean storing tens of thousands of track ids. Thank you
0
0
73
Mar ’25
SwiftData migration crashes when working with relationships
The following complex migration consistently crashes the app with the following error: SwiftData/PersistentModel.swift:726: Fatal error: What kind of backing data is this? SwiftData._KKMDBackingData<SwiftDataMigration.ItemSchemaV1.ItemList> My app relies on a complex migration that involves these optional 1 to n relationships. Theoretically I could not assign the relationships in the willMigrate block but afterwards I am not able to tell which list and items belonged together. Steps to reproduce: Run project Change typealias CurrentSchema to ItemSchemaV2 instead of ItemSchemaV1. Run project again -> App crashes My setup: Xcode Version 16.2 (16C5032a) MacOS Sequoia 15.4 iPhone 12 with 18.3.2 (22D82) Am I doing something wrong or did I stumble upon a bug? I have a demo Xcode project ready but I could not upload it here so I put the code below. Thanks for your help typealias CurrentSchema = ItemSchemaV1 typealias ItemList = CurrentSchema.ItemList typealias Item = CurrentSchema.Item @main struct SwiftDataMigrationApp: App { var sharedModelContainer: ModelContainer = { do { return try ModelContainer(for: ItemList.self, migrationPlan: MigrationPlan.self) } catch { fatalError("Could not create ModelContainer: \(error)") } }() var body: some Scene { WindowGroup { ContentView() } .modelContainer(sharedModelContainer) } } This is the migration plan enum MigrationPlan: SchemaMigrationPlan { static var schemas: [any VersionedSchema.Type] { [ItemSchemaV1.self, ItemSchemaV2.self] } static var stages: [MigrationStage] = [ MigrationStage.custom(fromVersion: ItemSchemaV1.self, toVersion: ItemSchemaV2.self, willMigrate: { context in print("Started migration") let oldlistItems = try context.fetch(FetchDescriptor<ItemSchemaV1.ItemList>()) for list in oldlistItems { let items = list.items.map { ItemSchemaV2.Item(timestamp: $0.timestamp)} let newList = ItemSchemaV2.ItemList(items: items, name: list.name, note: "This is a new property") context.insert(newList) context.delete(list) } try context.save() // Crash indicated here print("Finished willMigrate") }, didMigrate: { context in print("Did migrate successfully") }) ] } The versioned schemas enum ItemSchemaV1: VersionedSchema { static var versionIdentifier = Schema.Version(1, 0, 0) static var models: [any PersistentModel.Type] { [Item.self] } @Model final class Item { var timestamp: Date var list: ItemSchemaV1.ItemList? init(timestamp: Date) { self.timestamp = timestamp } } @Model final class ItemList { @Relationship(deleteRule: .cascade, inverse: \ItemSchemaV1.Item.list) var items: [Item] var name: String init(items: [Item], name: String) { self.items = items self.name = name } } } enum ItemSchemaV2: VersionedSchema { static var versionIdentifier = Schema.Version(2, 0, 0) static var models: [any PersistentModel.Type] { [Item.self] } @Model final class Item { var timestamp: Date var list: ItemSchemaV2.ItemList? init(timestamp: Date) { self.timestamp = timestamp } } @Model final class ItemList { @Relationship(deleteRule: .cascade, inverse: \ItemSchemaV2.Item.list) var items: [Item] var name: String var note: String init(items: [Item], name: String, note: String = "") { self.items = items self.name = name self.note = note } } } Last the ContentView: struct ContentView: View { @Query private var itemLists: [ItemList] var body: some View { NavigationSplitView { List { ForEach(itemLists) { list in NavigationLink { List(list.items) { item in Text(item.timestamp.formatted(date: .abbreviated, time: .complete)) } .navigationTitle(list.name) } label: { Text(list.name) } } } .navigationTitle("Crashing migration demo") .onAppear { if itemLists.isEmpty { for index in 0..<10 { let items = [Item(timestamp: Date.now)] let listItem = ItemList(items: items, name: "List No. \(index)") modelContext.insert(listItem) } try! modelContext.save() } } } detail: { Text("Select an item") } } }
1
1
129
Apr ’25
Strange Live Activity Occurrence Behavior
I am seeing a really weird behavior with Live Activities. The Live Activity is always appearing on the simulator. However the Live Activity is only appearing on my physical device when there is no other widget in the widget bundle shown below. @main struct HoerspielWidgetsBundle: WidgetBundle { var body: some Widget { // Uncomment the line below and the Live Activity will no longer appear // UpNextWidget() PlaybackLiveActivity() } } Annotating that var with @WidgetBundle has no effect. There are no logs indicating an error, the function to request a Live Activity does not throw and the status of the activity is active. Both the widget and the Live Activity are working fine otherwise. NSSupportsLiveActivities is set to true in the correct Info.plist file. I am not running any beta software and the physical iPhone is on the newest version (iOS 18.5). Using the template when adding a new target in Xcode, I was able to set up a similar app where the Live Activity works as expected. I am really at a loss here which additional information I should provide or how this issue can be resolved. Thank you for your help.
2
0
128
May ’25
Load bundle resources in UI Tests
I want to load images from my bundle, which works fine when running the main app. However this does not work when running UI Tests. I read that the test bundle is not the main bundle when running tests. I try loading the bundle via this snippet: let bundle = Bundle(for: Frames_HoerspielUITests.self) This is my test class wrapped these the canImport statements so it can be added to the main app target and used for getting the correct bundle: #if canImport(XCTest) import XCTest final class Frames_HoerspielUITests: XCTestCase { override func setUpWithError() throws { continueAfterFailure = false } override func tearDownWithError() throws { } @MainActor func testExample() throws { let app = XCUIApplication() app.launch() } @MainActor func testLaunchPerformance() throws { measure(metrics: [XCTApplicationLaunchMetric()]) { XCUIApplication().launch() } } } #else final class Frames_HoerspielUITests { } #endif However while this works when running the main app, it still fails in the UI tests. It is a SwiftUI only app. and I can't add the images to the asset catalog because they are referenced from another location. Any ideas? Thank you
1
0
320
Jul ’25
Xcodebuild does not find any simulators
I am trying to integrate fastlane, which relies on xcodebuild. So this is my first experience with xcodebuild. Whenever I run a command to test or build the target in the folder where all the files of the project are stored, the action failes with Unable to find a device matching the provided destination specifier For example I might be running this command: xcodebuild \ -project Hoerspielzentrale.xcodeproj \ -scheme HoerspielzentraleUITests \ -destination 'platform=iOS Simulator,name="Screenshot Simulator”' \ test However the mentioned error is returned: xcodebuild: error: Unable to find a device matching the provided destination specifier: { platform:iOS Simulator, OS:latest, name:"Screenshot Simulator” } The requested device could not be found because no available devices matched the request. I can confirm that the simulator exists, is available and it can be booted. I have tried different simulators, different schemas, referencing them by their identifier, switching Xcode Versions, reset all simulators, derived data and build folder. After the error a complete list of all available destinations can be found, where I can see the simulator I try to use. Available destinations for the "HoerspielzentraleUITests" scheme: { platform:iOS, id:dvtdevice-DVTiPhonePlaceholder-iphoneos:placeholder, name:Any iOS Device } { platform:iOS Simulator, id:dvtdevice-DVTiOSDeviceSimulatorPlaceholder-iphonesimulator:placeholder, name:Any iOS Simulator Device } { platform:iOS Simulator, id:961DC569-9931-419A-B46E-769AAFA73EA2, OS:18.5, name:Screenshot Simulator } { platform:iOS Simulator, id:961DC569-9931-419A-B46E-769AAFA73EA2, OS:18.5, name:Screenshot Simulator } { platform:iOS Simulator, id:5E51FD98-C451-472F-9CDE-08D49E6B737B, OS:18.5, name:Screenshot Simulator Pro } { platform:iOS Simulator, id:5E51FD98-C451-472F-9CDE-08D49E6B737B, OS:18.5, name:Screenshot Simulator Pro } The rest of the list was omitted. I am currently completely stuck. Thank you
1
0
148
Jul ’25
Display .icon files in SwiftUI
Is there a way to display a .icon file in SwiftUI? I want to show the app icon in the app itself but exporting and including the app icon as a PNG feels redundant. This would consume a lot of unnecessary storage especially when including a lot of alternative app icons. There has to be a better way Otherwise I would file a feedback for that Thank you
1
8
215
1w