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 7625215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:09:43+00:00 2026-05-31T05:09:43+00:00

i have started to integrate core data in my current project and it is

  • 0

i have started to integrate core data in my current project and it is the first time i work with it.

i have managed to write new objects into my NSManagedObjectContext and read them out. my problem is, that after closing the app all objects are lost. here is one of the functions that adds new objects:

-(void)addExpenseWithValue:(NSNumber *)value name:(NSString *)name type:(BOOL)type andDate:(NSDate *)date{

//get the right managed object context
NSManagedObjectContext *context = self.managedObjectContext;      

//creat a new expense in context
NSManagedObject *newExpense = [NSEntityDescription insertNewObjectForEntityForName:@"Expense" inManagedObjectContext:context];

//Get Number representation of month and year
NSNumber *monthNumber = [self getMonthNumber:date];
NSNumber *dayNumber = [self getDayNumber:date];

//set values of the expense
[newExpense setValue:[NSDate date] forKey:@"date"];
[newExpense setValue:value forKey:@"value"];
[newExpense setValue:name forKey:@"name"];
[newExpense setValue:[NSNumber numberWithBool:type ] forKey:@"expenseType"];
[newExpense setValue:monthNumber forKey:@"month"];
[newExpense setValue:dayNumber forKey:@"day"];
[self storeData];
}


-(void)storeData{
NSError *error = nil;
[managedObjectContext save:&error];

}

the objects are actually stored and i can read them out afterwards. as far as i know it has something to do with the persistentStore and persistentStoreCoordinator. my appDelegate has the following methods implemented:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
    return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory]
                                           stringByAppendingPathComponent: @"<Project Name>.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc]
                              initWithManagedObjectModel:[self managedObjectModel]];
if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                             configuration:nil URL:storeUrl options:nil error:&error]) {
    /*Error for store creation should be handled in here*/
}

return persistentStoreCoordinator;
}

- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
    return managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
    managedObjectContext = [[NSManagedObjectContext alloc] init];
    [managedObjectContext setPersistentStoreCoordinator: coordinator];
}

return managedObjectContext;
}

 - (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
    return managedObjectModel;
}
managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil] ;

return managedObjectModel;
}



- (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
 }

UPDATE:

i have added a small testfunction, to experiment it gets called in the first viewController:

-(void)test{

NSArray* stores = [[managedObjectContext persistentStoreCoordinator]persistentStores];
if ([stores objectAtIndex:0]==nil) {
    NSLog(@"no stores found");
}
else
    NSLog(@"%d", [stores count]);

}

managedObjectContext is the applications NSanagedObjectContext as initiated in the appdelegate. it exists, the array gets allocated, but the count stays at 0.

2012-03-06 20:09:37.772 DailyBudget[1533:fb03] *** Terminating app due to uncaught
exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 0 beyond      
bounds for empty array'
*** First throw call stack:
(0x135f052 0x174fd0a 0x134b674 0x3c6a 0x3894 0x46cfbf 0x46d21b 0x4838b1 0x46cfbf
0x46d21b 0x46e0f1 0x3dcfec 0x3e2572 0x3dc72b 0x3cbbc2 0x3cbce2 0x3cbea8 0x3d2d9a 0x25b3 
0x3a39d6 0x3a48a6 0x3b3743 0x3b41f8 0x3a7aa9 0x1f57fa9 0x13331c5 0x1298022 0x129690a
0x1295db4 0x1295ccb 0x3a42a7 0x3a5a9b 0x2488 0x23e5 0x1)
terminate called throwing an exception(gdb) 

at the moment of the error i have the following objects:

objects that exist

what am i missing?

  • 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-31T05:09:44+00:00Added an answer on May 31, 2026 at 5:09 am

    … 24 hours later. i was somehow able to solve it myself =)
    i think the problem layed within the storeURL. my new code was inspired from this post here:

    Adding CoreData to Existing Project (theappcodeblog.com)

    thanks for your guys for checking it out anyways =)

    new code (working):

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataTabBarTutorial.sqlite"];
    

    with function

    - (NSURL *)applicationDocumentsDirectory {
        return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started creating an app which uses a core data stack at the
We have started using Spring framework in my project. After becoming acquainted with the
For example I use Netbeans for PHP and have started to integrate PHPUnit &
I have an existing data model using openJPA and I am trying to integrate
I just started a new Haskell project and wanted to set up a good
I have finally started to take the time out to grok the continuous integration
I have started using Qunit to test my JS code. I am looking into
I've started work on a project that will be primarily acting as a Sync
i have just started using servlet. basically i am new to web projects. I
I'm trying to integrate some drawing functionality into my program. I have a JLabel

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.