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