目标是在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,因此我在创建注释时无法在主线程上设置它们。
我能做些什么吗?