insidegui / CloudKitCodable

An encoder and decoder for CKRecord
BSD 2-Clause "Simplified" License
387 stars 29 forks source link

cannot use an empty list to initialize a new field #11

Open huanglins opened 1 year ago

huanglins commented 1 year ago

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

insidegui commented 1 year ago

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

huanglins commented 1 year ago

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.

insidegui commented 1 year ago

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.