Post

Replies

Boosts

Views

Activity

Widget and Core Data with CloudKit
I have two apps that use App Groups, CloudKit and Core Data for sharing data between multiple devices and users (one is a sample for the issue, the other one the full fledged one). In the sample app I have the following model: Collection (name) -> many Items (names and count) and corresponding views to increment the count. I also have the widget share the proper App Group and CloudKit container. It has a button that triggers an increment app intent. A create a shared collection "Shares" and add two items in it - "A" & "B". Sharing from the applications and updates of the count takes about 2 seconds. I.e. I open the item view and I tap an increment button, the second phone screen updates almost immediately. Upon each update I also call WidgetCenter.shared.reloadAllTimelines(). What I see is that widgets have are not in sync with the data of the app. I have an explicit button to trigger the reload of the timelines, I can follow along with the debugger and the Core Data data is different, i.e. different count. I also show a date property, so I know that the widget is redrawn. I think that as soon as I added the target for the widget and added it to the app group I started seeing: BUG IN CLOUDKIT: Error submitting background task request: Error Domain=BGSystemTaskSchedulerErrorDomain Code=3 "(null)". The sync between the widget and the app on the same device is inconsistent. I also see a lot of when interactions happen fast. submitTaskRequest failed for com.apple.coredata.cloudkit.activity.import. submitTaskRequest failed for com.apple.coredata.cloudkit.activity.export. I least I hope is that after the app has synchronised, the widget will catch-up and show the same data - i.e. at least the same device will show data consistently. This does not happen though. Even on the device on which I'm doing the changes, and tapping multiple times the manual reload of widgets button, the Core Data is stale, and the time of reload of the widget is fine. The relevant threads this I've found are (none of them can shed any light): https://developer.apple.com/forums/thread/653112?answerId=697974022 https://developer.apple.com/forums/thread/650192?answerId=614112022#614112022 https://developer.apple.com/forums/thread/651648?answerId=827386022 I have tested with iOS 26.5.2 and iOS 27 Beta. The widget is it out sync
2
0
674
Jul ’26
iOS Simulator downloads a lot of data
iOS simulator downloads loads of data even over personal hotspots that should be low data - 10s of GBs which exhausts cellular plans. I.e. I'm connecting the phone over USB (charging and it automatically shares the cellular connection and faster deploy and debugging). I see that /Library/Developer/CoreSimulator/Volumes/iOS_23F77/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 26.5.simruntime/Contents/Resources/RuntimeRoot/usr/libexec/nsurlsessiond downloads 10s of GBs probably in /Library/Developer/Xcode/UserData/Previews/Simulator Devices/<UUID>/data/private/var/MobileAsset/AssetsV2 over time. Most plans are not that infinite. Has anyone seen such thing? The phone is connected over USB and automatically used as network device and also has the low data mode toggle on.
0
0
127
Jun ’26
Sample Code with Swift 6
I find these sample projects quite valuable: https://developer.apple.com/documentation/widgetkit/emoji-rangers-supporting-live-activities-interactivity-and-animations https://developer.apple.com/documentation/coredata/sharing-core-data-objects-between-icloud-users . Both use Swift 5, and it is not trivial to adopt Swift 6 with them. Any plans to update them? What is best approach for adopting Swift 6 on such sample code?
5
0
1.2k
Jun ’26
Running with CloudKit and Swift 6
Hello, everyone, I've started a thread and got a recommendation from DTS Engineer to move the conversation here. The sample code for https://developer.apple.com/documentation/coredata/sharing-core-data-objects-between-icloud-users is with Swift 5. I have a Swift 6 project and I'm running into issue porting the sample code. I've found that NSManagedObjectContext is sendable (https://fatbobman.com/en/posts/sendable-nsmanagedobjectcontext/). From it I can get the persistence controller, and from it I get the stores and this solves the Swift 6 migration path and looks good. i.e. not rely on the private and shared references to the persistence stores, I'll be getting them from the context. I've also notices that once I start to change the PersistenceController from lazy vars for lazy var previewContainer: NSPersistentCloudKitContainer and lazy var persistentContainer: NSPersistentCloudKitContainer to struct PersistenceController { static let shared = PersistenceController() @MainActor static let preview: PersistenceController = { let result = PersistenceController(inMemory: true) which is the default pattern for new CoreData applications (Xcode 26 / Xcode 27) I start getting preview crashes similar to https://developer.apple.com/forums/thread/654126. It turns out this is because of let persistenceController = PersistenceController.shared in the App. Quite unexpected. The persistence controller looks more suitable for actor based on the CloudKit sharing requirements. I saw such question on WWDC 26, however there was not enough time for an answer - is a good pattern to have the PersistenceController as an actor or in another way. Which is that most swiftly way for Swift 6?
0
0
524
Jun ’26
CloudKit, CoreData and Swift 6 for sharing between users
I have started from here: Apple's guide on the sharing core data objects between iCloud users and I have created a sample project that has Collections and Items. Everything works great while I stay on Swift 5, like with the initial project. I would like to migrate to Swift 6 (Default Actor Isolaton @MainActor, Approachable Concurrency: Yes) on the project and I am stuck at extension CDCollection: Transferable { ... }. When compiling with Swift 5, there is a warning: Conformance of 'NSManagedObject' to 'Sendable' is unavailable in iOS; this is an error in the Swift 6 language mode. After resolving almost all compile-time warnings I'm left with: Conformance of 'CDCollection' to protocol 'Transferable' crosses into main actor-isolated code and can cause data races. Which I don't think will work, because of the warning shown above. It can be worked around like: nonisolated extension CDCollection: Transferable, @unchecked Sendable Then there are errors: let persistentContainer = PersistenceController.shared.persistentContainer Main actor-isolated static property 'shared' can not be referenced from a nonisolated context. I've created the following class to have a Sendable object: struct CDCollectionTransferable: Transferable { var objectID: NSManagedObjectID var persistentContainer: NSPersistentCloudKitContainer public static var transferRepresentation: some TransferRepresentation { CKShareTransferRepresentation { collectionToExport in let persistentContainer = collectionToExport.persistentContainer let ckContainer = CloudKitProvider.container var collectionShare: CKShare? if let shareSet = try? persistentContainer.fetchShares( matching: [collectionToExport.objectID]), let (_, share) = shareSet.first { collectionShare = share } /** Return the existing share if the collection already has a share. */ if let share = collectionShare { return .existing(share, container: ckContainer) } /** Otherwise, create a new share for the collection and return it. Use uriRepresentation of the object in the Sendable closure. */ let collectionURI = collectionToExport.objectID .uriRepresentation() return .prepareShare(container: ckContainer) { let collection = await persistentContainer.viewContext .perform { let coordinator = persistentContainer.viewContext .persistentStoreCoordinator guard let objectID = coordinator?.managedObjectID( forURIRepresentation: collectionURI ) else { fatalError( "Failed to return the managed objectID for: \(collectionURI)." ) } return persistentContainer.viewContext.object( with: objectID ) } let (_, share, _) = try await persistentContainer.share( [collection], to: nil ) return share } } } } And I'm able to compile and run the app with this change: let transferable = CDCollectionTransferable( objectID: collection.objectID, persistentContainer: PersistenceController.shared .persistentContainer ) ToolbarItem { ShareLink( item: transferable, preview: SharePreview("Share \(collection.name)!") ) { MenuButtonLabel( title: "New Share", systemImage: "square.and.arrow.up" ) } } The app crashes when launched with libdispatch.dylib`_dispatch_assert_queue_fail: 0x1052c6ea4 <+0>: sub sp, sp, #0x50 0x1052c6ea8 <+4>: stp x20, x19, [sp, #0x30] 0x1052c6eac <+8>: stp x29, x30, [sp, #0x40] 0x1052c6eb0 <+12>: add x29, sp, #0x40 0x1052c6eb4 <+16>: adrp x8, 63 0x1052c6eb8 <+20>: add x8, x8, #0xa0c ; "not " 0x1052c6ebc <+24>: adrp x9, 62 0x1052c6ec0 <+28>: add x9, x9, #0x1e5 ; "" 0x1052c6ec4 <+32>: stur xzr, [x29, #-0x18] 0x1052c6ec8 <+36>: cmp w1, #0x0 0x1052c6ecc <+40>: csel x8, x9, x8, ne 0x1052c6ed0 <+44>: ldr x10, [x0, #0x48] 0x1052c6ed4 <+48>: cmp x10, #0x0 0x1052c6ed8 <+52>: csel x9, x9, x10, eq 0x1052c6edc <+56>: stp x9, x0, [sp, #0x10] 0x1052c6ee0 <+60>: adrp x9, 63 0x1052c6ee4 <+64>: add x9, x9, #0x9db ; "BUG IN CLIENT OF LIBDISPATCH: Assertion failed: " 0x1052c6ee8 <+68>: stp x9, x8, [sp] 0x1052c6eec <+72>: adrp x1, 63 0x1052c6ef0 <+76>: add x1, x1, #0x9a6 ; "%sBlock was %sexpected to execute on queue [%s (%p)]" 0x1052c6ef4 <+80>: sub x0, x29, #0x18 0x1052c6ef8 <+84>: bl 0x105301b18 ; symbol stub for: asprintf 0x1052c6efc <+88>: ldur x19, [x29, #-0x18] 0x1052c6f00 <+92>: str x19, [sp] 0x1052c6f04 <+96>: adrp x0, 63 0x1052c6f08 <+100>: add x0, x0, #0xa11 ; "%s" 0x1052c6f0c <+104>: bl 0x1052f9ef8 ; _dispatch_log 0x1052c6f10 <+108>: adrp x8, 95 0x1052c6f14 <+112>: str x19, [x8, #0x1f0] -> 0x1052c6f18 <+116>: brk #0x1 The app still crashes when I comment this code, and all Core Data related warnings. I'm quite stuck now as I want to use Swift 6. Has anyone figured CloudKit, CoreData and Swift 6 for sharing between users?
1
0
337
Feb ’26
Widget and Core Data with CloudKit
I have two apps that use App Groups, CloudKit and Core Data for sharing data between multiple devices and users (one is a sample for the issue, the other one the full fledged one). In the sample app I have the following model: Collection (name) -> many Items (names and count) and corresponding views to increment the count. I also have the widget share the proper App Group and CloudKit container. It has a button that triggers an increment app intent. A create a shared collection "Shares" and add two items in it - "A" & "B". Sharing from the applications and updates of the count takes about 2 seconds. I.e. I open the item view and I tap an increment button, the second phone screen updates almost immediately. Upon each update I also call WidgetCenter.shared.reloadAllTimelines(). What I see is that widgets have are not in sync with the data of the app. I have an explicit button to trigger the reload of the timelines, I can follow along with the debugger and the Core Data data is different, i.e. different count. I also show a date property, so I know that the widget is redrawn. I think that as soon as I added the target for the widget and added it to the app group I started seeing: BUG IN CLOUDKIT: Error submitting background task request: Error Domain=BGSystemTaskSchedulerErrorDomain Code=3 "(null)". The sync between the widget and the app on the same device is inconsistent. I also see a lot of when interactions happen fast. submitTaskRequest failed for com.apple.coredata.cloudkit.activity.import. submitTaskRequest failed for com.apple.coredata.cloudkit.activity.export. I least I hope is that after the app has synchronised, the widget will catch-up and show the same data - i.e. at least the same device will show data consistently. This does not happen though. Even on the device on which I'm doing the changes, and tapping multiple times the manual reload of widgets button, the Core Data is stale, and the time of reload of the widget is fine. The relevant threads this I've found are (none of them can shed any light): https://developer.apple.com/forums/thread/653112?answerId=697974022 https://developer.apple.com/forums/thread/650192?answerId=614112022#614112022 https://developer.apple.com/forums/thread/651648?answerId=827386022 I have tested with iOS 26.5.2 and iOS 27 Beta. The widget is it out sync
Replies
2
Boosts
0
Views
674
Activity
Jul ’26
iOS Simulator downloads a lot of data
iOS simulator downloads loads of data even over personal hotspots that should be low data - 10s of GBs which exhausts cellular plans. I.e. I'm connecting the phone over USB (charging and it automatically shares the cellular connection and faster deploy and debugging). I see that /Library/Developer/CoreSimulator/Volumes/iOS_23F77/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 26.5.simruntime/Contents/Resources/RuntimeRoot/usr/libexec/nsurlsessiond downloads 10s of GBs probably in /Library/Developer/Xcode/UserData/Previews/Simulator Devices/<UUID>/data/private/var/MobileAsset/AssetsV2 over time. Most plans are not that infinite. Has anyone seen such thing? The phone is connected over USB and automatically used as network device and also has the low data mode toggle on.
Replies
0
Boosts
0
Views
127
Activity
Jun ’26
Sample Code with Swift 6
I find these sample projects quite valuable: https://developer.apple.com/documentation/widgetkit/emoji-rangers-supporting-live-activities-interactivity-and-animations https://developer.apple.com/documentation/coredata/sharing-core-data-objects-between-icloud-users . Both use Swift 5, and it is not trivial to adopt Swift 6 with them. Any plans to update them? What is best approach for adopting Swift 6 on such sample code?
Replies
5
Boosts
0
Views
1.2k
Activity
Jun ’26
Running with CloudKit and Swift 6
Hello, everyone, I've started a thread and got a recommendation from DTS Engineer to move the conversation here. The sample code for https://developer.apple.com/documentation/coredata/sharing-core-data-objects-between-icloud-users is with Swift 5. I have a Swift 6 project and I'm running into issue porting the sample code. I've found that NSManagedObjectContext is sendable (https://fatbobman.com/en/posts/sendable-nsmanagedobjectcontext/). From it I can get the persistence controller, and from it I get the stores and this solves the Swift 6 migration path and looks good. i.e. not rely on the private and shared references to the persistence stores, I'll be getting them from the context. I've also notices that once I start to change the PersistenceController from lazy vars for lazy var previewContainer: NSPersistentCloudKitContainer and lazy var persistentContainer: NSPersistentCloudKitContainer to struct PersistenceController { static let shared = PersistenceController() @MainActor static let preview: PersistenceController = { let result = PersistenceController(inMemory: true) which is the default pattern for new CoreData applications (Xcode 26 / Xcode 27) I start getting preview crashes similar to https://developer.apple.com/forums/thread/654126. It turns out this is because of let persistenceController = PersistenceController.shared in the App. Quite unexpected. The persistence controller looks more suitable for actor based on the CloudKit sharing requirements. I saw such question on WWDC 26, however there was not enough time for an answer - is a good pattern to have the PersistenceController as an actor or in another way. Which is that most swiftly way for Swift 6?
Replies
0
Boosts
0
Views
524
Activity
Jun ’26
CloudKit, CoreData and Swift 6 for sharing between users
I have started from here: Apple's guide on the sharing core data objects between iCloud users and I have created a sample project that has Collections and Items. Everything works great while I stay on Swift 5, like with the initial project. I would like to migrate to Swift 6 (Default Actor Isolaton @MainActor, Approachable Concurrency: Yes) on the project and I am stuck at extension CDCollection: Transferable { ... }. When compiling with Swift 5, there is a warning: Conformance of 'NSManagedObject' to 'Sendable' is unavailable in iOS; this is an error in the Swift 6 language mode. After resolving almost all compile-time warnings I'm left with: Conformance of 'CDCollection' to protocol 'Transferable' crosses into main actor-isolated code and can cause data races. Which I don't think will work, because of the warning shown above. It can be worked around like: nonisolated extension CDCollection: Transferable, @unchecked Sendable Then there are errors: let persistentContainer = PersistenceController.shared.persistentContainer Main actor-isolated static property 'shared' can not be referenced from a nonisolated context. I've created the following class to have a Sendable object: struct CDCollectionTransferable: Transferable { var objectID: NSManagedObjectID var persistentContainer: NSPersistentCloudKitContainer public static var transferRepresentation: some TransferRepresentation { CKShareTransferRepresentation { collectionToExport in let persistentContainer = collectionToExport.persistentContainer let ckContainer = CloudKitProvider.container var collectionShare: CKShare? if let shareSet = try? persistentContainer.fetchShares( matching: [collectionToExport.objectID]), let (_, share) = shareSet.first { collectionShare = share } /** Return the existing share if the collection already has a share. */ if let share = collectionShare { return .existing(share, container: ckContainer) } /** Otherwise, create a new share for the collection and return it. Use uriRepresentation of the object in the Sendable closure. */ let collectionURI = collectionToExport.objectID .uriRepresentation() return .prepareShare(container: ckContainer) { let collection = await persistentContainer.viewContext .perform { let coordinator = persistentContainer.viewContext .persistentStoreCoordinator guard let objectID = coordinator?.managedObjectID( forURIRepresentation: collectionURI ) else { fatalError( "Failed to return the managed objectID for: \(collectionURI)." ) } return persistentContainer.viewContext.object( with: objectID ) } let (_, share, _) = try await persistentContainer.share( [collection], to: nil ) return share } } } } And I'm able to compile and run the app with this change: let transferable = CDCollectionTransferable( objectID: collection.objectID, persistentContainer: PersistenceController.shared .persistentContainer ) ToolbarItem { ShareLink( item: transferable, preview: SharePreview("Share \(collection.name)!") ) { MenuButtonLabel( title: "New Share", systemImage: "square.and.arrow.up" ) } } The app crashes when launched with libdispatch.dylib`_dispatch_assert_queue_fail: 0x1052c6ea4 <+0>: sub sp, sp, #0x50 0x1052c6ea8 <+4>: stp x20, x19, [sp, #0x30] 0x1052c6eac <+8>: stp x29, x30, [sp, #0x40] 0x1052c6eb0 <+12>: add x29, sp, #0x40 0x1052c6eb4 <+16>: adrp x8, 63 0x1052c6eb8 <+20>: add x8, x8, #0xa0c ; "not " 0x1052c6ebc <+24>: adrp x9, 62 0x1052c6ec0 <+28>: add x9, x9, #0x1e5 ; "" 0x1052c6ec4 <+32>: stur xzr, [x29, #-0x18] 0x1052c6ec8 <+36>: cmp w1, #0x0 0x1052c6ecc <+40>: csel x8, x9, x8, ne 0x1052c6ed0 <+44>: ldr x10, [x0, #0x48] 0x1052c6ed4 <+48>: cmp x10, #0x0 0x1052c6ed8 <+52>: csel x9, x9, x10, eq 0x1052c6edc <+56>: stp x9, x0, [sp, #0x10] 0x1052c6ee0 <+60>: adrp x9, 63 0x1052c6ee4 <+64>: add x9, x9, #0x9db ; "BUG IN CLIENT OF LIBDISPATCH: Assertion failed: " 0x1052c6ee8 <+68>: stp x9, x8, [sp] 0x1052c6eec <+72>: adrp x1, 63 0x1052c6ef0 <+76>: add x1, x1, #0x9a6 ; "%sBlock was %sexpected to execute on queue [%s (%p)]" 0x1052c6ef4 <+80>: sub x0, x29, #0x18 0x1052c6ef8 <+84>: bl 0x105301b18 ; symbol stub for: asprintf 0x1052c6efc <+88>: ldur x19, [x29, #-0x18] 0x1052c6f00 <+92>: str x19, [sp] 0x1052c6f04 <+96>: adrp x0, 63 0x1052c6f08 <+100>: add x0, x0, #0xa11 ; "%s" 0x1052c6f0c <+104>: bl 0x1052f9ef8 ; _dispatch_log 0x1052c6f10 <+108>: adrp x8, 95 0x1052c6f14 <+112>: str x19, [x8, #0x1f0] -> 0x1052c6f18 <+116>: brk #0x1 The app still crashes when I comment this code, and all Core Data related warnings. I'm quite stuck now as I want to use Swift 6. Has anyone figured CloudKit, CoreData and Swift 6 for sharing between users?
Replies
1
Boosts
0
Views
337
Activity
Feb ’26