Note that there are some explanatory texts on larger screens.

plurals
  1. POsaving only one attribute value out of two for an entity
    primarykey
    data
    text
    <p>I have an entity named <code>Attribute</code>, it has 2 attributes <code>attribute_name</code> and <code>attribute_value</code>.</p> <p>This <code>Attribute</code> <code>entity</code> is in on-to-many relationship with another entity named <code>Project</code>. I want to insert it like all the <code>attribute_name</code> for the one project to be same and only <code>attribute_value</code> to be different. </p> <p>When I enter data, I take these attributes for <code>Attribute</code> entity in two different arrays and save them in go on clicking <code>Save</code> button using a <code>for</code> loop.</p> <p>In order to keep <code>attribute_name</code> distinct, I check if they already exist in the data base,if yes then I only save <code>attribute_value</code>. But it still inserts "attributes_name" as <code>NULL</code>. Data is getting saved for <code>attributes_name</code>.</p> <p>So,when i click "save" button second time,I get double the textfield than before.which are empty.</p> <pre><code>NSFetchRequest * request = [[NSFetchRequest alloc] init]; [request setEntity:[NSEntityDescription entityForName:@"Attributes" inManagedObjectContext:self.context]]; [request setPredicate:[NSPredicate predicateWithFormat:@"attribute_name in %@", applicationDelegate.attributesTextArray]]; NSError * error; NSArray * anArray = [context executeFetchRequest:request error:&amp;error]; for (int i = 0; i &lt; [applicationDelegate.attributesTextArray count]; i++) { attributesObject = (Attributes*)[NSEntityDescription insertNewObjectForEntityForName:@"Attributes" inManagedObjectContext:self.context]; if (anArray.count == 0) { attributesObject.attribute_name = [applicationDelegate.attributesTextArray objectAtIndex:i]; attributesObject.attribute_value = [applicationDelegate.attributeGradesArray objectAtIndex:i]; } else { attributesObject.attribute_value = [applicationDelegate.attributeGradesArray objectAtIndex:i]; } [testArray addObject:attributesObject]; } for (int o=0; o&lt;[testArray count]; o++) { NSData * retrievedData = [[NSUserDefaults standardUserDefaults] objectForKey:@"ProjectManagedObject"]; NSURL * retrievedUrl = [NSKeyedUnarchiver unarchiveObjectWithData:retrievedData]; NSManagedObjectID * projectObjectId = [applicationDelegate.persistentStoreCoordinator managedObjectIDForURIRepresentation:retrievedUrl]; projectObject = (Project*)[applicationDelegate.managedObjectContext objectWithID:projectObjectId]; [projectObject addAttributesObject:[testArray objectAtIndex:o]]; NSError * error; if (![context save:&amp;error]) { NSLog(@"UNABLE TO SAVE DATA: %@", [error userInfo]); } </code></pre> <p>What exactly is going wrong here ? </p> <p>UPDATE: </p> <pre><code>NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription * entity = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; NSError * error; NSArray * fetchedObjects = [context executeFetchRequest:fetchRequest error:&amp;error]; NSLog(@"OBJECT FETCHED:%d", [fetchedObjects count]); if (fetchedObjects.count != 0) { Project * project_ = [fetchedObjects objectAtIndex:applicationDelegate.projectNumber]; NSArray * saved_attributes = [project_.attributes allObjects]; if (saved_attributes.count != 0) { //Update "attribute-value" } else { // create new attribute and insert both "attribute_name" and "attribute_value" ? } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload