过滤领域swift对象需要计算

时间:2017-11-19 11:49:22

标签: swift3 realm nspredicate

我有以下对象模型:

    public class Geofence: Object, Mappable {
        @objc public dynamic var id: String?
        @objc public dynamic var name: String?
        @objc public dynamic var uuid: String?
        @objc public dynamic var type: String?
        @objc public dynamic var latitude = 0.0
        @objc public dynamic var longitude = 0.0
        @objc public dynamic var radius = 0
        ...
    }

我的要求是在特定距离的范围内获取 Geofence 对象,如下所示:

func getGeofences(location: CLLocation, inDistance: Double = 20000.0) -> [Geofence] {
        do {
            let realm = try Realm()   
            return realm.objects(Geofence.self).filter("type == 'AREA'").filter({
                location.distance(from: CLLocation(latitude: $0.latitude, longitude: $0.longitude)) + CLLocationDistance($0.radius) <= inDistance
            })
        } catch let error as NSError {
            print(error.localizedDescription)
            fatalError(error.localizedDescription)
        }
    }

然而,它的作用是:它只获取类型为=='AREA'的对象,并使用swift 标准过滤器(_ isInlcuded :( Geofence)抛出 - 在内存中执行计算 - &gt; Bool)重新抛出 - &gt; [Geofence]功能,如果我从领域1.000.000对象中获取并在内存中进行计算,则可能相当昂贵。

我需要的是使用realm直接执行计算,并从领域中仅获取相关对象。

这可能使用领域吗? (领域子查询?)

非常感谢任何帮助。谢谢。

0 个答案:

没有答案