I have been attempting to study SwiftData with iCloud syncing and need to create an in reminiscence information retailer so I can take a look at ideas with dummy information that is added on load and by no means saved.
That approach I can play with the information shut down the app and re-load it to get again to the beginning. If I need to then play with current information I can take away the reminiscence solely flag and use the synced iCloud information.
The problem is that it appears to disregard isStoredInMemoryOnly
when creating the SwiftData ModelConfiguration
and saves to and hundreds from the iCloud supply not the in reminiscence one.
Whereas I do know I can rapidly create a “delete and reset all” button I now need to know what I am doing unsuitable as I additionally use this identical flag for previewing so don’t need different points to happen on account of testing in previews getting saved into my information storage.
I learn on none Apple sources that the parameter isStoredInMemoryOnly
does what it says and stops the ModelContainer from storing the information in iCloud. I’ve discovered no official descriptions of this parameter however really feel the title is obvious.
My first take a look at (utilizing the beneath code) labored positive as each time I closed the app it could lose the information however then I loaded the app in one other simulator and located it saved the information to the iCloud as reloading the app did not clear the information and nether did deleting and re-installing the app because it simply pulled the beforehand saved information from iCloud…
I’ve tried deleted the app from all simulators, deleting the iCloud information for that account in addition to resetting the app from the CloudKit Console and now all checks are inflicting it to retailer all information in iCloud and I can not even replicate the unique sucess.
Right here is a few instance code (mainly the Apple default iCloud sync code however with isStoredInMemoryOnly
set to true
.
-
I assume the very first thing to ask is that if anybody can affirm (through Apple docs) if the
isStoredInMemoryOnly
param when creating a brand newModelConfiguration
(proven on line 20 beneath) is in reality meant to stop it storing to the CloudKit Container or if that is only a dangerous title and everybody saying so has assumed. -
If that is true is there one thing unsuitable with my instance code or is it only a (identified?) bug.
Beneath is a fast instance. To make use of it you may want an empty iCloud container related with the CloudKit
Service chosen.
import SwiftUI
import SwiftData
import Basis
@Mannequin
last class Merchandise {
var timestamp: Date?
init(timestamp: Date) {
self.timestamp = timestamp
}
}
@fundamental
struct CloudKitTest_TempApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Item.self,
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: true)
do {
return attempt ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Couldn't create ModelContainer: (error)")
}
}()
var physique: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
}
}
struct ContentView: View {
@Surroundings(.modelContext) personal var modelContext
@Question personal var objects: [Item]
var physique: some View {
NavigationSplitView {
Listing {
ForEach(objects) { merchandise in
if (merchandise.timestamp != nil) {
NavigationLink {
Textual content("Merchandise at (merchandise.timestamp!, format: Date.FormatStyle(date: .numeric, time: .commonplace))")
} label: {
Textual content(merchandise.timestamp!, format: Date.FormatStyle(date: .numeric, time: .commonplace))
}
}
}
.onDelete(carry out: deleteItems)
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
ToolbarItem {
Button(motion: addItem) {
Label("Add Merchandise", systemImage: "plus")
}
}
}
} element: {
Textual content("Choose an merchandise")
}
}
personal func addItem() {
withAnimation {
let newItem = Merchandise(timestamp: Date())
modelContext.insert(newItem)
}
}
personal func deleteItems(offsets: IndexSet) {
withAnimation {
for index in offsets {
modelContext.delete(objects[index])
}
}
}
}