我有一个继承自NSManagedObject的模型名为“Deletion”,代表一个coredata实体“Deletion”,所以在Deletion.m中我得到了以下方法:
- (NSString *)toJSON
{
NSArray *keys = [[NSArray alloc] initWithObjects:@"table", @"recordid", @"deleted", nil];
NSMutableString *json = [NSMutableString stringWithString:@""];
for(int i = 0; i < [keys count]; i++) {
NSString *key = [keys objectAtIndex:i];
id value = [self valueForKeyPath:key];
[json appendFormat:@"%@ \"%@\": \"%@\"", (i == 0 ? @"" : @", "), key, value];
NSLog(@"deletion entry %@ has value %@", key, value);
}
NSLog(@"deleted has value %@", self.deleted);
return [NSString stringWithFormat:@"{ %@ }", json];
}
这将在输出中打印以下内容:
2014-09-17 10:36:18.779 Heater (T)[905:60b] deletion entry table has value Piperun
2014-09-17 10:36:18.780 Heater (T)[905:60b] deletion entry recordid has value 256
2014-09-17 09:42:32.762 Heater (T)[876:60b] deletion entry deleted has value 0
2014-09-17 09:42:32.763 Heater (T)[876:60b] deleted has value 2014-09-17 07:42:29 +0000
为什么直接访问self.deleted有效,但现在通过valueForKeyPath?
[编辑:]我认为它与创建条目后保存模型有关。我在创建后调用[managedObjectContext save:&amp; error],没有别的,如果有帮助的话。
答案 0 :(得分:1)
好的,我发现了问题。事实证明这是Core Data boolean key value change not reflected in fetch
的副本问题在于:&#34;调用核心数据属性&#34;删除&#34;与NSManagedObject&#34;
的isDeleted属性冲突