如何在coredata中使用谓词和sortdecriptor

时间:2017-07-11 04:22:09

标签: swift core-data

我使用'sortdescriptor'来获取'nsmutablearray',它按创建日期排序,但问题是它没有按谓词过滤。我想同时应用sort和filter.i尝试过以下

let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: entityName)
    let sort = NSSortDescriptor(key: "date", ascending: false)
    let pre:NSPredicate = NSPredicate(format: "id == %d", 100)
    fetchRequest.predicate = pre
    fetchRequest.sortDescriptors = [sort]

1 个答案:

答案 0 :(得分:0)

你可以试试这个: -

change <Message> to your entityModel


let fetchRequest = NSFetchRequest<Message>.init(entityName: "Message")
fetchRequest.sortDescriptors = [NSSortDescriptor.init(key: "date", ascending: false)]
let predicate = NSPredicate.init(format: "id = %@","100")
fetchRequest.predicate = predicate