NSFileManager fileExistsAtPath总是在设备中返回NO而不是在模拟器中

时间:2012-07-13 16:00:01

标签: iphone objective-c xcode ios5

我已按照iOS上的标准课程 - 核心数据课程编写了一个应用程序。我的应用程序在模拟器上完美运行,但在设备上导致问题,[NSFileManager defaultManager] fileExistsAtPath:始终返回NO,导致应用程序始终进入SAveFORcreating,一段时间后它会变慢并且不会从核心加载数据数据了。这不会发生在模拟器中。所以请一些帮助!

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (!self.recipiesDatabase) {  
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        self.fileurl = [url URLByAppendingPathComponent:@"RecipiesDatabase"];
        NSLog(@"URL IN FILE %@", url);
        self.recipiesDatabase = [[UIManagedDocument alloc] initWithFileURL:self.fileurl]; // setter will create this for us on disk
    }
}

- (void)useDocument
{


    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.recipiesDatabase.fileURL path] ]) {
        // does not exist on disk, so create it
        [self.recipiesDatabase saveToURL:self.recipiesDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            NSLog(@"No existe");
            [self setupFetchedResultsController];
            [self fetchFlickrDataIntoDocument:self.recipiesDatabase];

        }];
    } else if (self.recipiesDatabase.documentState == UIDocumentStateClosed) {
        // exists on disk, but we need to open it
        [self.recipiesDatabase openWithCompletionHandler:^(BOOL success) {
            NSLog(@"OPen");
            [self setupFetchedResultsController];
            //[self fetchFlickrDataIntoDocument:self.recipiesDatabase];

        }];
    } else if (self.recipiesDatabase.documentState == UIDocumentStateNormal) {
        // already open and ready to use
        NSLog(@"Normal");
        [self setupFetchedResultsController];

    }
}

0 个答案:

没有答案