麻烦排序CoreData支持NSArrayController

时间:2014-07-06 21:21:34

标签: cocoa core-data nsarraycontroller

我试图从我的Core Data支持的NSArrayController中检索已排序对象的数组。一旦ManagedObjectContext准备就绪,我就会触发一个Notification,该对象拥有NSArrayController,它会监听,然后使用sortDescriptors设置获取请求。但是,数据永远不会排序。我现在已经在这几个小时了。

我在数组中获得了预期的数据,但它根本没有排序。任何人都有任何想法我错了吗?

// Elsewhere in code
...
_charts = [[NSArrayController alloc] init];
_charts.entityName = @"Charts";
...

// When Cored Data and Managed Object Context is ready
-(void)mangagedObjectContextReady
{
    NSManagedObjectContext *moc = ((AppDelegate *)[NSApp delegate]).managedObjectContext;
    _charts.managedObjectContext = moc;
    _charts.usesLazyFetching = NO;
    _charts.automaticallyPreparesContent = YES;
    _charts.automaticallyRearrangesObjects = YES;

    // Create fetch request for data
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    fetchRequest.entity = [NSEntityDescription entityForName:@"Charts" inManagedObjectContext:moc];

    // Create the sort descriptors for the Charts entity
    // Name and chartType are properties on the entity "Charts"
    NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    NSSortDescriptor *chartTypeDescriptor = [[NSSortDescriptor alloc] initWithKey:@"chartType" ascending:YES selector:@selector(caseInsensitiveCompare:)];
    fetchRequest.sortDescriptors = @[nameDescriptor, chartTypeDescriptor];

    NSError *error = nil;
    BOOL success = [_charts fetchWithRequest:fetchRequest merge:NO error:&error];
    if(!success)
        NSLog(@"Error %@:", [error localizedDescription]);
    else {
       [_charts rearrangeObjects];
       [_tv reloadData];
    }

我使用此方法获取数组或已排序的对象..但数据未按上述排序描述符排序?

[_charts.arrangedObjects objectAtIndex:row]

1 个答案:

答案 0 :(得分:0)

_charts.sortDescriptors = @ [nameDescriptor,chartTypeDescriptor];

相关问题