Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Diary App Using Core Data Inserting and Saving Data Inserting a Managed Object

When is -(NSURL *)applicationDocumentsDirectory called?

Hey everyone! so when the view is dismissed, -(void)saveContext is called. Which is this:

  • (void)saveContext { NSError *error = nil; NSManagedObjectContext *managedObjectContext = self.managedObjectContext; if (managedObjectContext != nil) { if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) { // Replace this implementation with code to handle the error appropriately. // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } }

how is -(NSURL *)applicationDocumentsDirectory called from that ? Somehow the console logs everything out.. but I am missing where applicationDocumentsDirectory is called ?

1 Answer

Robin Malhotra
Robin Malhotra
14,883 Points

I think NSPersistent Store and the backend structures of CoreData handle that. Instead of Core Data, if you're dealing with static stuff, I recommend using something like this

''' @try { NSFileManager *fileMgr = [NSFileManager defaultManager]; NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"test2.sqlite"]; BOOL success = [fileMgr fileExistsAtPath:dbPath]; if(!success) { NSLog(@"Cannot locate database file '%@'.", dbPath); } if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)) { NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

        }

    NSString *myString = [NSString stringWithFormat:@"SELECT * FROM  event WHERE Cat = %@ ORDER By Date", ofType];// and Date = '%@',ofdate
    const char *sql= [myString cStringUsingEncoding:NSASCIIStringEncoding];


    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
        {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
        }else{

            while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
                Event * event = [[Event alloc] init];
                event.name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)];
                event.location = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
                event.startTime = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 2)];
                event.endTime = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
                event.catogory = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 4)];
                event.date = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 5)];
                event.text = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 6)];
                event.info = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 7)];
                [theevents addObject:event];
            }
        }

'''