有这个奇怪的问题,无论什么谓词我应用它只返回一个结果,如果我不添加任何谓词它返回表的所有行,我想要的只返回匹配progIds的行< / p>
我有programIds列表,基于我想要在TvInfo表中搜索。
.......
......
progId = [Int64(3211),Int64(16844)] // hardcored ids from tvInfo table
let tvInfoRequest: NSFetchRequest<TvInfo> = TvInfo.fetchRequest()
tvInfoRequest.predicate = NSPredicate(format: "progId == %i", argumentArray: progId)
tvInfoRequest.returnsDistinctResults = true
tvInfoRequest.resultType = .managedObjectResultType
var tvShows = [TvInfo]()
coreDataStack.managedObjectContext.performAndWait({
do{
let tvInfoResult = try self.coreDataStack.managedObjectContext.fetch(tvInfoRequest)
tvShows = tvInfoResult
} catch {
fatalError("Error fetching channels displayShow")
}
})
我想也许是progId的一些问题(类型不匹配Int64),所以我尝试使用tvShow名称获取(来自tvInfo表的精确showNames仍然只得到一个结果,(在数组中传递名字)
tvInfoRequest.predicate = NSPredicate(format: "progName MATCHES %@", argumentArray: ["Supercar Superbuild","Kanahiya Bhaiya Live"])
奇怪的是,我使用相同的逻辑从通道表中获取progIds并且其工作正常(使用精确匹配获得progId)并且其工作用于其他表也像从通道表中获取通道ID
// fetching progIds from channel table
let tvScheduleRequest: NSFetchRequest<TvSchedule> = TvSchedule.fetchRequest()
tvScheduleRequest.predicate = NSPredicate(format: "chId == %i", argumentArray: channelIds)
tvScheduleRequest.returnsDistinctResults = true
var progId = [Int64]()
coreDataStack.managedObjectContext.performAndWait({
do{
let tvScheduleResult = try self.coreDataStack.managedObjectContext.fetch(tvScheduleRequest)
if tvScheduleResult.count > 0 {
for array in tvScheduleResult {
progId.append(array.progId)
}
}
} catch {
fatalError("Error fetching channels to display show")
}
})
PS:
感谢您的帮助
答案 0 :(得分:0)
在谓词中尝试使用%@而不是%i。
tvInfoRequest.predicate = NSPredicate(格式:“progId ==%i”,argumentArray:progId)。
还有一件事要检查你传递给谓词progId的变量。