CoreData使NSNumber属性的部分工作不正确

时间:2015-03-18 08:06:29

标签: core-data

我在我的项目中使用CoreData fetchedResultsController。我想用Event : NSManagedObject属性对isDone两个部分进行排序,实际上是BOOL值。当我添加NSManagedObject子类时,我没有检查"使用标量属性来处理原始数据类型"。所以它是NSNumber类型

问题是部分名称和数字是否正确。但是" 0"部分中的事件不是全部isDone = FALSE和部分" 1"不是全部'isDone=YES',我的代码可能或有什么问题吗?

Event.h

@interface Event : NSManagedObject
@property (nonatomic, retain) NSDate * endDate;
@property (nonatomic, retain) NSNumber * isDone;
@property (nonatomic, retain) NSString * name;
end

初始化NSFetchedResultsController

- (NSFetchedResultsController *)fetchedResultsController {

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"Event" inManagedObjectContext:[self managedObjectContext]];
    [fetchRequest setEntity:entity];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"endDate" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];

    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController =
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                        managedObjectContext:[self managedObjectContext] sectionNameKeyPath:@"isDone"
                                                   cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    return _fetchedResultsController;

}

有任何帮助吗?谢谢!

------------- EDIT ------------------------

NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"endDate" ascending:YES];
    NSSortDescriptor *sort1 = [[NSSortDescriptor alloc]
                              initWithKey:@"isDone" ascending:YES];
    [fetchRequest setSortDescriptors:@[sort1,sort]];

1 个答案:

答案 0 :(得分:1)

在@pbasdf的帮助下,我将代码更改为以下内容。我知道当set sectionNameKeyPath aleady进行排序时,实际上并没有。您也可以查看IOS DEV CENTER sample code。希望帮助某人。

    NSSortDescriptor *sort = [[NSSortDescriptor alloc]
                              initWithKey:@"endDate" ascending:YES];
    NSSortDescriptor *sort1 = [[NSSortDescriptor alloc]
                              initWithKey:@"isDone" ascending:YES];
    [fetchRequest setSortDescriptors:@[sort1,sort]];