我遇到MyClass:UITableViewController
了NSFetchedResultsController
一些问题:当我创建 MyClass 的新实例时, fetchedResultsController 返回给我 empty fetchedObjects
。但是在这个类的第一个实例中,一切都很好......
我做错了什么?
使用 managaedCondext 和其他需要的变量一切正常(即没有错误只有空结果)
我的类.h
@interface MyClass : UITableViewController <UISearchDisplayDelegate, NSFetchedResultsControllerDelegate>
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
@end
我的班级.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...