0
我已經得到那個給我下面的崩潰報告我的應用程序崩潰:爲什麼NSOperation觸發這個崩潰?
http://crashes.to/s/bed19f6404b
的方法挑起的麻煩,這是一個:
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let MapThread = NSOperationQueue()
MapThread.name = "Map Update"
MapThread.addOperationWithBlock() {
if self.StartandStop.titleLabel?.text == "Start Flight" || self.StartandStop.titleLabel?.text == "Démarrez" {
if let location = locations.first as? CLLocation {
let lastLocation = locations.last as? CLLocation
var altitude = lastLocation?.altitude
dispatch_async(dispatch_get_main_queue(),{
self.mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 17, bearing: 0, viewingAngle: 0)
});
}
}
if self.StartandStop.titleLabel?.text == "Stop Flight" || self.StartandStop.titleLabel?.text == "Arrêtez" {
if let mylocation = self.mapView.myLocation as CLLocation? { // This is the line indicated in the stack trace
let appDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
if CMAltimeter.isRelativeAltitudeAvailable() {
println("Check")
appDelegate.maxAltitude = NSString(format: "%.01f", self.maxAlt*3.28084) + " Ft"
}
else {
let relativeAlt = (self.mapView.myLocation as CLLocation).altitude - appDelegate.elevation
appDelegate.altitudes.append(Float(relativeAlt)*3.28084)
self.altitude = Float(relativeAlt)
if appDelegate.maxAltitude == "" {
appDelegate.maxAltitude = "0.0 Ft"
}
}
var lastDistance = self.Distance(self.lastLocation, Coordinate2: (self.mapView.myLocation as CLLocation).coordinate)
self.distance += lastDistance
var lastSpeed = Float((self.mapView.myLocation as CLLocation).speed)
if self.groundspeed > lastSpeed {
appDelegate.groundspeed = NSString(format: "%.01f", Float((self.mapView.myLocation as CLLocation).speed)*1.94384) + " Kts"
}
self.groundspeed = lastSpeed
self.path.addCoordinate(self.mapView.myLocation.coordinate)
dispatch_async(dispatch_get_main_queue(),{
GMSPolyline(path: self.path).map = self.mapView
self.mapView.camera = GMSCameraPosition(target: self.mapView.myLocation.coordinate as CLLocationCoordinate2D, zoom: 17, bearing: 0, viewingAngle: 0)
});
self.lastLocation = (self.mapView.myLocation as CLLocation).coordinate
}
}
}
}
應用後發生崩潰在這種方法經常運行的情況下,會在很長一段時間內留在後臺。難道說NSOperation在後臺讓我很難過嗎?自從我從後臺線程更新UI之前,我遇到了麻煩。然後我添加了GCD派遣來繪製地圖,這固定了最後一個問題。我應該儘量從應用程序中儘可能地消除NSOperation?
恐怕我對這個詞不熟悉。什麼是對象的深層副本? – user3185748 2015-02-06 04:43:16
不用擔心。有關基本解釋,請參閱http://stackoverflow.com/a/184745/1445366。 – 2015-02-06 05:44:36