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

  • SEARCH
  • Home
  • 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 7743637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:35:09+00:00 2026-06-01T09:35:09+00:00

When my application first runs I’m using some simple code to read in some

  • 0

When my application first runs I’m using some simple code to read in some data from a source and then saving it to core data to be read back in the future. Here is my applicationDidFinishLaunching method:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    categories = [[NSMutableArray alloc] init];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if (![defaults objectForKey:@"dataImported"]) {
        NSMutableArray *temp_Categories = [[NSMutableArray alloc] initWithObjects:@"Food & Drink", @"Medical", @"Hotel", @"Travel", nil];

        for(int i = 0; i < [temp_Categories count]; i++){
            //Insert a new object of type ProductInfo into Core Data
            NSManagedObject *categoryInfo = [NSEntityDescription
                                              insertNewObjectForEntityForName:@"Category" 
                                              inManagedObjectContext:self.managedObjectContext];

            //Set category entities values
            [categoryInfo setValue:[temp_Categories objectAtIndex:i] forKey:@"categoryName"];

            [categories addObject:[temp_Categories objectAtIndex:i]];
        }

        [defaults setObject:@"OK" forKey:@"dataImported"];
        [defaults synchronize];

        NSLog(@"Imported");
    }
    else {
        //read from core data
        NSError *error;
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription 
                                       entityForName:@"Category" inManagedObjectContext:self.managedObjectContext];
        [fetchRequest setEntity:entity];
        NSMutableArray *temp_Categories = [[NSMutableArray alloc] initWithArray:[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]];

        for (int i = 0; i < [temp_Categories count]; i++){
            NSString *category = [[temp_Categories objectAtIndex:i] objectForKey:@"categoryName"];
            [categories addObject:category];
            NSLog(@"Success");
        }

        NSLog(@"Read From Core");
    }

    return YES;
}

When I run this code the very first time, it works correctly, however when I run it in future (when it read from core data) it does not work, it doesn’t read in the array from core data and temp_Categories count = 0. Can anyone explain what I’m doing wrong? I’m having the same trouble trying to do the same thing with a plist file in another app.

Thanks,

Jack

EDIT 1 I’m using iOS 5.

EDIT 2 Data model screenshot:Data Model

  • 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-06-01T09:35:11+00:00Added an answer on June 1, 2026 at 9:35 am

    I managed to get it to work with this:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        categories = [[NSMutableArray alloc] init];
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
        if (![defaults objectForKey:@"dataImported"]) {
            NSMutableArray *temp_Categories = [[NSMutableArray alloc] initWithObjects:@"Food & Drink", @"Medical", @"Hotel", @"Travel", nil];
    
            for(int i = 0; i < [temp_Categories count]; i++){
                //Insert a new object of type Category into Core Data
                NSManagedObject *categoryInfo = [NSEntityDescription
                                                 insertNewObjectForEntityForName:@"Category" 
                                                 inManagedObjectContext:self.managedObjectContext];
    
                //Set category entities values
                [categoryInfo setValue:[temp_Categories objectAtIndex:i] forKey:@"name"];
    
                NSError *error;
                if (![self.managedObjectContext save:&error]) {
                    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
                }
    
                NSLog(@"Added category to Core Data");
    
                //Add to global array
                [categories addObject:[temp_Categories objectAtIndex:i]];
            }
    
            [defaults setObject:@"OK" forKey:@"dataImported"];
            [defaults synchronize];
    
            NSLog(@"Imported");
        }
        else {
            //read from core data
            NSError *error;
            NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
            NSEntityDescription *entity = [NSEntityDescription 
                                           entityForName:@"Category" inManagedObjectContext:self.managedObjectContext];
            [fetchRequest setEntity:entity];
            NSMutableArray *temp_Categories = [[NSMutableArray alloc] initWithArray:[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]];
    
            for (NSManagedObject *object in temp_Categories){
                [categories addObject: [object valueForKey:@"name"]];
            }
    
            NSLog(@"Read From Core");
        }
    
        return YES;
    }
    

    Thanks for all your help.

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

Sidebar

Related Questions

I am building my first Java application using GWT which must read in data
I’m building MVC3 code first application and to display data on my page I’m
I'm using ClickOnce to deploy my application. When the app runs the first time,
Im my application I have the following code, the first bit runs when my
I have an application that runs continuously, it creates and destroys classes some of
I have an Ant script which runs tests then deploys my application to a
This is my first WinForm application using the Entity Framework and I have to
I have an application that runs using as AsInvoker manifest, this uses ProcessStartInfo to
Im creating a database via a code first application, the sql server contains no
I am going to install first application in my android phone, but having some

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.