diff --git a/Yep/Caches/ImageCache.swift b/Yep/Caches/ImageCache.swift index 88952756..07372c6f 100644 --- a/Yep/Caches/ImageCache.swift +++ b/Yep/Caches/ImageCache.swift @@ -157,8 +157,8 @@ class ImageCache { let fileName = message.localAttachmentName // 再保证一次,防止旧消息导致错误 - let latitude: CLLocationDegrees = abs(coordinate.latitude) > 90 ? 0 : coordinate.latitude - let longitude: CLLocationDegrees = abs(coordinate.longitude) > 180 ? 0 : coordinate.longitude + let latitude: CLLocationDegrees = coordinate.safeLatitude + let longitude: CLLocationDegrees = coordinate.safeLongitude dispatch_async(self.cacheQueue) { diff --git a/Yep/Realm/Models.swift b/Yep/Realm/Models.swift index 7011edd1..e05f2b42 100644 --- a/Yep/Realm/Models.swift +++ b/Yep/Realm/Models.swift @@ -195,7 +195,16 @@ class Coordinate: Object { dynamic var latitude: Double = 0 // 合法范围 (-90, 90) dynamic var longitude: Double = 0 // 合法范围 (-180, 180) - func configureWithLatitude(latitude: Double, longitude: Double) { + // NOTICE: always use safe version property + + var safeLatitude: Double { + return abs(latitude) > 90 ? 0 : latitude + } + var safeLongitude: Double { + return abs(longitude) > 180 ? 0 : longitude + } + + func safeConfigureWithLatitude(latitude: Double, longitude: Double) { self.latitude = abs(latitude) > 90 ? 0 : latitude self.longitude = abs(longitude) > 180 ? 0 : longitude } diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index 5f37e933..b6060938 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -1393,7 +1393,7 @@ func createAndSendMessageWithMediaType(mediaType: MessageMediaType, inFilePath f latitude = messageInfo["latitude"] as? Double { let coordinate = Coordinate() - coordinate.configureWithLatitude(latitude, longitude: longitude) + coordinate.safeConfigureWithLatitude(latitude, longitude: longitude) message.coordinate = coordinate } diff --git a/Yep/Services/YepServiceSync.swift b/Yep/Services/YepServiceSync.swift index ddfa3ec8..432f462b 100644 --- a/Yep/Services/YepServiceSync.swift +++ b/Yep/Services/YepServiceSync.swift @@ -877,7 +877,7 @@ func recordMessageWithMessageID(messageID: String, detailInfo messageInfo: JSOND latitude = messageInfo["latitude"] as? Double { let coordinate = Coordinate() - coordinate.configureWithLatitude(latitude, longitude: longitude) + coordinate.safeConfigureWithLatitude(latitude, longitude: longitude) message.coordinate = coordinate }