Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I update the boolean value of my core data entity's attribute?

This is really annoying me. In Objective-C, I have an Item entity with a boolean attribute Deleted. I would like to be able to set the value of Deleted to YES or 1.

This is my code:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

NSString *itemID = [[fetchedObjects objectAtIndex:(int)[currentTable selectedRow]] valueForKey:@"ItemID"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ItemID = %@", itemID];
[fetchRequest setPredicate:predicate];

NSError *error = nil;
Item *objectToDelete = [[managedObjectContext executeFetchRequest:fetchRequest error:&error] objectAtIndex:0];
if (objectToDelete == nil) {
    NSLog(@"ERROR");
}

[fetchRequest release];

[objectToDelete setValue:[NSNumber numberWithBool:YES] forKey:@"Deleted"];

[managedObjectContext save:&error];

Something to note is that I am able to successfully change different attributes; for example: I add the string -DEL to the end of the Item's attributes Code and Name. When I view a table of data, the strings for those values are updated correspondingly, however the value for Deleted continues to be 0.

like image 411
D'Arcy Rail-Ip Avatar asked Jun 02 '12 23:06

D'Arcy Rail-Ip


1 Answers

I would highly highly highly recommend using a different name for this attribute. NSManagedObject already has methods closely related to this name (in particular -isDeleted) which is potentially resulting in collisions with your custom attribute name. As the documentation for NSPropertyDescription says:

Note that a property name cannot be the same as any no-parameter method name of NSObject or NSManagedObject. For example, you cannot give a property the name "description". There are hundreds of methods on NSObject which may conflict with property names—and this list can grow without warning from frameworks or other libraries. You should avoid very general words (like "font”, and “color”) and words or phrases which overlap with Cocoa paradigms (such as “isEditing” and “objectSpecifier”).

Does your code work if you change the name of your property to something not likely to collide with any other property/method names?

like image 94
Sean Avatar answered Sep 20 '22 14:09

Sean