核心数据上下文中的意外键值行为

发布于 2024-08-30 02:13:04 字数 940 浏览 8 评论 0原文

如果我创建一个字符串数组(通过键值编码),其中包含第一次存储在应用程序委托中的托管对象实体的属性名称,我会得到一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

金橙橙 2024-09-06 02:13:04

我从来没有弄清楚发生了什么。我放弃了,重建数据模型,一切都很好。

I never did figure out what was happening. I gave up, rebuilt the data model, and all was fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文