Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3933758
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:38:15+00:00 2026-05-19T23:38:15+00:00

I am trying to make loading multiple entities more manageable in code. The code

  • 0

I am trying to make loading multiple entities more manageable in code. The code below is attached to an object that can load over 1000 times, so I’ve put it in a loop and attached a manageable array of entities.

The problem is, because I have no idea if a key is available in singleObject, I am getting a crash on [singleObject valueForKey:@”actor”] when I am on the directors entity. It’s because the key “actor” doesn’t exist. See my code here.

NSArray *entities = [[NSArray alloc] initWithObjects: @"actors", @"directors", @"subtitles", @"audios", nil];

for (NSString *anEntity in entities)
{
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:anEntity inManagedObjectContext:context];
    [request setEntity:entityDescription];
    NSArray *objects = [context executeFetchRequest:request error:&error];
    if (objects == nil) {
        // error
    }

    for (NSManagedObject *singleObject in objects)
    {
        if (singleObject != nil) 
        {
            if ([singleObject valueForKey:@"actor"] != nil)
            {
                [self.actors addObject:[singleObject valueForKey:@"actor"]];
            }
            else if ([singleObject valueForKey:@"director"] != nil)
            {
                [self.directors addObject:[singleObject valueForKey:@"director"]];
            }
            else if ([singleObject valueForKey:@"subtitle"] != nil)
            {
                [self.subtitles addObject:[singleObject valueForKey:@"subtitle"]];
            }
            else if ([singleObject valueForKey:@"audio"] != nil)
            {
                [self.audios addObject:[singleObject valueForKey:@"audio"]];
            }
        }
    }
}

[entities release];
[request release];'

How can I make this code work, and be vastly expandable, without having to put a bunch of try/catches around everything?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:38:16+00:00Added an answer on May 19, 2026 at 11:38 pm

    OK I think I found a way that works for me using NSDictionary instead. It works in my situation simply because my entity names are plural (actors) and my attributes are singular (actor). Here is what I came up with:

    NSMutableDictionary *entities = [[NSMutableDictionary alloc] init];
    [entities setValue:actors forKey:@"actors"];
    [entities setValue:directors forKey:@"directors"];
    [entities setValue:subtitles forKey:@"subtitles"];
    [entities setValue:audios forKey:@"audios"];
    
    for (NSMutableString *aKey in entities)
    {
        NSEntityDescription *entityDescription = [NSEntityDescription entityForName:aKey inManagedObjectContext:context];
        [request setEntity:entityDescription];
        NSArray *objects = [context executeFetchRequest:request error:&error];
        if (objects == nil) {
            // error
        }
    
        for (NSManagedObject *singleObject in objects)
        {
            if (singleObject != nil) 
            {
                if ([singleObject valueForKey:[aKey substringToIndex:[aKey length] - 1]] != nil)
                {
                    [[entities valueForKey:aKey] addObject:[singleObject valueForKey:[aKey substringToIndex:[aKey length] - 1]]];
                }
            }
        }
    }
    

    Details:

    I loop through the keys in the dictionary. I make them mutable so I can take the s off of the end of the key. Then I pull the value (which is an array) from that key and load the array using the key without the s.

    Somewhat complicated to read the code, but really slims down the manageability. Now all I need to do is add a new plural entity with matching singular attribute and just add the key/value pair to the dictionary.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.