NSFetchedResultsController不会在IOS 5中显示数据

时间:2012-10-26 07:48:54

标签: objective-c ios5 core-data ios6 nsfetchedresultscontroller

我是IOS开发的新手。目前我正在构建一个iPad应用程序,从Web服务获取数据,将其保存在核心数据库中,并使用NSFetchedResultController在tableview中显示。

问题是什么在IOS6中它可以像魅力一样工作,在IOS 5.0中它保存数据但不在tableview中显示。它给出了以下错误。

 CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  The NIB data is invalid. with userInfo (null)

当我在IOS 5.1中测试时,它什么也没做。它不会在tableview中显示数据,也不会将数据存储在核心数据库中。

这是我的工作。在我看来,确实出现了我是否有可以使用的文件。

if (!self.genkDatabase) {  // we'll create a default database if none is set
        NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
        url = [url URLByAppendingPathComponent:@"Default appGenk Database"];
        self.genkDatabase = [[UIManagedDocument alloc] initWithFileURL:url]; // setter will create this for us on disk
    }

然后它转到我的二传手。然后在usedocument中检查我的文档是哪个状态。

- (void)setGenkDatabase:(UIManagedDocument *)genkDatabase
{
    if (_genkDatabase != genkDatabase) {
        _genkDatabase = genkDatabase;
        [self useDocument];
    }
    NSLog(@"Comes in the setdatabase methode.");
}

    - (void)useDocument
    {
        NSLog(@"Comses in the use document");
        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(@"create");
                [self setupFetchedResultsController];
                NSLog(@"create 2");
                [self fetchNewsIntoDocument:self.genkDatabase];
                NSLog(@"create 3");
            }];
        } else if (self.genkDatabase.documentState == UIDocumentStateClosed) {
            NSLog(@"closed news");
            // exists on disk, but we need to open it
            [self.genkDatabase openWithCompletionHandler:^(BOOL success) {
                [self setupFetchedResultsController];
            }];
        } else if (self.genkDatabase.documentState == UIDocumentStateNormal) {
            NSLog(@"normal");
            // already open and ready to use
            [self setupFetchedResultsController];
        }

    }

最后,我使用以下函数在文档中获取数据。

- (void)fetchNewsIntoDocument:(UIManagedDocument *)document
{
    dispatch_queue_t fetchNews = dispatch_queue_create("news fetcher", NULL);
    dispatch_async(fetchNews, ^{
        NSArray *news           = [GenkData getNews];
        [document.managedObjectContext performBlock:^{
        int newsId          = 0;
        for (NSDictionary *genkInfo in news) {
            newsId++;
            [News newsWithGenkInfo:genkInfo inManagedObjectContext:document.managedObjectContext withNewsId:newsId];
            NSLog(@"news inserted");
        }
        [document saveToURL:document.fileURL forSaveOperation:UIDocumentSaveForOverwriting completionHandler:NULL];
        }];

    });
    dispatch_release(fetchNews);
}

对于IOS 5.0中的问题,这是我的NSFetchResultController方法。

- (void)setupFetchedResultsController // attaches an NSFetchRequest to this UITableViewController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"News"];
    request.sortDescriptors = [NSArray arrayWithObjects:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES],nil];
    self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
                                managedObjectContext:self.genkDatabase.managedObjectContext
                                sectionNameKeyPath:nil
                                cacheName:nil];
}

我这样做就像斯坦福大学Paul Hegarty的榜样。您可以在here上找到示例 Photomania

希望有人能帮助我!

亲切的问候

0 个答案:

没有答案