NSFetchedResultsController似乎是将空对象插入数组? [英] NSFetchedResultsController seems to be inserting empty objects into array?

查看:343
本文介绍了NSFetchedResultsController似乎是将空对象插入数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道发生了什么问题。我在我的数据模型中有一个实体,称为Quote,其中约3,000个存储在.sqlite中。我只是试图通过 NSFetchedResultsController 将这些实体的属性之一添加到 UITableView 的单元格中c>。我得到的错误是这行代码:

I'm not sure what's going wrong. I have one Entity in my data model, called Quote, and around 3,000 of them stored in a .sqlite. I'm just trying to get one of the attributes of those entities into the cells of a UITableView, by way of an NSFetchedResultsController. The error I'm getting is on this line of code:

NSFetchedResultsController * theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:@];

似乎问题是它试图加载一个空对象到数组。我猜这意味着它没有找到.sqlite文件中的对象。这是可能的情况吗?

It seems that the problem is that it's trying to load an empty object into an array. I'm guessing this means that it hasn't found the objects inside the .sqlite file. Is that likely to be the case? If so, how can I begin trying to track down why that might be?

这里有一些代码,如果你需要更多,请问:

Here's some code, if you need any more, please ask:

来自AppDelegate.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *nav = (UINavigationController *) self.window.rootViewController;
    FQQuotesTableViewController *quotesTableViewController = (FQQuotesTableViewController *) [[nav viewControllers] objectAtIndex: 0];
    quotesTableViewController.managedObjectContext = self.managedObjectContext;
    quotesTableViewController.test = @"Hello!";
    return YES;
}

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (_persistentStoreCoordinator != nil) {
        return _persistentStoreCoordinator;
    }

    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
    NSString *storePath = [documentsPath stringByAppendingPathComponent: @"Quotes.sqlite"];
    NSURL *storeURL = [NSURL fileURLWithPath: storePath];

    if (![[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) {
        NSURL *preloadURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Quotes" ofType:@"sqlite"]];
        NSError* err = nil;

        if (![[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&err]) {
            NSLog(@"Oops, could copy preloaded data");
        }
    }

    NSError *error = nil;
    _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![_persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return _persistentStoreCoordinator;
}

从我的ViewController.m >

And from my ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSError *error;
    if (![self.fetchedResultsController performFetch: &error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();    
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return [[_fetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> secInfo = [[_fetchedResultsController sections] objectAtIndex: section];
    return [secInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    NSManagedObject *blah = [self.fetchedResultsController objectAtIndexPath: indexPath];
    cell.textLabel.text = [blah valueForKey: @"quote"];

    return cell;
}

#pragma mark - Fetched Results Controller

- (NSFetchedResultsController *) fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName: @"Quote" inManagedObjectContext: [self managedObjectContext]];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending: YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    [fetchRequest setFetchBatchSize: 50];

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest: fetchRequest managedObjectContext: [self managedObjectContext] sectionNameKeyPath: nil cacheName: @""];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;
}


推荐答案

键(您的模型的属性名称) sortDescriptor ?我认为是问题。

Have you tried to set a key (a attribute name of your model) for sortDescriptor? I think it is the issue.

NSSortDescriptor *sortDescriptor =
  [[NSSortDescriptor alloc] initWithKey:nil  // set key for the descriptor
                              ascending:YES];

这篇关于NSFetchedResultsController似乎是将空对象插入数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆