ios - 几次打开后,无法打开 UIManagedDocument 文件似乎已损坏

标签 ios objective-c uimanageddocument

我试图在创建 UIManagedDocument 后打开它。几次成功打开文档后,似乎无法打开文档。如果我更改文档名称或再次删除/复制应用程序,它会起作用。每次启动应用程序时,都会在应用程序委托(delegate)中调用以下方法。

-(void) loadDataDocument {

    NSURL *documentURL= [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                             inDomains:NSUserDomainMask] lastObject] URLByAppendingPathComponent:@"TestDataDocument1"];
    self.document = [[UIManagedDocument alloc] initWithFileURL:documentURL];


    if(![[NSFileManager defaultManager] fileExistsAtPath:[documentURL path]]) {
    [self.document saveToURL:documentURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
        if (success) {
            [self documentIsReady];
        }else {
            NSLog(@"Tried to create new file. Cannot open document");
        }
    }];
    } else if (self.document.documentState == UIDocumentStateClosed) {
        [self.document openWithCompletionHandler:^(BOOL success) {
            if (success) {
               [self documentIsReady];
            }else {
               NSLog(@"Document was Closed. Cannot open document");
            }
        }];
    } else {
        [self documentIsReady];
    }

最佳答案

-(void) loadDataDocument {

    NSString * fileUrl = [NSString stringWithFormat:@"%@abc.txt",[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]];
    NSURL *documentURL= [NSURL URLWithString:fileUrl];

    UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:documentURL];

    if(![[NSFileManager defaultManager] fileExistsAtPath:[documentURL path]]) {
        [document saveToURL:documentURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            if (success) {
                NSManagedObjectContext * _context;
                _context = document.managedObjectContext;

            }
        }];
    } else if (document.documentState == UIDocumentStateClosed) {
             NSLog(@"document.documentState == UIDocumentStateClosed");
        NSString *fileDataStr = [[NSString alloc]initWithContentsOfURL:documentURL encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"%@",fileDataStr);
    } else {
        NSManagedObjectContext * _context;
        _context = document.managedObjectContext;

    }

}

注意: 'UIManagedDocument 只能读取文件包的文档'

关于ios - 几次打开后,无法打开 UIManagedDocument 文件似乎已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668140/

相关文章:

iphone - 无法设置委托(delegate)和调用方法

ios - 在 Swift 中子类化 UIView - 错误

ios - 我的应用启动画面保持白色

iphone - 如何使用键值编码在 ABPerson 中设置属性值

ios - 使用 UIImageView 上的 UIViewAnimationOptionTransitionCrossDissolve 对 UIView transitionWithView 的多个并发调用闪烁

ios5 - 使用未声明的标识符 'NSMigratePersistentStoresAutomaticallyOption'

ios - iOS 设备旋转时如何处理 View 交换

iphone - 如何在 ContainerView 内部的 ViewController 之间切换?

ios - 获取的结果 Controller 返回部分数 = 0

objective-c - UIManagedDocument 在后台线程中插入对象