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


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

struct book_managerApp: App {
    var body: some Scene {
        WindowGroup {
        .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 =
    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 ?? ""


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..


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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.