0

I have a CoreData private database that I'd like to make into a public database. I've done this before but this database is more complicated and perhaps that is the underlying issue. When I check the CloudKit console logs all I see i a request flagged as BAD REQUEST. How can I tell why it is a bad request?

I am not getting any data synched to CloudKit.

My setup in PersistenceController is ...

import CoreData
import CloudKit
import UIKit

struct PersistenceController
{
    
    static var preview: PersistenceController = {
        let result = PersistenceController(inMemory: true)
        let viewContext = result.container.viewContext
        
        return result
    }()

    let container: NSPersistentCloudKitContainer

    init(inMemory: Bool = false)
    {
        container = NSPersistentCloudKitContainer(name: "SBWorkbook")
        if inMemory
        {
            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
        }
        guard let description = container.persistentStoreDescriptions.first else
        {
            fatalError()
        }
        
        description.cloudKitContainerOptions?.databaseScope = .public
        
        container.viewContext.automaticallyMergesChangesFromParent = true
        container.viewContext.transactionAuthor = "Me"
        container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
        
        container.loadPersistentStores(completionHandler:
        { (storeDescription, error) in
            if let error = error as NSError?
            {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
    }
Asperi
  • 228,894
  • 20
  • 464
  • 690
Russ
  • 467
  • 3
  • 10
  • Show the code where you are fetching data, some functions are only on private dbs. So post the code and the error when that code gets executed. – user1046037 Aug 16 '22 at 09:51
  • It appears that this problem happens before any fetches. I created a new app, changed PersistenceController to public, copied the schema into the app as well as through the dashboard. I get the same error without any explicit fetches. – Russ Aug 17 '22 at 14:15
  • With `NSPersistentCloudKitContainer` sync happens automatically, try to post the complete error and usually CKError mentions the reason as well. So post that entire error. – user1046037 Aug 17 '22 at 17:21
  • `container.persistentStoreDescriptions.first` would be private db, leave it as it is, and create a copy of that and modify it to make it public then later do `container.persistentStoreDescriptions.append(publicStore)`. That is they do for shared db. Refer: https://developer.apple.com/wwdc21/10015 – user1046037 Aug 17 '22 at 17:28

0 Answers0