领域在MKAnnotation

时间:2017-12-10 18:03:25

标签: swift realm mkmapview mkannotation

目标是在MKAnnotation's标题和副标题上获得反向关系属性。

我有MainObject,其关系属性为Location对象:

class MainObject: Object, StaticMappable {
    @objc dynamic var location: Location?
    @objc dynamic var name: String        = ""

链接的Location对象与MainObject

之间存在反比关系
class Location: Object, StaticMappable {
    @objc dynamic var latitude: Double  = 0.0
    @objc dynamic var longitude: Double = 0.0

    let mainObjects = LinkingObjects(fromType: MainObject.self, property: "location")
    var mainObject: MainObject? { return mainObjects.first }

问题是Location对象也符合MKAnnotation(所以我可以在mapView上轻松显示对象)

extension Location: MKAnnotation {
    // coordinate already set
    // set other optional vars
    public var title: String? { return mainObject?.name }
}

MainObject的集合是在主线程(com.apple.main-thread (serial))上获取的,但是当地图上显示Location时,MKAnnotation将在后台线程上处理( com.apple.vectorkit.dispatch.layoutqueue)这会导致明显的崩溃:Realm accessed from incorrect thread

MKAnnotation标题和副标题是get-only,因此我在创建注释时无法在主线程上设置它们。

我能做些什么吗?

0 个答案:

没有答案