0
所以我有兩個實體。列表和評論。列表可以有多個相關的評論。使用fetchrequest謂詞過濾掉某些coredata項目。
extension Listing {
@NSManaged var listingTitle: String?
@NSManaged var comments: NSSet?
@NSManaged var listingLatitude: Double
@NSManaged var listingLongitude: Double
}
extension Comment {
@NSManaged var comment: String?
@NSManaged var commentId: String?
@NSManaged var rating: Int32
@NSManaged var username: String?
@NSManaged var listing: Listing?
}
使用NSPredicate我可以很容易地篩選位於特定地理區域的列表。
// Fetching data from CoreData
let fetchRequest = NSFetchRequest()
fetchRequest.predicate = NSPredicate(format: "listingLatitude BETWEEN {%f,%f} AND listingLongitude BETWEEN {%f,%f}", (latitude-0.10), (latitude+0.10), (longitude-0.10), (longitude+0.10))
let entity = NSEntityDescription.entityForName("Listing", inManagedObjectContext: self.mainMoc)
fetchRequest.entity = entity
fetchRequest.sortDescriptors = [sortDescriptor]
do {
listings = try self.mainMoc.executeFetchRequest(fetchRequest) as! [Listing]
}
這工作得很好,但我怎麼做到與評論過濾掉所有房源已給定的特定等級的實例?
是否有可能以某種方式與我的其他NSPredicate鏈接?以下內容似乎會引發錯誤: fetchRequest.predicate = NSPredicate(格式:「listingLatitude BETWEEN {%f,%f} AND listingLongitude BETWEEN {%f,%f}」AND ANY comments.rating> 4「,(緯度-0.10),(緯度+ 0.10),(經度0.10),(經度+ 0.10)) – ardevd
也似乎返回:由於未捕獲的異常'NSInvalidArgumentException'終止應用程序,原因:'對多密鑰不允許在這裏' – ardevd
沒關係!完全有效。不知道我以前做錯了什麼,謝謝! – ardevd