2015-02-06 101 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