Thursday 15 May 2014

objective c - iOS CoreData parsing from CSV cannot save error 1570 nil object -



objective c - iOS CoreData parsing from CSV cannot save error 1570 nil object -

i'm quite new objective c , ios, appreciate help here. have been banging head against wall trying working, maintain getting error:

failed save info store: operation couldn’t completed. (cocoa error 1570.)

what doing importing csv file.

from can tell partially working. csv correctly beingness parsed, i'm getting right values, , have debugged through , can tell ka objects getting right values.

but there seems wrong how doing relationships - the 1570 issue coming nil newtrainingday's required date field (which seems filled when i've debugged). have feeling else wrong not sure what..

can tell me doing wrong here?

i think problem stems creating , inserting new objects database, before searching see if there existing object meets needs. take code, example:

// existing day katrainingday *newtrainingday = [nsentitydescription insertnewobjectforentityforname:@"katrainingday" inmanagedobjectcontext:self.managedobjectcontext]; // fetching fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"katrainingday"]; [fetchrequest setpredicate:[nspredicate predicatewithformat:@"date == %@", date]]; // execute fetch request fetcherror = nil; nsarray *trainingdays = [self.managedobjectcontext executefetchrequest:fetchrequest error:&fetcherror]; // if found if (!fetcherror) { if ([trainingdays count]) { (katrainingday *day in trainingdays) { newtrainingday = day; } } else { // set name , date training day newtrainingday.date = date; newtrainingday.name = trainingdaynamestring; [newtrainingday addroutinesobject:newtrainingroutine]; [newtrainingroutine addtrainingdaysobject:newtrainingday]; }

you start creating , inserting katrainingday context, , fetch katrainingday right date. if find it, assign pointer (newtrainingday) object fetched. but object inserted still in context (though no longer have pointer it) , have nil values attributes , relationships. hence when save, object fails required day constraint.

to prepare this, insert new objects if don't find match fetch:

// existing day katrainingday *newtrainingday; // fetching fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"katrainingday"]; [fetchrequest setpredicate:[nspredicate predicatewithformat:@"date == %@", date]]; // execute fetch request fetcherror = nil; nsarray *trainingdays = [self.managedobjectcontext executefetchrequest:fetchrequest error:&fetcherror]; // if found if (!fetcherror) { if ([trainingdays count]) { (katrainingday *day in trainingdays) { newtrainingday = day; } } else { // set name , date training day newtrainingday = [nsentitydescription insertnewobjectforentityforname:@"katrainingday" inmanagedobjectcontext:self.managedobjectcontext] newtrainingday.date = date; newtrainingday.name = trainingdaynamestring; } [newtrainingday addroutinesobject:newtrainingroutine];

(and likewise other entities). in above have moved addroutinesobject: phone call outside else block, execute whether find existing or create new (which assume want). , note don't need set both sides of relationship - if set 1 side, coredata other automatically (assuming defined inverses). 1 other tip, don't need loop lastly item in array, can use:

newtrainingday = [trainingdays lastobject];

ios objective-c core-data

No comments:

Post a Comment