核心数据上下文中的意外键值行为
如果我创建一个字符串数组(通过键值编码),其中包含第一次存储在应用程序委托中的托管对象实体的属性名称,我会得到一个 NSString 数组,没有任何问题。如果我随后从代码中的同一入口点进行相同的调用,则同一集合将成为 NULL 对象的数组 - 即使核心数据上下文中的任何内容都没有更改。
一种没有吸引力的解决方法是每次都重新创建字符串数组,但我想知道是否有人猜测幕后发生了什么。
// Return an array of strings with the names of attributes the Activity entity
- (NSArray *)activityAttributeNames {
#pragma mark ALWAYS REFRESH THE ENTITY NAMES?
//if (activityAttributeNames == nil) {
// Create an entity pointer for Activity
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Activity" inManagedObjectContext:managedObjectContext];
NSArray *entityAttributeArray = [[NSArray alloc] initWithArray:[[entity attributesByName] allValues]];
// Extract the names of the attributes with Key-Value Coding
activityAttributeNames = [entityAttributeArray valueForKeyPath:@"name"];
[entityAttributeArray release];
//}
return activityAttributeNames;
}
If I create an array of strings (via key-value coding) containing the names of a Managed Object entity's attributes which are stored in the App Delegate the first time, I get an array of NSStrings without any problems. If I subsequently make the same call later from the same entry point in code, that same collection becomes an array of NULL objects- even though nothing in the Core Data Context has changed.
One unappealing work-around involves re-creating the string array every time, but I'm wondering if anyone has a guess as to what's happening behind the scenes.
// Return an array of strings with the names of attributes the Activity entity
- (NSArray *)activityAttributeNames {
#pragma mark ALWAYS REFRESH THE ENTITY NAMES?
//if (activityAttributeNames == nil) {
// Create an entity pointer for Activity
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Activity" inManagedObjectContext:managedObjectContext];
NSArray *entityAttributeArray = [[NSArray alloc] initWithArray:[[entity attributesByName] allValues]];
// Extract the names of the attributes with Key-Value Coding
activityAttributeNames = [entityAttributeArray valueForKeyPath:@"name"];
[entityAttributeArray release];
//}
return activityAttributeNames;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从来没有弄清楚发生了什么。我放弃了,重建数据模型,一切都很好。
I never did figure out what was happening. I gave up, rebuilt the data model, and all was fine.