2015-02-06 141 views
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?

回答

0

NSOperationQueueNSBlockOperation只是GCD的便利包裝,所以我不認爲你會從刪除它們中受益。

我有三個假設:

  1. CLLocation對象被一個線程改變,而你正在使用他們的另一個。
  2. 這可能是因爲locations是強制解包(「隱式解包」)。也許這個數組在你引用它們時被釋放,導致崩潰。 (這不應該發生,因爲你保持強烈的參考。)
  3. 這可能是一個Obj-C/Swift橋的問題 - 如果我正確地閱讀日誌,它似乎崩潰在objc_retain上,從Swift中解包後保留一個對象可選。

無論哪種方式,我不相信這是你的錯,但作爲一個實驗中,你可以做的locations深拷貝和使用你的操作中複製。

+0

恐怕我對這個詞不熟悉。什麼是對象的深層副本? – user3185748 2015-02-06 04:43:16

+0

不用擔心。有關基本解釋,請參閱http://stackoverflow.com/a/184745/1445366。 – 2015-02-06 05:44:36