崩溃后核心数据为空

时间:2014-11-12 22:57:42

标签: ios objective-c core-data nsfetchrequest

我正在模拟崩溃以测试我的应用如何响应并注意到重新启动应用时我的core data为空。到目前为止,使用核心数据和NSFetchRequest一直运行良好 - 我可以终止应用程序或关闭手机,数据始终保持不变。我正在使用核心数据将URLS存储到NSFileManager中存储的音频文件中。当我重新启动应用时,我可以看到这些文件仍在NSFileManagerNSFetchRequest返回的计数为零,tableview为空。有人以前经历过这个吗?

编辑:我一直在用这两行模拟崩溃:

    NSArray *array = @[];
    array[1];

在我的主视图控制器的viewDidLoad中,我调用以下内容:

self.managedDocument = [OSManagedDocument getInstance];
[self.managedDocument setUpDocument];  

managedDocument是一个包含我所有数据管理方法的单例。

@property (nonatomic, strong) NSManagedObjectContext * managedObjectContext;


+(OSManagedDocument*) getInstance
{
    static OSManagedDocument* _instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[OSManagedDocument alloc] init];
    });
    return _instance;
}

-(void) setUpDocument
{

    if(self.document == nil)
    {
        NSFileManager * fileManager = [NSFileManager defaultManager];
        NSURL * documentsDirectory = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
        NSString * documentName = @"MyRecordings";
        self.url = [documentsDirectory URLByAppendingPathComponent:documentName];
        self.document = [[UIManagedDocument alloc] initWithFileURL:self.url];


        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:[self.url path]];

        if(fileExists)
        {
             [self.document openWithCompletionHandler:^(BOOL success) {
                if(success)
                {
                    [self documentIsReady];

                }

             }];
        }
        else
        {
             [self.document saveToURL:self.url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
                if(success)
                {
                     [self documentIsReady];

                }
                else
                {
                    NSLog(@"Fail");
                }

            }];

        }
    }
}

-(void) documentIsReady
{

    if(self.document.documentState ==UIDocumentStateNormal)
    {
        self.managedObjectContext = self.document.managedObjectContext;
        NSLog(@"context is ready");
        [self fetchControllerFiles];
        [self recordingCount];
    }
}

-(int) recordingCount
{

    NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:@"Recording"];
    NSError *error;
    request.predicate = nil;
    request.sortDescriptors =nil;
    NSArray * recordings = [self.managedObjectContext executeFetchRequest:request error:&error];

    NSLog(@"recordings count %lu", (unsigned long)recordings.count);
    return (int)[recordings count];
}

当我的应用程序在崩溃后重新启动时,我会调用以下内容:

 -(void) appBecameActive:(NSNotification *)notification
 {
     self.managedDocument = [OSManagedDocument getInstance];
 }

我将此方法称为添加到核心数据

-(void) saveRecording:(NSMutableDictionary*) recordPackage inManagedObjectContext:(NSManagedObjectContext*) context
{

    Recording * recording = [NSEntityDescription insertNewObjectForEntityForName:@"Recording" inManagedObjectContext:context];

    recording.trackTitle = [recordPackage objectForKey:@"track label"];
    recording.audioURL = [[recordPackage objectForKey:@"audioURL"] absoluteString];
    recording.timeAdded = [NSDate date];
    recording.waveData = [recordPackage objectForKey:@"waveData"];
    recording.minAmpl = [recordPackage objectForKey:@"minAmpl"];
    recording.recordingNumber = [recordPackage objectForKey:@"recordingNumber"];


}

1 个答案:

答案 0 :(得分:0)

我一直指望自动保存来处理保存,它一直运行良好,直到我开始强制崩溃,看看应用程序是如何恢复的。由于数据在每次崩溃后都消失了,我决定尝试手动保存并且有效。现在即使在强制崩溃后数据也会出现。我假设这意味着没有及时发生自动保存。不完全确定如何解释它,但手动保存上下文解决了我的问题。

[self.document saveToURL:self.document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:^(BOOL success) {
    if(success)
    {
        NSLog(@" NEW FILE SAVED");
    }
    else
    {
        NSLog(@"FAIL TO SAVE");
    }

}];