开发者

IPhone Core Data Persistence & Lifecycle Question

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?


-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'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.


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


Have you tried working with

-(void)applicationDidBecomeActive { 

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜