如何排序数组由valueForKey取 [英] how to sort array from fetch by valueForKey

查看:219
本文介绍了如何排序数组由valueForKey取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过多种方法运行编译数组的fetchResultController。最后一个单元格显示姓名和电话号码(排名)。我想借此从的tableview细胞和由数字(升序)排序。我试过nssortdescriptor在FRC但遇到问题时,我尝试调用'排名',因为它不是我取的实体的的keyPath。有没有我可以拦截此阵散发出来的FRC由valueForKey排序的地方:@排行榜?我将如何实现这个排序?

I have a fetchResultController that compiles an array by running through multiple methods. The final cells display a name and number (ranking). I'd like to take the cells from the tableview and sort them by the number (ascending). I've tried nssortdescriptor in the frc but run into issues when I try to call the 'ranking' because it isn't a keypath of the entity I'm fetching. Is there a place I can intercept this array that comes out of the frc to sort by valueForKey:@"ranking"? How would I implement this sort?

通过创建valueForKey的方法,它的周期是在这里:

The methods it cycles through to create the valueForKey is here:

- (NSExpressionDescription*) rankingExpressionDescriptionForTags:(NSSet*)itemToTag
{
    NSPredicate* p2 = [NSPredicate predicateWithFormat:@"SUBQUERY(itemToTag,$t,$t.tagName IN %@).@count > 0",[itemToTag valueForKey:@"tagName"]];
    NSExpression* rankExpresion = [(NSComparisonPredicate*)p2 leftExpression];
    NSExpressionDescription* rankExpDesc = [[NSExpressionDescription alloc] init];
    rankExpDesc.name = @"ranking";
    rankExpDesc.expression = rankExpresion;
    rankExpDesc.expressionResultType = NSInteger64AttributeType;

    return rankExpDesc;
}

- (NSExpressionDescription*) objectIDExpressionDescription
{
    NSExpressionDescription* expDesc = [[NSExpressionDescription alloc] init];
    expDesc.name = @"objectID";
    expDesc.expressionResultType = NSObjectIDAttributeType;
    expDesc.expression = [NSExpression expressionForEvaluatedObject];
    return expDesc;
}

- (NSFetchRequest*) rankingRequestForItem:(Item*)item 
{
    NSFetchRequest* r = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
    NSPredicate* p = [NSPredicate predicateWithFormat:@"SELF != %@",item.objectID];
    r.resultType = NSDictionaryResultType;


    r.propertiesToFetch = @[[self objectIDExpressionDescription],@"itemName",
                            [self rankingExpressionDescriptionForTags:[item mutableSetValueForKey:@"itemToTag"]]];


    r.predicate = p;
    r.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"itemName" ascending:YES]];


    return r;
}

和与细胞配置的FRC是在这里:

and the frc with cell config is here:

- (NSFetchedResultsController *)fetchedResultsController
{
    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSLog(@"selected item: %@, itemToTag: %@",selectedItem.itemName,[selectedItem.itemToTag valueForKey:@"tagName"]);
    NSFetchRequest *fetchRequest = [self rankingRequestForItem:selectedItem];
    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".


    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    NSError *error = nil;
    if (![self.fetchedResultsController performFetch:&error]) {

        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return _fetchedResultsController;
}


- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *item = [self.fetchedResultsController objectAtIndexPath:indexPath];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@",[item valueForKey:@"itemName"],[item valueForKey:@"ranking"]];
}

感谢您的帮助!

推荐答案

正如我解释<一个href=\"http://stackoverflow.com/questions/24103508/$p$pdicate-subquery-to-return-items-by-matching-tags/24104718?noredirect=1#comment37410578_24104718\">HERE,有没有办法(据我所知)排序根据排名前pression。你将不得不在内存中的结果进行排序,从而彻底作废了FRC的需要。

As I explained HERE, there is no way (AFAIK) to sort based on the ranking expression. You will have to sort the results in-memory, thus nullifying completely the need for a FRC.

要找特定情况下执行请求和排序内存:

Simply execute the request on the given context and sort in-memory:

NSError* error = nil;
NSFetchRequest* r = [self rankingRequestForItem:selectedItem];
NSArray* results = [context executeFetchRequest:r error:&error];
NSSortDescriptor* sd = [NSSortDescriptor sortDescriptorWithKey:@"ranking" ascending:NO];
results = [results sortedArrayUsingDescriptors:@[sd]];

结果阵列现在将持有的排序信息,用它作为你的表中的数据源。
存储的导致一个视图控制器属性,如:

The results array will now hold your sorted information, use it as a datasource for your table. Store the results in a view controller property like:

self.results = results;

这code可能去为您例如 viewDidLoad中方法。

This code might go in your viewDidLoad method for example.

你到处访问FRC得到一个对象,你现在访问你的数据源表中的信息(假设你只有1部分):

everywhere you access the FRC to get an object, you now access your table datasource for that information (assuming you have only 1 section):

NSIndexPath* someIndexPath = //some index path you might get as parameter
[self.results objectAtIndex:someIndexPath.row]; //return NSDictionary

这篇关于如何排序数组由valueForKey取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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