简体   繁体   English

核心数据:表格视图获取对象的FRC数量为零

[英]Core data: table view FRC number of fetched objects is zero

I am implementing core data in two table view controllers. 我在两个表视图控制器中实现核心数据。 The first display bank name, city and state, the second displays bank's detail info like zip, closing date etc. Accordingly I have two entities in my model. 第一个显示银行名称,城市和州,第二个显示银行的详细信息,如zip,截止日期等。因此我的模型中有两个实体。 First is called 'BankInfo', second one is called 'BankDetails'. 首先称为'BankInfo',第二个称为'BankDetails'。 The two entities are in a one-to-one relationship. 这两个实体是一对一的关系。

Problem arises when I segue from the first controller to the second. 当我从第一个控制器转到第二个控制器时出现问题。 It appears that number of fetched objects in the second table view controller is always zero. 看来第二个表视图控制器中的获取对象数始终为零。 It means FRC fetches nothing. 这意味着FRC什么都不拿。 What's going wrong? 出了什么问题?

The first table is working fine. 第一张表工作正常。 And I am sure the second entity is filled with data properly. 我确信第二个实体正确地填充了数据。 And I tried removing the predicate, in which case FRC should return all objects in entity. 我尝试删除谓词,在这种情况下,FRC应该返回实体中的所有对象。 But it still returns nothing.. 但它仍然没有返回..

#import "SecondViewController.h"
#import "AppDelegate.h"

@interface SecondViewController () <NSFetchedResultsControllerDelegate>
@property (nonatomic,strong) NSFetchedResultsController *fetchedResultsController;
@end

@implementation SecondViewController
@synthesize bankInfo = _bankInfo;
@synthesize fetchedResultsController = _fetchedResultsController;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.fetchedResultsController.delegate = self;

    NSError *error;

    if (![self.fetchedResultsController performFetch:&error]) {
        // Update to handle the error appropriately.
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }

    [self.tableView reloadData];

    NSLog(@"Frist fetched object is %@",[[self.fetchedResultsController fetchedObjects] objectAtIndex:0]);
}


- (void)setBankInfo:(BankInfo *)bankInfo
{
    _bankInfo = bankInfo;
}

- (NSFetchedResultsController *)fetchedResultsController
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"BankDetails"];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"info.state = %@",self.bankInfo.state];
    [request setPredicate:predicate];

    [request setSortDescriptors: [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"zip"
                                                                                    ascending:YES
                                                                                     ]]];
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = appDelegate.managedObjectContext;

    _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];

    return _fetchedResultsController;
}
// FRC delegate methods...
@end

Your getter function fetchedResultsController creates a new FRC each time it is called. getter函数fetchedResultsController每次调用时fetchedResultsController创建一个新的FRC。

So when you call 所以当你打电话

[[self.fetchedResultsController fetchedObjects] objectAtIndex:0]

then fetchedResultsController is called, creates a new FRC, stores it in _fetchedResultsController , and returns the new FRC. 然后fetchedResultsController ,创建一个新的FRC,将其存储在_fetchedResultsController ,并返回新的FRC。 Since performFetch has never been called on this new FRC, fetchedObjects is empty. 由于从未在此新FRC上调用performFetch ,因此fetchedObjects为空。 Also the delegate is not set for the new FRC. 代理人也没有为新的FRC设置。

The same thing happens probably in the table view delegate methods. 同样的事情可能发生在表视图委托方法中。

You can verify easily if this is the problem by setting a breakpoint in fetchedResultsController or adding some NSLog() to that method. 通过在fetchedResultsController设置断点或向该方法添加一些NSLog() ,可以轻松验证是否存在此问题。

What you perhaps wanted to do is the lazy getter function . 您可能想要做的是懒惰的getter函数 It also makes sense to set the delegate and perform the fetch in that method. 设置委托并在该方法中执行提取也是有意义的。

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController == nil) {
        /* ... create request, predicate, sort descriptor ... */
        _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:context sectionNameKeyPath:nil cacheName:nil];
        _fetchedResultsController.delegate = self;
        [_fetchedResultsController performFetch:&error]; // error handling omitted
    }
    return _fetchedResultsController;
}

If the search criteria change, you just set _fetchedResultsController = nil to force the creation of a new FRC. 如果搜索条件发生更改,则只需设置_fetchedResultsController = nil即可强制创建新的FRC。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM