iOS App crashed com.apple.root.background-qos

人走茶凉 提交于 2019-12-08 07:52:56

问题


An app crash was found in the application. I suspect that this could happen because of the code for firebase observers. Since in a user case, a user can go from one event to a user profile (which participates in this event) then from the user profile can go back to this event I needed a handle for ref to delete a specific observer. Because of this, I made the following code.

Snippet of code

fileprivate func firObserve(_ isObserve: Bool) {
    guard card != nil else { return }
    firCardObserverDBManager.observeParticipationCard(observer: self, card: card, isObserve: isObserve, success: { [weak self] (updatedCard) in
            debugPrint("updated card")
            self?.checkUpdatedCard(updatedCard)
        }, removed: { [weak self] in
            self?.isParticipationCardDateRemoved = true
            self?.presentCardRemovedAlert()
    }) { (error) in
        DispatchQueue.main.async {
            SVProgressHUD.showError(withStatus: error.localizedDescription)
        }
    }
}

from FIRCardObserverDBManager

    fileprivate var observeParticipationCardObservers = NSMapTable<AnyObject, AnyObject>(keyOptions: .weakMemory, valueOptions: .strongMemory)


func observeParticipationCard(observer: Any, card: CardModel, isObserve: Bool, success: ((_ updatedCard: CardModel) -> Void)?, removed: (() -> Void)?, fail: ((_ error: Error) -> Void)?) {
    let ref = Database.database().reference().child(MainGateways.cards.rawValue).child(card.id)

    if !isObserve {
        guard let handle = self.observeParticipationCardObservers.object(forKey: observer as AnyObject) as? UInt else { return }
        ref.removeObserver(withHandle: handle)
        observeParticipationCardObservers.removeObject(forKey: observer as AnyObject)
        return
    }

    if let observableVC = observer as? UIViewController {
        observableVC.firReferences.append(ref)
    }

    DispatchQueue.global(qos: .background).async {
        let handle = ref.observe(.value, with: { (snapshot) in
            if snapshot.value is NSNull {
                // not exist
                removed?()
            }
            guard let json = snapshot.value as? [String : Any] else { return }
            guard let updatedCard = Mapper<CardModel>().map(JSON: json) else { return }
            success?(updatedCard)
        }, withCancel: { (error) in
            debugPrint(error.localizedDescription)
            fail?(error)
        })
        self.observeParticipationCardObservers.setObject(handle as AnyObject, forKey: observer as AnyObject)
        debugPrint("observeParticipationCardObservers", self.observeParticipationCardObservers)
    }
}

an UIViewController's extension for it

extension UIViewController {

    private struct FirebaseQueues {
        static var firQueues = [DatabaseQuery]()
    }

    var firQueues: [DatabaseQuery] {
        get {
            guard let firQueues = objc_getAssociatedObject(self, &FirebaseQueues.firQueues) as? [DatabaseQuery] else { return [] }
            return firQueues
        }
        set {
            objc_setAssociatedObject(self, &FirebaseQueues.firQueues, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    private struct FirebaseReferences {
        static var firReferences = [DatabaseReference]()
    }

    var firReferences: [DatabaseReference] {
        get {
            guard let firReferenses = objc_getAssociatedObject(self, &FirebaseReferences.firReferences) as? [DatabaseReference] else { return [] }
            return firReferenses
        }
        set {
            objc_setAssociatedObject(self, &FirebaseReferences.firReferences, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }

    func removeFIRQueuesReferensesObservers() {
        for firQueqry in firQueues {
            firQueqry.removeAllObservers()
        }
        firQueues.removeAll()
        for firRef in firReferences {
            firRef.removeAllObservers()
        }
        firReferences.removeAll()
    }

}

Crash stack

   Crashed: com.apple.root.background-qos
    0  libswiftCore.dylib             0x1022ba574 swift_unknownRelease (__hidden#21375_:381)
    1  myapp                       0x1002e3fd0 specialized specialized _ArrayBufferProtocol._arrayOutOfPlaceUpdate<A where ...> (inout _ContiguousArrayBuffer<A.Element>, Int, Int, A1) -> () (EventsUpcomingVC.swift)
    2  myapp                       0x1002e0eb8 specialized Array._copyToNewBuffer(oldCount : Int) -> () (EventsUpcomingVC.swift)
    3  myapp                       0x100319a84 specialized ChatGeneralManager.(observeConversationModel(Bool, conversationID : String, updated : () -> ()?, fail : (Error) -> ()?) -> ()).(closure #1).(closure #1) (ChatGeneralManager.swift)
    4  myapp                       0x100311f70 ChatGeneralManager.(observeConversationModel(Bool, conversationID : String, updated : () -> ()?, fail : (Error) -> ()?) -> ()).(closure #1).(closure #1) (ChatGeneralManager.swift)
    5  myapp                       0x10032515c partial apply for ChatGeneralManager.(observeConversationModel(Bool, conversationID : String, updated : () -> ()?, fail : (Error) -> ()?) -> ()).(closure #1).(closure #1) (ChatGeneralManager.swift)
    6  libdispatch.dylib              0x185b9e9e0 _dispatch_call_block_and_release + 24
    7  libdispatch.dylib              0x185b9e9a0 _dispatch_client_callout + 16
    8  libdispatch.dylib              0x185baebac _dispatch_root_queue_drain + 888
    9  libdispatch.dylib              0x185bae7d0 _dispatch_worker_thread3 + 124
    10 libsystem_pthread.dylib        0x185da7100 _pthread_wqthread + 1096
    11 libsystem_pthread.dylib        0x185da6cac start_wqthread + 4

回答1:


I assumed that the data app crashes happen because of the incorrect functions in the runtime. After changing the function, I no longer have app crashes.

private var queuesKey = 0
private var referencesKey = 1

extension UIViewController {

    var firQueues: [DatabaseQuery]? {
        get {
            return objc_getAssociatedObject(self, &queuesKey) as? [DatabaseQuery]
        }
        set {
            objc_setAssociatedObject(self, &queuesKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
        }
    }

    var firReferences: [DatabaseReference]? {
        get {
            return objc_getAssociatedObject(self, &referencesKey) as? [DatabaseReference]
        }
        set {
            objc_setAssociatedObject(self, &referencesKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
        }
    }


    func removeFIRQueuesReferensesObservers() {
        if var _firQueues = firQueues {
            for firQueqry in _firQueues {
                firQueqry.removeAllObservers()
            }
//            debugPrint("removeFIRQueuesReferensesObservers _firQueues start", _firQueues.count, "firQueues", firQueues?.count ?? "nil")
            _firQueues.removeAll()
            firQueues?.removeAll()
//            debugPrint("removeFIRQueuesReferensesObservers _firQueues end", _firQueues.count, "firQueues", firQueues?.count ?? "nil")
        }

        if var _firReferences = firReferences {
            for firRef in _firReferences {
                firRef.removeAllObservers()
            }
//            debugPrint("removeFIRQueuesReferensesObservers _firReferences start", "_firReferences", _firReferences.count, "firReferences", firReferences?.count ?? "nil")
            _firReferences.removeAll()
            firReferences?.removeAll()
//            debugPrint("removeFIRQueuesReferensesObservers _firReferences end", "_firReferences", _firReferences.count, "firReferences", firReferences?.count ?? "nil")
        }
    }

}


来源:https://stackoverflow.com/questions/45613424/ios-app-crashed-com-apple-root-background-qos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!