Core Data Save Error (NSValidationErrorKey, Cocoa error 1570) saving NSDate
Asked Answered
O

6

53

I'm getting an error with saving to a Core data object in Xcode.

Xcode says that the error is in the NSDate variable 'datum' but I have tried almost everything. Error is:

2011-07-12 18:01:29.068 WeekLijstje[3205:207] Core Data Save Error
NSValidationErrorKey    datum
NSValidationErrorPredicate  (null)

NSValidationErrorObject
<DagLijst: 0x6e2fcd0> (entity: DagLijst; id: 0x6e2fd30 <x-coredata:///DagLijst/t99F423FC-AAE9-4692-9264-EF0FF7A020572> ; data: {
    Voedsel = nil;
    datum = nil;
    hoeveelheid = 0;
    punten = 0;
})
NSLocalizedDescription:The operation couldn’t be completed. (Cocoa error 1570.)

A small code snipet:

        DagLijst *newDaglijst = [NSEntityDescription insertNewObjectForEntityForName:@"DagLijst" inManagedObjectContext:self.managedObjectContext];

        NSDate *selDatum = [NSDate date];
        newDaglijst.punten = [NSNumber numberWithInteger:10];
        newDaglijst.hoeveelheid = [NSNumber numberWithInt:100];
        newDaglijst.Voedsel = geselecteerdVoedsel;
        newDaglijst.datum = selDatum;
        NSError *error = nil;
        if (![newDaglijst.managedObjectContext save:&error]) {
...

Also the class of the DagLijst object:

@interface DagLijst : NSManagedObject {
@private
}
@property (nonatomic, retain) NSDate * datum;
@property (nonatomic, retain) NSNumber * punten;
@property (nonatomic, retain) NSNumber * hoeveelheid;
@property (nonatomic, retain) Voedsel *Voedsel;

@end

So you can see that I put an NSDate into the 'datum' variable. But on execution I still get an error.

Obstetrician answered 12/7, 2011 at 16:22 Comment(0)
D
154

The cocoa error 1570 means that mandatory fields are not filled in. In this case, your have two attribues that are nil : Voedsel and datum.

I see in your code :

newDaglijst.Voedsel = geselecteerdVoedsel;
newDaglijst.datum = selDatum;

Check that geselecteerdVoedsel and selDatum are not nil or that they are overreleased and finish to be nil. If they are optional data (but I don't think so), define them as optional in coredata.

Hope this help,

Dominican answered 6/9, 2011 at 7:2 Comment(3)
in my case, the child context has proper data, whereas the parent context, when it tries to save has no data..!! Everything is nil!! How can I ensure that the data is getting reflected to the parent ? Link to my question is : #19666678Thalia
I ran into this same problem trying to save NO in an NSNumber that we used as a boolean value. I wracked my brain for an hour before realizing I needed to use @NOLumbard
Same here: tried to write 0 to a non-optional NSNumber attribute, setting it nil. Assign @0 instead.Suppletion
S
3

as Michael A said. Check your attributes value are not nil. There are 2 alternatives to get rid of these error. Case 1:If 2 attributes are Required

If the 2 attributes are required attributes then it is mandatory to check the values you are passing are not nil,It may happens sometimes,If u want to get out of these error u have to give default values for those attributes in attributes inspector of your Entity in data Model

Case 2: Set those attributes to optional in attribute inspector by selecting the check mark of optional.

Me too struggled for days to know these error. Hope it helps someone.

Shumpert answered 13/11, 2014 at 7:41 Comment(0)
T
1

Your logging would look like this:

Fatal error: 'try!' expression unexpectedly raised an error: 
Error Domain=NSCocoaErrorDomain Code=1560 "(null)" UserInfo={NSDetailedErrors=(
...

It means you have an uncommitted change for one (or more) property of a entity, in which you told it is NOT optional, but you left it optional.

To find out which entity you failed to set value for a property, look for this in your logging:

UserInfo={NSValidationErrorObject=<YOURENTITYISHERE: ...>

To find out the property, search for:

NSValidationErrorKey=YOURPROPERTYISHERE

Somewhere in your code you forget to set a value for that property for the given entity.

Thousand answered 19/1, 2019 at 18:6 Comment(0)
C
0

I had this issue when I copied entities from other xcdatamodeld file. They lost inverse attributes, so that was the reason.

Clarendon answered 8/12, 2013 at 11:33 Comment(0)
K
0

Not really related with date, but with the error, I share it as this question has more views:

In my case, I was setting a BOOL property directly as YES or NO, but you should use

NSNumber numberWithBOOL

in order to make it work.

Kyanite answered 10/4, 2016 at 20:6 Comment(0)
G
0

To reinforce Michael's answer, you can check your Entity properties in the inspector. One of your items may be accidentally considered Optional, or not. Or, if you're like me, your "Delete Rule" might have been set to "Nullify", which made my Entity's relationship property Nil at runtime. Because my object-to-be-deleted had a One-To-Many relationship with some other objects, it prevented the parent objects from being deleted. Changing it to Cascade solved the problem.

Entity Inspector - Delete Rule

Guth answered 30/5, 2018 at 17:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.