TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SwiftData `isStoredInMemoryOnly: true` not working on macOS + iCloud?

Forums > SwiftUI

Wondering if anyone else is running into this.

It seems ModelConfiguration(isStoredInMemoryOnly: true) for previews (as outlined here) works correctly for

  • iOS + iCloud syncing
  • macOS WITHOUT iCloud syncing

But as soon as I turn on iCloud syncing capability for my macOS target, its as if the isStoredInMemoryOnly has no effect on the macOS target.

Here's my code...

I made a PreviewHelper to encapsulate the preview logic...

enum PreviewHelper {
    static let previewModelContainer: ModelContainer = {
        do {
            let config = ModelConfiguration(isStoredInMemoryOnly: true)
            let container = try ModelContainer(for: Task.self, configurations: config)
            return container
        } catch {
            fatalError("Failed to create model container for previewing: \(error.localizedDescription)")
        }
    }()
}

And then use it like so...

#Preview {
    let container = PreviewHelper.previewModelContainer

    for task in MockData.tasks {
        container.mainContext.insert(task)
    }

    return HorizonView()
        .modelContainer(container)

}

On the macOS target & destination, using a macOS device in the Preview canvas, with iCloud syncing turned on that code inserts the MockData.tasks into my iCloud container every time the preview refreshes, so the data just keeps getting duplicated. With iCloud syncing turned off it behaves as expected/correctly (just inserting the MockData as needed for Previews).

In an iOS target, using the same helper and mock data, the helper behaves as expected/correctly (with or without iCloud syncing enabled).

Assuming this might be a bug/oversight with SwiftData and macOS? Or am I missing a needed configuration/capability on the macOS side? Anybody else seeing this?

3      

FWIW, I also just tried running Paul's face facts app app with a macOS destination (after commenting out the bits that don't work on macOS), and the same behavior happens in previews.

3      

Same here! A real issue!

1      

I suggest you create a bug report on apple developer feedback as well.

1      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.