无法从现有Core Data数据库读回

时间:2012-04-19 09:15:11

标签: core-data

任何人都可以帮助我。我真的被卡住了。

在我的代码中,如果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");
}

1 个答案:

答案 0 :(得分:0)

暂时解决问题。我恢复使用传统的Core Data堆栈而不是UIManagedDocument。这是非常不可靠的。