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");
}
4

1 回答 1

0

暂时结束这个问题。我恢复使用传统的核心数据堆栈而不是 UIManagedDocument。这是非常不可靠的。

于 2012-05-25T22:29:49.013 回答