我在两个表视图控制器中实现核心数据。第一个显示银行名称、城市和州,第二个显示银行的详细信息,如邮编、关闭日期等。因此,我的模型中有两个实体。第一个称为“BankInfo”,第二个称为“BankDetails”。这两个实体是一对一的关系。
当我从第一个控制器切换到第二个时,问题就出现了。看来第二个表视图控制器中获取的对象数始终为零。这意味着 FRC 什么也得不到。怎么了?
第一个表工作正常。而且我确信第二个实体正确地填充了数据。我尝试删除谓词,在这种情况下,FRC 应该返回实体中的所有对象。但它仍然没有返回任何东西..
#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