IPhone核心数据持久性&生命周期问题

时间:2010-01-24 14:12:07

标签: iphone core-data persistence lifecycle

每当我建立&运行我的程序我注意到在以下位置创建了一个新目录: / Users / Username / Library / Application Support / iPhone Simulator / User / Applications

因此,我无法在应用程序构建之间保留核心数据。我想解决这个问题的方法(从测试的角度来看)就是只需使用iphone模拟器通过按下圆形菜单按钮退出应用程序并重新运行我的应用程序。即,不构建它,而是通过模拟器重新运行它以查看数据是否持久存储在核心数据中。

现在我想检查每次运行应用程序时数据是否持久化。我正在使用的事件是:

  • (void)applicationDidFinishLaunching:(UIApplication *)application

但它只是在我建立&运行应用程序,但每次重新启动应用程序时都不会被激活 - 通过iphone模拟器(即按下菜单按钮,然后重新运行我的程序)。

我应该使用另一个事件吗?如果我每次加载应用程序时都会触发一个事件,我想我可以检查核心数据是否包含数据,如果不是,我只用xml文件填充它来初始化它,如果确实有的话数据我什么也没做。听起来不对?如果是这样,那个事件叫什么?

4 个答案:

答案 0 :(得分:5)

-applicationDidFinishLaunching:每次你的应用程序启动时都会调用,无论是来自调试器,还是触发Springboard(启动器)中的图标,还是设备中的任何一个。

在SIM卡上,为您的应用创建了... / Applications目录中的文件夹,其中存储的所有数据都将保留。每次构建和运行应用程序时,文件夹的实际名称都会更改,但内容将保持不变,因此您可以在那里存储数据。

答案 1 :(得分:1)

Ben是对的。您没有看到-applicationDidFinishLaunching的原因是因为从模拟器启动时调试器没有运行,该方法仍在触发。

听起来你还处于核心数据开发过程的早期阶段。启用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.
}

每次更改数据模型时,都不必破坏数据存储,这样就可以让Core Data智能地为您更新模型。

对不起,这有点偏离主题,但我花了很多时间擦除并重新加载我的数据存储,因为我没有意识到这样的轻量级迁移。

答案 2 :(得分:1)

正如vfn所写,您需要附加调试器或将日志值保存到磁盘。

我正在做OAuth,这需要模拟器离开应用程序并进行一些身份验证是Safari,然后Safari将使用URL方案重新打开应用程序。这意味着我无法获得应用程序退出后记录的不同身份验证步骤的日志。

无论如何,我写了这个类,它会将消息记录到位于〜/ user * / library / application support / iPhone Simulator / user / yourapp * / documents

中的“log.txt”

* user和yourapp当然是变量名。

//
//  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

答案 3 :(得分:0)

您是否尝试过使用

-(void)applicationDidBecomeActive { 

}