因此,我有一個包含緯度和經度值的點的數據集,我想檢索它們並按照距離用戶當前位置進行排序。目前,我有以下內容:按計算結果對NSFetchRequest進行排序
mainMoc.performBlockAndWait {
// Fetching data from CoreData
let fetchRequest = NSFetchRequest()
fetchRequest.predicate = NSPredicate(format: "pointLatitude BETWEEN {%f,%f} AND pointLongitude BETWEEN {%f,%f}", (latitude-0.03), (latitude+0.03), (longitude-0.03), (longitude+0.03))
let entity = NSEntityDescription.entityForName("PointPrimary", inManagedObjectContext: self.mainMoc)
fetchRequest.entity = entity
let sortDescriptor = NSSortDescriptor(key: "pointTitle", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]
do {
points = try self.mainMoc.executeFetchRequest(fetchRequest) as! [PointPrimary]
} catch {
let jsonError = error as NSError
NSLog("\(jsonError), \(jsonError.localizedDescription)")
abort()
}
}
所以目前我只是根據標題進行排序。但是,如果我想要計算距離來說CLLocationCoordinate2D並基於該距離對fetchRequest結果進行排序,我將如何繼續?
非常感謝!