在我的应用中,我正在使用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)
答案 0 :(得分:14)
如果您出于任何原因需要使用两个谓词(scale_fill_gradientn(colours = c("blue","white","red"),
values = c(0,5,10),
guide = "colorbar", limits=c(0,10))
个对象)而不是一行:
NSPredicate
然后使用"(userid == %@) AND (is_deleted == %@)"
API:
NSCompoundPredicate
答案 1 :(得分:1)
获取和排序
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>