0

I am making my first IOS application and using JSON and core data for getting and storing my data.

But for some reason or another it won't open it's NSDocument. This is what I am doing.

- (void)useDocument
{
    if (![[NSFileManager defaultManager] fileExistsAtPath:[self.genkDatabase.fileURL path]]) {
        // does not exist on disk, so create it
        [self.genkDatabase saveToURL:self.genkDatabase.fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
            NSLog(@"test1"); 
            [self setupFetchedResultsController];
            NSLog(@"test12");
            [self fetchFlickrDataIntoDocument:self.genkDatabase];

        }];
    } else if (self.genkDatabase.documentState == UIDocumentStateClosed) {
        NSLog(@"test2"); 
        // exists on disk, but we need to open it
        [self.genkDatabase openWithCompletionHandler:^(BOOL success) {
            NSLog(@"test4");
            [self setupFetchedResultsController];
            NSLog(@"test5");
        }];
    } else if (self.genkDatabase.documentState == UIDocumentStateNormal) {
        NSLog(@"test3");
        // already open and ready to use
        [self setupFetchedResultsController];
    }
}

- (void)setupFetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"News"];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"News.title" ascending:YES]];
    // no predicate because we want ALL the Photographers

    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                                        managedObjectContext:self.genkDatabase.managedObjectContext
                                                                          sectionNameKeyPath:nil
                                                                                   cacheName:nil];
    NSLog(@"test10");
}

When you look at my log, you see the following.

Test 2 Test 4 Test 10 Test 5

As you can see it opens it succefully. Then executes the method setupFetchedResultsController. But then it suddenly stops. I don't get any errors.

What It normally should do is go to the fetchFlickrDataIntoDocument method.

Can anybody help me please ?

4

1 回答 1

0

我相信 [self fetchFlickrDataIntoDocument:self.genkDatabase]; 应该在您的数据库不存在时调用此方法,因此您希望在磁盘上的文档目录中创建它。当您运行测试时,您的数据库已经存在并且尚未打开,因此您的程序尝试打开它。您基本上只需遵循条件语句:if (self.genkDatabase.documentState == UIDocumentStateClosed)。我不明白为什么你的程序应该调用,fetchFlickrDataIntoDocument:self因为它应该只在你的数据库不存在时发生。

于 2012-10-01T14:11:55.620 回答