UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Default values -- CloudKit and CoreData

Forums > SwiftUI

Hi,

I decided to add CloudKit support to my app to let my CoreData model be kept in sync and also allow some future enhancements. I've noted that as soon as I ticked the 'CloudKit' checkbox on my project; I got loads of errors around my CoreData model.

Things like; xxx.id or xxx.name must have a default value.

Also; a few of the CoreData relationships I had setup were non-optional, and this was flagged as not being possible. Can I check --- is this normal? That I have to give every model field a default value? I'm not even sure how I can do that with UUID fields?

2      

hi Pete,

i remember that i ran into similar problems with switching NSPersistentContainer to NSPersistentCloudKitContainer, and yes, you've found two of (at least) three things that must be true to move to the cloud:

  • Attributes that are intended to be optional in Core Data must be optional (you can't write your own @NSManaged var name: String definition, which XCode would by default generate to be of type String?).
  • Attributes that Core Data expects to be non-optional, such as Integer16 and Boolean and Double, must always have a default value. for a Date or a UUID, which come across in Swift to be of type Date? and UUID?, just leave the Default button unchecked in the model editor.
  • Relationships must be unordered (so, an NSSet? and not an NSOrderedSet?). if you have an ordered relationship, you'll have to keep the order somewhere else (possibly as an [Int] in one of the entities, or in another entity whose purpose is to track order in the relationship).

hope that helps,

DMG

3      

Thanks; it's a tad disappointing that this disparity exists but at least I know for sure.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.