GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

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      

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

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.