崩溃:libsystem_pthread.dylib:_pthread_wqthread

Crash: libsystem_pthread.dylib: _pthread_wqthread

这是我的应用程序在 AppStore 中的第 1 次崩溃。问题是我找不到解决这个问题的方法,因为我无法重现它,也不知道是什么原因造成的。崩溃日志说明如下:

线程 0.8 是 didUpdateLocations

我认为它可能在 checkStealRange() 中,但我看不出有什么问题。

func checkStealRange() {

    var objectsWithdistance = [PFObject]()
    stealobject = nil
    print("checkin steal and setting stealobject to nil")

    if nearbystreets.count != 0 {
        for object in self.nearbystreets {
            if let lon = object["lon"] as? Double, let lat = object["lat"] as? Double{
                let locationStreet = CLLocation(latitude: lat, longitude: lon)
                if let currentLocation = self.locationManager.location {
                    let distance = currentLocation.distance(from: locationStreet)
                    object["distance"] = distance
                    objectsWithdistance.append(object)
                } else {
                    if self.lastlocationregionset != nil {
                        let distance = self.lastlocationregionset!.distance(from: locationStreet)
                        object["distance"] = distance
                        objectsWithdistance.append(object)
                    }
                }
            }
        }
    } else {
        print("no nearby streets loaded to check for steal")
        stealButton.isEnabled = false
        return
    }

    if objectsWithdistance.count > 0 {
        print("objectsWithdistance count:", objectsWithdistance.count)
        let sortedArray = (objectsWithdistance as NSArray).sortedArray(using: [
            NSSortDescriptor(key: "distance", ascending: true)
            ])
        for object in sortedArray {
            guard let street = object as? PFObject else { continue }
            if let streetDistance = street["distance"] as? Double {
                var allowedDistance = Game.steal.stealMinDistance +
                    Game.steal.stealDistancePerLevel * Double(Main().level())
                if Main().getStealBoost() {
                    allowedDistance += 250
                }
                //print("distance:", streetDistance, "allowed:", allowedDistance)

                guard let user = street["current_owner"] as? PFUser else { continue }
                if user != PFUser.current() && streetDistance <= allowedDistance {
                    print("found steal")
                    self.stealobject = street
                    break
                }
            }
        }
    } else {
        print("checkin steal failed")
        stealButton.isEnabled = false
        return
    }
    print("nearbystreet count:", nearbystreets.count)

    if !self.timecheat && stealobject != nil {
        stealButton.isEnabled = true
    } else {
        stealButton.isEnabled = false
    }
}

使用 Parse localdate 存储重写函数,并解决了问题。

func checkStealRange() {

    stealobject = nil

    let query = PFQuery(className: "SHStreets")
    if let currentLocation = self.locationManager.location {
        let userGeoPoint = PFGeoPoint(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
        query.whereKey("geo", nearGeoPoint: userGeoPoint, withinKilometers: 5)
    } else {
        print("no location, returning from check steal range")
        self.stealButton.isEnabled = false
        return
    }
    query.fromLocalDatastore()
    query.findObjectsInBackground { (objects : [PFObject]?, error: Error?) in

        if error != nil {
            print(error as Any)
            self.stealButton.isEnabled = false
            return
        }

        if objects == nil || objects!.count == 0 {
            print("no nearby streets loaded to check for steal")
            self.stealButton.isEnabled = false
            return
        }

        if objects != nil {

            for (index, object) in objects!.enumerated() {
                guard let lon = object["lon"] as? Double else { continue }
                guard let lat = object["lat"] as? Double else { continue }
                let locationStreet = CLLocation(latitude: lat, longitude: lon)

                if let currentLocation = self.locationManager.location {
                    let distance = currentLocation.distance(from: locationStreet)

                    //steal distance
                    var allowedDistance = Game.steal.stealMinDistance + Game.steal.stealDistancePerLevel * Double(Main().level())
                    if Main().getStealBoost() {
                        allowedDistance += 250
                    }

                    print("distance for street:" , index + 1, "is", distance, "allowed:", allowedDistance)

                    guard let user = object["current_owner"] as? PFUser else { continue }
                    if user != PFUser.current() && distance <= allowedDistance {
                        print("found steal")
                        self.stealobject = object

                        if !self.timecheat && self.stealobject != nil && !self.stealinprogress {
                            self.stealButton.isEnabled = true
                        } else {
                            self.stealButton.isEnabled = false
                        }
                        return
                    }
                }
            }
        }
    }
}