views:

358

answers:

4

Whenever I build & run my program I notice that a new directory gets created in: /Users/Username/Library/Application Support/iPhone Simulator/User/Applications

Therefore, there's no way for me to persist core data between application builds. The way I thought to get around this issue (from a testing point of view) was to just use the iphone simulator to exit the application by pressing the circular menu button and re-run my app. I.e., not build it but just rerun it via the simulator to see if the data is persisted in core data.

Now I wanted to check if the data is persisting each time the application is run. The event that I'm using is:

  • (void)applicationDidFinishLaunching:(UIApplication *)application

But it only fires after I build & run the application but doesn't get fired each time i restart the application - via iphone simulator (i.e., pressing menu button then rerunning my program).

Is there another event I should be using?? If I had an event that gets fired every time the application loaded I think I could just check to see if core data has data in it, if it doesn't i just populate it with an xml file to initialize it, if it does have data I don't do anything. Sound right? If so, what is that event called?

+5  A: 

-applicationDidFinishLaunching: will be called EVERY time your app launches, whether from the debugger, hitting the icon in the Springboard (launcher), or either of these on the device.

On the sim, a folder in the .../Applications directory is created for your app, and any data stored in there will be persisted. The actual name of the folder will change each time you build-and-run your app, but the contents will remain the same, so you can store data there.

Ben Gottlieb
I don't think it gets fired every time because if I have an NSLog(@"hello"); in -applicationDidFinishLaunching it'll just say that hello once even though i'll relaunch application several times via springboard.
Shnitzel
It is indeed called every time your app launches. The problem is that you are not using the NSLog properly.
vfn
To view NSLog() statements from the Simulator when not debugging, you need to open the Console app in /Applications/Utilities.
Ben Gottlieb
Ben, you were right in both things you said. data was being saved even though dir was renamed and the event was being called. I just had some wrong theories due to some weird bugs. ;)
Shnitzel
A: 

Have you tried working with

-(void)applicationDidBecomeActive { 

}
Garrett H
hmm that's weird, that event is also only getting called once after I build the thing but not after I relaunch it in springboard.
Shnitzel
+1  A: 

Ben's right. The reason you aren't seeing -applicationDidFinishLaunching is because the debugger doesn't run when you launch from the simulator, the method is still firing.

It sounds like you're still early in the Core Data development process. You'd probably benefit from turning on Lightweight Migration.

NSError *error;
NSURL *storeURL = <#The URL of a persistent store#>;
NSPersistentStoreCoordinator *psc = <#The coordinator#>;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
    [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

if (![psc addPersistentStoreWithType:<#Store type#>
    configuration:<#Configuration or nil#> URL:storeURL
    options:options error:&error]) {
    // Handle the error.
}

Instead of having to destroy your data store each time you make changes to the data model, this should allow Core Data to intelligently update the model for you.

Sorry this is a little off-topic, but I've spent a lot of time erasing and re-loading my data store because I didn't realize there was such a thing as this lightweight migration.

kubi
+1  A: 

Hi

As vfn writes, you either need to attach the debugger or persist the log values to the disk.

I was doing OAuth and that requires the simulator to leave the app and do some authentication is Safari and then Safari will reopen the app using an URL Scheme. This meant I could not get a log of the different authentication steps logged after the app had quit.

Anyways, I wrote this class that will log messages to "log.txt" situated in ~/user*/library/application support/iPhone Simulator/user/yourapp*/documents

*user and yourapp is of course variable names.

//
//  LogFile.m
//  
//
//  Created by RickiG on 11/30/09.
//  Copyright 2009 www.rickigregersen.com.. All rights reserved.
//

#import "LogFile.h"


@implementation LogFile

+ (void) stringToLog:(NSString *) str {

    NSDate *now = [NSDate date];
    NSDateFormatter *logTimeFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [logTimeFormatter setDateFormat:@"HH:mm:ss"];
    NSString *timeStr = [NSString stringWithFormat:@"%@", [logTimeFormatter stringFromDate:now]];
    NSString *logMsg = [NSString stringWithFormat:@"%@\n%@\n\n", timeStr, str];
    NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *path = [docsDirectory stringByAppendingPathComponent:@"log.txt"];

    NSData *dataToWrite = [[NSString stringWithString:logMsg] dataUsingEncoding:NSUTF8StringEncoding];

    // Check if file exists
    NSFileManager *fileManager = [NSFileManager defaultManager];

    if([fileManager fileExistsAtPath:path]) { // Returns a BOOL     

        NSData *dataFromFile = [[NSData alloc] initWithContentsOfFile:path];
        NSMutableData *combinedDataToWrite = [NSMutableData dataWithData:dataFromFile];
        [combinedDataToWrite appendData:dataToWrite];
        [combinedDataToWrite writeToFile:path atomically:YES];
        [dataFromFile release];

    } else {    

        [fileManager createFileAtPath:path contents:dataToWrite attributes:nil];
    }
}

@end
RickiG
yes you guys were right it was calling that method behind the scenes, and this log sample will come in handy, thank you.
Shnitzel