【发布时间】:2012-11-23 00:05:57
【问题描述】:
我正在尝试获取我的数据,但结果为空!
首先,我有一个表格,其中包含来自一个 NSFetchedResultsController 的数据。直到这里,我的代码是正确的,而且我知道我有数据。
但是,我过滤了那个 NSFetchedResultsController,所以我需要另一个 NSFetchedResultsController 没有过滤器的记录,我的意思是,我的所有记录。因此,我尝试使用另一个 NSFetchedResultsController 再次获取数据,使用相同的代码,但结果始终为空!!!。
这是我正在使用的功能:
+(NSFetchedResultsController*)fetchedResultsControllerForEntity:(NSString*)entityName titleKey:(NSString*)titleKey sectionNameKeyPath:(NSString*)sectionNameKeyPath{
NSFetchRequest *request=[self fetchRequestForEntity:entityName titleKey:titleKey sectionNameKeyPath:sectionNameKeyPath];
return [[[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:[self managedContext]
sectionNameKeyPath:sectionNameKeyPath
cacheName:[NSString stringWithFormat:@"%@.%@.%@",entityName,sectionNameKeyPath,titleKey]]autorelease];
}
+(NSFetchRequest *) fetchRequestForEntity:(NSString*)entityName titleKey:(NSString*)titleKey sectionNameKeyPath:(NSString*)sectionNameKeyPath{
NSFetchRequest *request=[[[NSFetchRequest alloc]init]autorelease];
request.entity=[NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedContext]];
NSSortDescriptor* titleSortDescriptor=titleKey?[[NSSortDescriptor alloc ]initWithKey:titleKey ascending:YES]:nil;
NSSortDescriptor* sectionSortDescriptor=sectionNameKeyPath?[[NSSortDescriptor alloc ]initWithKey:sectionNameKeyPath ascending:YES]:nil;
NSMutableArray * sortDescriptors=[[NSMutableArray alloc]init];
if(sectionNameKeyPath){
[sortDescriptors addObject:sectionSortDescriptor];
[sectionSortDescriptor release];
}
if(titleKey){
[sortDescriptors addObject:titleSortDescriptor];
[titleSortDescriptor release];
}
request.sortDescriptors=sortDescriptors;
[sortDescriptors release];
request.fetchBatchSize=20;
return request;
}
我不明白,为什么如果我使用相同的代码,我的 NSFetchResultsController 是空的!!!
我试图从另一个实体中获取数据,但结果也是空的...
为什么我不见了?
谢谢
【问题讨论】:
-
方法名称前面的
+看起来有问题。为什么要创建类方法? -
因为我在另一个类中调用该函数。我把+改成了-,结果还是一样的……
-
这完全没有意义。类方法被发送到类(
[MyClass method];),实例方法被发送到一个实例([myObject method];)。它甚至不应该编译。我的结论是你没有调用你认为你正在调用的方法。 -
它编译!!而且我已经找到了解决方案!
标签: objective-c core-data xcode4.3 nsfetchedresultscontroller