3

I'm trying to open a UIManagedDocument after creating it. It seems to be unable to open the document after a few times of successfully opening the document. If I change the document name or delete/copy the app again, it works. The below method is called in the app delegate every time the application launches.

-(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];
    }

}

malhal
  • 26,330
  • 7
  • 115
  • 133
Venkat S. Rao
  • 1,110
  • 3
  • 13
  • 29

1 Answers1

0
-(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;

    }

}

Note: 'UIManagedDocument can only read documents that are file packages'

yunas
  • 4,143
  • 1
  • 32
  • 38
  • Not sure what the purpose of this code is. Can you please give some context. I already check if the file exists. The problems comes when I open it. – Venkat S. Rao Mar 27 '13 at 20:04
  • oh i thought u only wanted to check wether the file exists or not. Can you brief a little what type of your document is ? – yunas Mar 27 '13 at 21:25
  • NSURL *documentURL= [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; it provides you the path till documents folder instead of a specific file you want to open... – yunas Mar 27 '13 at 21:39
  • example : /Library/Application%20Support/iPhone%20Simulator/6.1/Applications/44AFE37E-3C91-4F9F-9AB7-C5BB8D67A145/Documents/ – yunas Mar 27 '13 at 21:40
  • kindly see the modified method – yunas Mar 27 '13 at 22:02
  • I appended the a file name but it still doesn't seem to work. – Venkat S. Rao Apr 03 '13 at 12:37