开发者

Unexpected key-value behavior in a Core Data Context

开发者 https://www.devze.com 2022-12-28 07:37 出处:网络
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 wi

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 开发者_JAVA百科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;

}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号