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

SwiftData iCloud Syncing isn't working in default project

Forums > Swift

@lrn  

I'm trying to get a project working that syncs SwiftData with iCloud. I first tried adding SwiftData and CloudKit to an existing project that has a model that looks like this. Its also setup as a modelContainer in the main app file. It returns this error "Fatal error: failed to find a currently active container for Book". I have selected a container in the iCloud capability also. I then just tried to create a new project that included SwiftData from the beginning. That one, all i did was select a container since the rest of the setup was already done and its giving an error that says "Could not create ModelContainer: SwiftDataError(error: SwiftData.SwiftDataError.Error.loadIssueModelContainer)" . Any insight would be appreciated.

import SwiftUI
import SwiftData

@main
struct book_managerApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .modelContainer(for: Book.self)
    }
}
@Model class Book {
    var isbn: String
    var title: String
    var authors: [String]
    var publisher: String
    var publishDate: String
    var bookDescription: String
    var pageCount: Int
    var categories: [String]
    var smallThumbnail: String
    var thumbnail: String
    var dateScanned: Date = Date.now
    var seriesName: String = ""
    var seriesNumber: Int = 0
    var readDates: [Date] = []
    var isOwned: Bool = true

    init(isbn: String, title: String, authors: [String], publisher: String, publishDate: String, bookDescription: String, pageCount: Int, categories: [String], smallThumbnail: String? = nil, thumbnail: String? = nil) {
        self.isbn = isbn
        self.title = title
        self.authors = authors
        self.publisher = publisher
        self.publishDate = publishDate
        self.bookDescription = bookDescription
        self.pageCount = pageCount
        self.categories = categories
        self.smallThumbnail = smallThumbnail ?? ""
        self.thumbnail = thumbnail ?? ""
    }
}

2      

I believe for Cloudkit sync to work, all parts of your Book Model class must be given a default value or be optional so

@Model class Book {
    var isbn: String = "unknown"
    var title: String = "blank"
    var authors: [String] = []
    var publisher: String = "anonymous"

and so on..

2      

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.