1
任何人都可以在這裏幫助我。我很困難。無法從現有核心數據數據庫中讀回
在我的代碼中,如果Documents位置中不存在文件;我創建商店,將數據放入其中並從中讀回。這工作正常。 但是,如果文件已經存在於該位置,我無法從它讀回。我嘗試了很多,但我真的不確定它爲什麼不起作用。有些時候(實際上很少),非常相同的代碼將工作,但其他時間不會。
這是代碼。當它不起作用時,useDocument方法中的調試消息「End of useDocument」從不記錄。這是在UIManagedDocument上調用openWithCompletionHandler的方法。代碼進入方法,但從來沒有達到該方法的末尾!
- (void)setupFetchedResultsController
{ NSLog(@"In setupFetchedResultsController");
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"WorkOutTypes"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"workoutName" ascending:YES]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.workoutDatabase.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
NSLog(@"End of setupFetchedResultsController");
}
- (void)pushWorkoutDataIntoDocument:(UIManagedDocument *)document
{ NSLog(@"In pushWorkoutDataIntoDocument");
dispatch_queue_t pushQ = dispatch_queue_create("Push data", NULL);
dispatch_async(pushQ, ^{
NSArray *workOuts = [NSArray arrayWithObjects:@"Squats", @"Bench Press", @"Overhead Press", @"Bent Over Rows", nil];
[document.managedObjectContext performBlock:^{
for (NSString *workout in workOuts) {
[WorkOutTypes workoutName:workout inManagedObjectContext:document.managedObjectContext];
}
}];
});
dispatch_release(pushQ);
}
- (void)useDocument
{ NSLog(@"In useDocument");
NSLog(@"File path - %@", [self.workoutDatabase.fileURL path]);
if (![[NSFileManager defaultManager] fileExistsAtPath:[self.workoutDatabase.fileURL path]]) {
NSLog(@"1");
[self.workoutDatabase saveToURL:self.workoutDatabase.fileURL
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success){
[self setupFetchedResultsController];
[self pushWorkoutDataIntoDocument:self.workoutDatabase];
}];
}
else
if (self.workoutDatabase.documentState == UIDocumentStateClosed) {
NSLog(@"2");
[self.workoutDatabase openWithCompletionHandler:^(BOOL success){
NSLog(@"handler called");[self setupFetchedResultsController];}];
}
else
if (self.workoutDatabase.documentState == UIDocumentStateNormal) {
NSLog(@"3");
[self setupFetchedResultsController];
}
NSLog(@"End of useDocument");
}
- (void)setWorkoutDatabase:(UIManagedDocument *)workoutDatabase
{ NSLog(@"In setWorkoutDatabase");
if (_workoutDatabase != workoutDatabase) {
_workoutDatabase = workoutDatabase;
[self useDocument];
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
if (!self.workoutDatabase) {
NSLog(@"Came in");
NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
url = [url URLByAppendingPathComponent:@"Default Workouts Database"];
self.workoutDatabase = [[UIManagedDocument alloc] initWithFileURL:url];
NSLog(@"URL - %@", [url path]);
}
if (self.workoutDatabase) {
NSLog(@"workoutDatabase created");
}
else
NSLog(@"workoutDatabase not created");
}