Reputation: 5041
The Apple Core Data Programming Guide says:
You can specify that an attribute is optional—that is, it is not required to have a value. In general, however, you are discouraged from doing so—especially for numeric values (typically you can get better results using a mandatory attribute with a default value—in the model—of 0). The reason for this is that SQL has special comparison behavior for NULL that is unlike Objective-C's nil. NULL in a database is not the same as 0, and searches for 0 will not match columns with NULL.
But i need my attributes to have no value at all (nil) when they are created. I cannot have 0
(Zero) or any other default value, because that would result in wrong domain data.
So what problems will arise if i define my numeric Core Data attributes as optional with no default value? And can i live with them?
Upvotes: 2
Views: 2348
Reputation: 21902
Say you have a numeric (int
for example) optional column in your DB. If you leave it blank, there will be a NULL in the DB. Now say you subclass NSManagedObject
and use primitive types in your object. That column will be mapped to an int
property. When the object is loaded, what will it put in there? It can't be nil
and it can't be zero.
It's discouraged, not forbidden. Just don't use primitive types in your model objects, which from what you are saying, you wouldn't use in this case anyway if you need to be able to support nils.
Upvotes: 6