Wednesday, 21 August 2013

Core Data one to many record insert error

Core Data one to many record insert error

I have three entities
Forms{ name:string jobs<-->>JSAjobs.form }
JSAjobs{ name:string form<<-->Forms.jobs }
Jobs{ step:string jobs<<-->Forms.jobs }
I am getting this error: to-many relationship fault "jobs" for objectID
0x95afe60. . . fulfilled from database. Got 0 rows
Now I save the row for Forms entity first later on I need to fetch the
last record on the Form entity create a new row on JSAjobs with details on
JSAsop like next
Thanks
NSMutableArray *jobData = [[NSMutableArray
alloc]initWithArray:controller.jobData];
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"JSAform" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSPredicate *testForFalse = [NSPredicate predicateWithFormat:@"emailed ==
NO"];
[fetchRequest setPredicate:testForFalse];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest
error:&error];
NSLog(@"Fetched Rows: %i", [fetchedObjects count]);
//NSManagedObject *existingParent= //... results of a fetch
JSAform *lastForm = [fetchedObjects objectAtIndex:0];
JSAjobs *newJobs = [NSEntityDescription
insertNewObjectForEntityForName:@"JSAjobs"
inManagedObjectContext:context];
// Setting new values
newJobs.jobType = [NSString stringWithFormat:@"%@", [jobData
objectAtIndex:0]];
newJobs.jobName = [NSString stringWithFormat:@"%@", [[[jobData
objectAtIndex:1]objectAtIndex:0] objectAtIndex:0]];
newJobs.comments = [NSString stringWithFormat:@"%@", [[[jobData
objectAtIndex:1]objectAtIndex:0] objectAtIndex:1]];
newJobs.date = [NSDate date];
[newJobs setValue:lastForm forKey:@"form"];
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
//New SOP Value
JOBsop *jobSOP = [NSEntityDescription
insertNewObjectForEntityForName:@"JOBsop" inManagedObjectContext:context];
for (int i = 0; i< [[jobData objectAtIndex:1]count]; i++){
NSLog(@"Value for key: %i", i);
if (i > 0){
for (int k = 0; k< [[[jobData objectAtIndex:1]objectAtIndex:i]
count]; k++){
jobSOP.step = [[[jobData objectAtIndex:1]objectAtIndex:i]
objectAtIndex:k];
[jobSOP setValue:newJobs forKey:@"jobs"];
// [NSNumber numberWithInt:[txtBoots.text integerValue]];
NSLog(@"Simple key: %@", [[[jobData
objectAtIndex:1]objectAtIndex:i] objectAtIndex:k]);
}
}
}
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
enter code here

No comments:

Post a Comment