NSFetchResultsController + sectionNameKeyPath + section order [英] NSFetchResultsController + sectionNameKeyPath + section order

查看:196
本文介绍了NSFetchResultsController + sectionNameKeyPath + section order的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体之间的一对多的实体关系:

  EntityP <  - > EntityC(Child)

属性和关系:



< pre class =lang-m prettyprint-override> EntityP.title
EntityP.dateTimeStamp
EntityP.PtoC(关系)

EntityC.title
EntityC.dateTimeStamp
EntityC.CtoP(关系)//可用于获取一个EntityP记录

我使用fetch results controller显示结果。这是我的fetch结果控制器的实现:

  #pragma mark  -  
#pragma标记获取结果控制器

- (NSFetchedResultsController *)fetchedResultsController {
//如果需要,设置获取的结果控制器
if(fetchedResultsController!= nil){
return fetchedResultsController;
}

//为实体创建获取请求
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];

//设置实体
NSEntityDescription * entity = [NSEntityDescription entityForName:@EntityCinManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

//设置谓词
//(忽略,我们要获取所有EntityC记录的列表)

//设置排序描述符部分,然后对Child - 对于行)
NSSortDescriptor * sortDescriptorPDate = [[NSSortDescriptor alloc] initWithKey:@CtoP.dateTimeStampascending:YES];
NSSortDescriptor * sortDescriptorDate = [[NSSortDescriptor alloc] initWithKey:@dateTimeStampascending:YES];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorPDate,sortDescriptorDate,nil];

[fetchRequest setSortDescriptors:sortDescriptors];
[fetchRequest setFetchBatchSize:20];

//创建并初始化fetch结果控制器
NSFetchedResultsController * aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@CtoP.titlecacheName:nil] ;
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

//清除内存
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptorPDate release];
[sortDescriptorDate release];
[sortDescriptors release];

NSError * error = nil;
if(![fetchedResultsController performFetch:& error]){
/ *
用代码来替换这个实现,以适当地处理错误。

abort()导致应用程序生成崩溃日志并终止。您不应在运送应用程序中使用此功能,但在开发过程中可能很有用。如果无法从错误中恢复,请显示一个警报面板,指示用户通过按主页按钮退出应用程序。
* /
NSLog(@未解析的错误%@,%@,错误,[错误userInfo]);
abort();
}

return fetchedResultsController;
}



现在,例如,如果我在持久存储中有以下数据: / p>

  EntityP.dateTimeStamp EntityP.title EntityC.dateTimeStamp EntityC.title 
今天B今天d
昨天A昨天a
今天B昨天c
昨天A今天b

注意:昨天和今天是NSDate格式。



然后,我应该按照以下顺序获得部分和行:

  A 
a
b

B
c
d

但是,排序不工作这样。我得到正确的顺序的行,但部分没有排序!我希望sortDescriptorPDate正在做他的工作。我失踪了什么?感谢预期。

解决方案

这似乎正在工作!我不知道什么改变了!评论时间70年01月01日原作者:Mustafa


I have a one to many entity relationship between two entities:

EntityP (Parent) <-->> EntityC (Child)

Attributes and Relationships:

EntityP.title
EntityP.dateTimeStamp
EntityP.PtoC (relationship)

EntityC.title
EntityC.dateTimeStamp
EntityC.CtoP (relationship) // Can be used to get "one" EntityP record

I use fetch results controller to show the results. Here's my implementation of the fetch results controller:

#pragma mark -
#pragma mark Fetched results controller

- (NSFetchedResultsController *)fetchedResultsController {
     // Set up the fetched results controller if needed
     if (fetchedResultsController != nil) {
          return fetchedResultsController;
     }

     // Create the fetch request for the entity
     NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

     // Set Entity
     NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityC" inManagedObjectContext:self.managedObjectContext];
     [fetchRequest setEntity:entity];

     // Set Predicate
     // (Ignore, we want to get list of all EntityC records)

     // Set Sort Descriptors (sort on Parent - for section, and then on Child - for rows)
     NSSortDescriptor *sortDescriptorPDate = [[NSSortDescriptor alloc] initWithKey:@"CtoP.dateTimeStamp" ascending:YES];
     NSSortDescriptor *sortDescriptorDate = [[NSSortDescriptor alloc] initWithKey:@"dateTimeStamp" ascending:YES];
     NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorPDate, sortDescriptorDate, nil];

     [fetchRequest setSortDescriptors:sortDescriptors];
     [fetchRequest setFetchBatchSize:20];

     // Create and initialize the fetch results controller
     NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"CtoP.title" cacheName:nil];
     aFetchedResultsController.delegate = self;
     self.fetchedResultsController = aFetchedResultsController;

     // Cleanup memory
     [aFetchedResultsController release];
     [fetchRequest release];
     [sortDescriptorPDate release];
     [sortDescriptorDate release];
     [sortDescriptors release];

     NSError *error = nil;
     if (![fetchedResultsController 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 fetchedResultsController;
}     

Now, for example, if i have following data in the persistence store:

EntityP.dateTimeStamp  EntityP.title    EntityC.dateTimeStamp   EntityC.title
Today                  B                Today                   d
Yesterday              A                Yesterday               a
Today                  B                Yesterday               c
Yesterday              A                Today                   b

Note: Yesterday and Today is in NSDate format.

Then i should get the sections and rows in following order (exactly):

A
 a
 b

B
 c
 d

But, the sort is not working like this. I'm getting the rows in correct order, but the sections are not ordered! I hope sortDescriptorPDate is doing his job. What am i missing? Thanking in anticipation.

解决方案

This seems to be working now! I'm not sure what changed! – Mustafa Sep 6 '10 at 4:03

这篇关于NSFetchResultsController + sectionNameKeyPath + section order的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆