我已按照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];
}
}