ios – Sync not working for unbiased watchOS app utilizing CloudKit and SwiftData

0
1
ios – Sync not working for unbiased watchOS app utilizing CloudKit and SwiftData


I am engaged on an unbiased watchOS app and I used to be searching for the very best resolution to persist information regionally and have a web based sync with the iCloud storage.

I’ve discovered in regards to the SwiftData which is working very properly for my app. Then I needed to hook up the info to the iCloud containers so I picked up the CloudKit. I am testing the strategy with a TestFlight builds for my watchOS app however I can’t see any data being saved within the iCloud container.

I attempted working the identical configuration and similar sort for a clear iOS app simply to check the syncing system. It is working with no drawback. I can see the info uploaded (in iCloud container) from an iOS in addition to within the iOS app whereas making a report from the CloudKit Console. Sadly it isn’t the case for my watchOS app.

@Mannequin
closing class Merchandise {
    var timestamp: Date = Date()
    
    init(timestamp: Date) {
        self.timestamp = timestamp
    }
}

@predominant
struct TestApp: App {
    var sharedModelContainer: ModelContainer = {
        let schema = Schema([
            Item.self,
        ])
        let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)

        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 {
    @Atmosphere(.modelContext) personal var modelContext
    @Question personal var gadgets: [Item]

    var physique: some View {
        NavigationSplitView {
            Listing {
                ForEach(gadgets) { merchandise in
                    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(gadgets[index])
            }
        }
    }
}

Code above is principally an instance offered by the Xcode for an app utilizing CloudKit and it is working very properly for the iOS app.

I’ve added the aptitude to:

  1. Background Modes: Distant notifications
  2. iCloud: CloudKit + added a container
  3. Push notifications

Did I miss one thing whereas configuring the setup? I figured that if Apple supplies SwiftData and CloudKit that it could look the identical between the platforms when it comes to a logic. I can’t discover something extra to setup the SwiftData + CloudKit for an unbiased watchOS app.

LEAVE A REPLY

Please enter your comment!
Please enter your name here