CKRecordValue type NSDate stores nil value?

In the development of an app, one of its UITextField is designed to allow users to use input view of datePicker view to pick a date and also allows the users to delete a displayed date via clear button as a overlay view in the textField while edting. Data is stored via Cloudkit and a CKRecordValue type - NSDate is used for that UITextField.


My question is how to change the NSDate to reflect the user deleting an existing date on the textField? It seems NSDate can't be nil as I got atomic error when trying to save a nil date. Direct database like MySQL gives an option of null value and I wonder if we may set a NSDate type to allow nil input?

Replies

>It seems NSDate can't be nil as I got atomic error when trying to save a nil date


Rather than use nil, use your favorite date in the past. If the viewController detects that the use has tapped the clear button then set the date to that favorite date.

Great point which definitely walkarounds the empty or nil date issue. However it may increase the complexity of coding as I have to deal with NS Date field value differrently from other CKRecordValue types which may store empty value of that type via Cloudkit.


Why can Apple redesign their API for Cloudkit to implement the similar walkaround? It's very common usage - any data can be optional including date input and I bet their underlying database schema can be set to use optional null/nil.


If their API mandate is to provide easy-to-use Cloudkit usage for developer, I truely hope they may take the initiative to make the change.

So for a column that represents NSDate, it is considered nonnull and you have to use a placeholder date value instead? Is there any particular reason why you can't set a date nil?

Just discovered that... We can not have a nil NSDate field in CloudKit, when I save a record it is set to a default date 2001,01,01 but it is not nil.

I do not want to add a boolean column or a "placeholder" value to know if the date is nil or not + always think about it otherwise I will have incorrect data shown to the user.

Is there something we can do ? Does someone has an answer? I miss something I think as I can not find many resources on this BIG disadvantage to use CloudKit!

  • CloudKit dashboard: date is nil when created and when editing we have to delete each part of the date manually and save.

    It seems to be a problem with CoreData + CloudKit sync, still searching

  • In CoreData, Dates are saved with a time interval from a reference date (which is 2001-01-01 00:00:00). The date field is "optional' in CoreData, and when saving 'nil', the value sent to CloudKit is the reference date, and when accessing the date, for CoreData it is not nil, I have the reference data as a value. One workaround could be something like :

    date.timeIntervalSinceReferenceDate != .zero

    but just no

Add a Comment