I can't for the life of me get transitions and animations to work well with SwiftData and List on MacOS 15 and iOS 18.
I've included an example below, where I define several animations and a transition type, but they are all ignored.
How do I animate items being added to / removed from a List()?
I am attached to List() due to its support for selection, context menu, keyboard shortcuts, etc. If I would switch to ScrollView with VStack I would have to rebuild all of that.
Also, this is super basic and should just work, right?
Thanks for reading.
import SwiftUI
import SwiftData
struct ContentView: View {
@Environment(\.modelContext) private var modelContext
/// Issues on iOS:
/// Items animate into and out of view, but I seem to have no control over the animation.
/// In the code here I've specified a 'bouncy' and a slow 'easeIn' animation: both are not triggered.
/// The code also specifies using a 'slide' transition, but it is ignored.
/// -> How do I control the transition and animation timing on iOS?
///
/// Issues on MacOS:
/// Items do not animate at all on MacOS! They instantly appear and are instantly removed.
/// -> How do I control the transition and animation timing on MacOS?
// animation added here -> has no effect?
@Query(animation: .bouncy) private var items: [Item]
var body: some View {
VStack {
Button("Add to list") {
// called without 'withAnimation' -> no animation
let newItem = Item(timestamp: Date())
modelContext.insert(newItem)
}
List() {
ForEach(items, id: \.self) { item in
Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))
.transition(.slide) // items do not slide in/out of view
.onTapGesture {
// called with 'withAnimation' -> no animation
withAnimation(.easeIn(duration: 2)) {
modelContext.delete(item)
}
}
}
.animation(.spring(duration: 3), value: items)
}
}
.padding()
}
}
#Preview {
ContentView()
.modelContainer(for: Item.self, inMemory: true)
}
Post
Replies
Boosts
Views
Activity
I am seeing a strange warning pop up in my SwiftData ModelActor:
Publishing changes from background threads is not allowed; make sure to publish values from the main thread (via operators like receive(on:)) on model updates.
This warning is triggered by the try self.modelContext.save() call in the following function in my ModelActor:
public func purgeLocalEvents(for calendarId: PersistentIdentifier) async {
do {
let calendars = try self.modelContext.fetch(CalendarFetchDescriptors.getCalendar(calendarId))
if let calendar = calendars.first {
if let events = calendar.events {
for event in events {
self.modelContext.delete(event)
}
}
calendar.lastSync = .distantPast
try self.modelContext.save()
}
} catch {
debugPrint("Error loading calendar for event purge", error.localizedDescription)
}
}
The function in the ModelActor is called like this:
Task.detached(priority: .userInitiated) {
let actor = await RemoteGoogleCalendarActor(modelContainer: SwiftDataCoordinator.shared.fullContainer)
await actor.purgeLocalEvents(for: calendarId)
}
I perform saves from modelactors in many other places, and I've never seen this warning before. What could be causing this issue?
Hi,
I'm developing an app for iOS and MacOS with SwiftData and CloudKit syncing. I had sync working very well with a set of models. This schema was also pushed to CloudKit production.
Last week I added several models, and several relationship properties linking my existing models to newly added models. The schema updated just fine and everything worked.
Then things went sideways: earlier this week I decided I wanted to rename a property on one of my new models. So I renamed the property, removed the application support folder for my local debug app (on Mac OS), removed the app from the iOS Simulator (to clear its local database), and finally reset my CloudKit container to its Production schema. Basically, I tried to go back to the same state I had as when I first added the new models.
However, this time things don't go so smoothly, even after starting the app several times, rebooting my machine, turning iCloud on and off in Xcode and MacOS and iOS. When I look in CloudKit console, I see only my old models there: none of the new ones are added.
I'd love some pointers on how I can best debug this issue, as I feel completely stuck.
On MacOS
I have very little
mac-logs.txt
to go on. Since the logs are a bit lengthy I've added them as an attachment. I get a few warnings, but it is unclear what they are warning me about.
One thing that does stand out is that I am running the CloudKit in Development mode here. However, the logs do state accountPartition=Prod . And when I query CKContainer.default() for the container environment, the response is sandbox, which matches Development!
On iOS
The logs show a few errors, but I cannot make sense of them.
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _performSetupRequest:]_block_invoke(1240): <NSCloudKitMirroringDelegate: 0x600003d09860>: Failed to set up CloudKit integration for store: <NSSQLCore: 0x103325c90> (URL: file:///Users/bastiaan/Library/Developer/CoreSimulator/Devices/BF847CE5-A3E2-4B4C-8CD5-616B75B29AFE/data/Containers/Data/Application/0A916F67-B9B2-457B-8FA7-8C42819EA9AA/Library/Application%20Support/default.store)
<CKError 0x600000c433f0: "Partial Failure" (2/1011); "Failed to modify some record zones"; partial errors: {
com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600000c956b0: "Internal Error" (1/5005); "Couldn't create new PCS blob for zone <CKRecordZoneID: 0x600000c475d0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>">
}>
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate recoverFromError:](2310): <NSCloudKitMirroringDelegate: 0x600003d09860> - Attempting recovery from error: <CKError 0x600000c433f0: "Partial Failure" (2/1011); "Failed to modify some record zones"; partial errors: {
com.apple.coredata.cloudkit.zone:__defaultOwner__ = <CKError 0x600000c956b0: "Internal Error" (1/5005); "Couldn't create new PCS blob for zone <CKRecordZoneID: 0x600000c475d0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>">
}>
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _recoverFromPartialError:forStore:inMonitor:]_block_invoke(2773): <NSCloudKitMirroringDelegate: 0x600003d09860>: Found unknown error as part of a partial failure: <CKError 0x600000c956b0: "Internal Error" (1/5005); "Couldn't create new PCS blob for zone <CKRecordZoneID: 0x600000c475d0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>">
error: CoreData+CloudKit: -[NSCloudKitMirroringDelegate _recoverFromPartialError:forStore:inMonitor:](2820): <NSCloudKitMirroringDelegate: 0x600003d09860>: Error recovery failed because the following fatal errors were found: {
"<CKRecordZoneID: 0x600000c8fd50; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>" = "<CKError 0x600000c956b0: \"Internal Error\" (1/5005); \"Couldn't create new PCS blob for zone <CKRecordZoneID: 0x600000c475d0; zoneName=com.apple.coredata.cloudkit.zone, ownerName=__defaultOwner__>\">";
}
And in CloudKit logs I see:
06/11/2024, 9:09:59 UTC 738513AC-9326-42DE-B4E2-DA51F6462943 iOS;18.1 ZoneFetch EphemeralGroup
{
"time":"06/11/2024, 9:09:59 UTC"
"database":"PRIVATE"
"zone":"com.apple.coredata.cloudkit.zone"
"userId":"_0d9445f850459ec351330ca0fde4134f"
"operationId":"611BA98C9B10D3F2"
"operationGroupName":"EphemeralGroup"
"operationType":"ZoneFetch"
"platform":"iPhone"
"clientOS":"iOS;18.1"
"overallStatus":"USER_ERROR"
"error":"ZONE_NOT_FOUND"
"requestId":"738513AC-9326-42DE-B4E2-DA51F6462943"
"executionTimeMs":"53"
"interfaceType":"NATIVE"
}
Any pointers are greatly appreciated!
Bastiaan