我MyClass:UITableViewController
NSFetchedResultsController
并遇到一些问题:
当我创建MyClass的新实例时,fetchedResultsController返回空fetchedObjects
。但是在这个班级的第一个例子中一切都好......
我做错了什么?
使用managaedCondext和其他所需变量一切正常(即没有错误只有空结果)
MyClass.h
@interface MyClass : UITableViewController <UISearchDisplayDelegate, NSFetchedResultsControllerDelegate>
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@end
MyClass.m
@implementation MyClass
@synthesize fetchedResultsController = _fetchedResultsController;
......
- (NSFetchedResultsController *)newFetchedResultsControllerWithSearch:(NSString *)searchString
{
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *callEntity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:_managedObjectContext];
[fetchRequest setEntity:callEntity];
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"attr == %@", _value];
NSMutableArray *predicateArray = [NSMutableArray array];
if(searchString.length)
{
// your search predicate(s) are added to this array
[predicateArray addObject:[NSPredicate predicateWithFormat:@"title CONTAINS[cd] %@", searchString]];
// finally add the filter predicate for this view
if(filterPredicate)
{
filterPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:filterPredicate, [NSCompoundPredicate orPredicateWithSubpredicates:predicateArray], nil]];
}
else
{
filterPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:predicateArray];
}
}
[fetchRequest setPredicate:filterPredicate];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[sortDescriptor release];
[fetchRequest setSortDescriptors: sortDescriptors];
[sortDescriptors release];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[DataManager shared].managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
aFetchedResultsController.delegate = self;
[fetchRequest release];
NSError *error = nil;
if (![aFetchedResultsController performFetch:&error])
{
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return aFetchedResultsController;
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil)
{
return _fetchedResultsController;
}
_fetchedResultsController = [self newFetchedResultsControllerWithSearch:nil];
return [[_fetchedResultsController retain] autorelease];
}
其他方法是标准的,没有额外的......
第一个瞬间就是(一切正常!):
MyClass *myClass = [[[MyClass alloc] initWithNibName:@"MyClass" bundle:nil] autorelease];
myClass.tabBarItem.image = [UIImage imageNamed:@"myClass"];
myClass.title = NSLocalizedString(@"myClass", @"myClass");
UINavigationController *myClassNavigationController = [[[UINavigationController alloc] initWithRootViewController:myClass] autorelease];
然后我尝试显示该类的实例:
MyClass *myClass1 = [[MyClass alloc] initWithNibName:@"MyClass" bundle:nil];
myClass1.hidesBottomBarWhenPushed = YES;
myClass1.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
[self.navigationController presentModalViewController:myClass1 animated:YES];
[myClass1 release];
什么都没有 - 没有错误的空tableView ......