我如何在单个提取请求中调用两个 NSPredicate。核心数据 iOS Swift

Posted

技术标签:

【中文标题】我如何在单个提取请求中调用两个 NSPredicate。核心数据 iOS Swift【英文标题】:How can i call two NSPredicate in single fetch request. Core Data iOS Swift 【发布时间】:2016-06-06 10:05:41 【问题描述】:

在我的应用中,我使用的是 Core Data。现在我想从数据库中获取数据,为此我正在使用获取请求。但我希望两个在单个提取请求中使用两个 NSPrediate,所以我该怎么做。

这是我的代码:

let feedfetch = NSFetchRequest(entityName: "Feed")
feedfetch.predicate = NSPredicate(format: "userid == %@", userid)

let fetchedPerson = try moc.executeFetchRequest(feedfetch)

但我想使用两个或更多谓词,我该怎么做呢?

我想在一个获取请求中调用所有这些,我该怎么做

let feedfetch = NSFetchRequest(entityName: "Feed")
feedfetch.predicate = NSPredicate(format: "userid == %@", userid)
feedfetch.predicate = NSPredicate(format: "is_deleted == %@", "0")
let sortDescriptor = NSSortDescriptor(key: "feed_id", ascending: false,selector: #selector(NSString.localizedStandardCompare))
feedfetch.sortDescriptors = [sortDescriptor]

let fetchedPerson = try moc.executeFetchRequest(feedfetch)

【问题讨论】:

NSPredicate(格式: "userid == \(userId) AND is_deleted == '0'") 使用 AND 运算符,例如 userid == %@ AND is_deleted == [NSNumber numberWithBool: FALSE] AND ...... @LucaDavanzo 很酷,它运行良好,但我怎样才能在其中添加 sortDescriptior? 我认为你不能在谓词中添加排序.. 你可以使用 CompoundPredicate 【参考方案1】:

如果您出于任何原因需要使用两个谓词(NSPredicate 对象)而不是一行:

"(userid == %@) AND (is_deleted == %@)"

然后使用NSCompoundPredicate API:

feedfetch.predicate = NSCompoundPredicate(type:.AndPredicate, subpredicates:[
    NSPredicate(format: "userid == %@", userid), 
    NSPredicate(format: "is_deleted == %@", "0")])

【讨论】:

但是我如何在这个请求中添加 NSSortDescriptor? @Andriy 与您的代码中的方式相同。排序描述符附加到 NSFetchRequest 而不是 NSPredicate【参考方案2】:

获取和排序

    let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate
    let context:NSManagedObjectContext = appDel.managedObjectContext

    //Getting the cards with a video from Core Data filtered by  ("save")  and be an empity string  ("")  and by ("All")
    //Fetching from DB Card
    let savedCardRequestForVideo:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest(entityName: "Card")

    //Creating mulit variables for filtering
    //Creating a variable "save" for filtering Core Data
    let savedCardFilterForVideo    = "save"

    //Creating a variable with an empty string "" for filtering Core Data
    let cardSortingIDForVideo      = ""

    //Creating a variable "All" for filtering Core Data
    let savedCardFilterForVideoAll = "All"

    //Creating the predicate array
     savedCardRequestForVideo.predicate = NSCompoundPredicate(type:.and, subpredicates:[
        NSPredicate(format: "saveVideoControl = %@", savedCardFilterForVideo),
        NSPredicate(format: "cardSortingID = %@", cardSortingIDForVideo),
        NSPredicate(format: "allData == %@", savedCardFilterForVideoAll)])


    //Here we are sorting the reuslts in a ascending order
    let savedSortDescriptorForVideo                = NSSortDescriptor(key: "businessName", ascending: ascendingOrDescending)
        savedCardRequestForVideo.sortDescriptors   = [savedSortDescriptorForVideo]

    //Creating a variable to hold the results that are being fetched
    let savedCardFilterResultsFoeVideo             = try! context.fetch(savedCardRequestForVideo) as! [NSManagedObject]

    //The variable savedCardsForVideo will hold the results array
    savedCardsForVideo  = savedCardFilterResultsFoeVideo  //This ia an Array<AnyObject>

【讨论】:

以上是关于我如何在单个提取请求中调用两个 NSPredicate。核心数据 iOS Swift的主要内容,如果未能解决你的问题,请参考以下文章

如何累积单个请求的解析调用?

如何异步处理 WCF 的传入调用?

如何从 Python 请求调用中提取 HTTP 响应正文?

如何使用两个 NSPredicates 获取请求 - Swift 2

您可以从位于单个结果集中的两个单独的表中提取数据吗? [不加入问题]

如何使用动态别名在单个请求中多次调用 GraphQL 突变