小编Chr*_*ris的帖子

核心数据 - 放弃更改

希望有人能解释发生了什么.

如果我从Core Data模型中获取了一个对象,请修改一个不持久或甚至在模型中定义的属性!然后再次销毁并获取对象,该值仍然是先前设置的.

为什么是这样?

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;
Run Code Online (Sandbox Code Playgroud)

输出是:

2 2

供参考:

+(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;
}
Run Code Online (Sandbox Code Playgroud)

model core-data objective-c

14
推荐指数
2
解决办法
6607
查看次数

标签 统计

core-data ×1

model ×1

objective-c ×1