我不确定出了什么问题。我的数据模型中有一个名为Quote的Entity,其中大约有3,000个存储在.sqlite中。我只是试图通过UITableView
将这些实体的一个属性放入NSFetchedResultsController
的单元格中。我得到的错误就是这行代码:
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest: fetchRequest managedObjectContext: [self managedObjectContext] sectionNameKeyPath: nil cacheName: @""];
似乎问题是它正在尝试将空对象加载到数组中。我猜这意味着它没有找到.sqlite文件中的对象。可能是这种情况吗?如果是这样,我怎样才能开始尝试追踪可能的原因?
以下是一些代码,如果您还需要,请询问:
来自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
- (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;
}
答案 0 :(得分:1)
您是否尝试为sortDescriptor
设置密钥(模型的属性名称)?我认为这是问题所在。
NSSortDescriptor *sortDescriptor =
[[NSSortDescriptor alloc] initWithKey:nil // set key for the descriptor
ascending:YES];