CloudKitCodable icon indicating copy to clipboard operation
CloudKitCodable copied to clipboard

cannot use an empty list to initialize a new field

Open huanglins opened this issue 1 year ago • 3 comments

Do I have an optional array type field such as [Int]? How to fix this error

huanglins avatar Jul 28 '23 02:07 huanglins

@huanglins Can you share some code snippets of what you're trying to achieve?

insidegui avatar Jul 28 '23 12:07 insidegui

I probably have a model of this

struct Person {
    var name: String
    var scores: [Int]?
}

let person = Person(name: "v")

sync person
  1. When scores is empty, initializing a CKRecord is not possible. cannot use an empty list to initialize a new field

  2. Once a value has been set for an optional property and it has been synced to iCloud, it cannot be set back to nil and synced to iCloud again. It will always sync with the iCloud value.

func encode<T>(_ value: T, forKey key: Key) throws where T : Encodable {

Values should not be set for fields that cannot be nil.

The second issue is more severe.

huanglins avatar Jul 28 '23 13:07 huanglins

cannot use an empty list to initialize a new field

This error sounds like you're attempting to initialize a new field for a record in the development environment, which also defines its schema. When that's the case, then there must be some sort of value to inform the CloudKit servers what type of list you're trying to create, since it's a brand new field that the servers don't know about.

I don't think this would be a problem for existing record fields.

insidegui avatar Jul 28 '23 14:07 insidegui