15
Hope someone can explain what's going on.
If I get an object from my Core Data model, modify a property which is not persisted or even defined in the model! and then destroy and get the object again the value is still as previously set.
Why is this?
Promotion *promotion = [Promotion promotionWithId:[NSNumber numberWithInt:1512] inManagedObjectContext:context];
[promotion setQuantity:[NSNumber numberWithInt:2]];
NSLog(@"%d", [promotion.quantity intValue]);
promotion = nil;
promotion = [Promotion promotionWithId:[NSNumber numberWithInt:1512] inManagedObjectContext:context];
NSLog(@"%d", [promotion.quantity intValue]);
promotion = nil;
Output is:
2 2
For information purposes:
+(Promotion *)promotionWithId:(NSNumber *)promotionId inManagedObjectContext:(NSManagedObjectContext *) context {
NSFetchRequest *fetchReq = [[NSFetchRequest alloc]init];
[fetchReq setEntity:[NSEntityDescription entityForName:@"Promotion" inManagedObjectContext:context]];
NSPredicate *query = [NSPredicate predicateWithFormat:@"promotionId=%d", [promotionId intValue]];
[fetchReq setPredicate:query];
NSMutableArray *resultArray = [[NSMutableArray alloc]initWithArray:[context executeFetchRequest:fetchReq error:nil]];
if([resultArray count] > 0) {
return [resultArray objectAtIndex:0];
}
return nil;
}