gpt4 book ai didi

iOS App 崩溃 com.apple.root.background-qos

转载 作者:行者123 更新时间:2023-11-28 08:04:19 24 4
gpt4 key购买 nike

在应用程序中发现应用程序崩溃。我怀疑这可能是因为 firebase 观察者的代码。由于在用户案例中,用户可以从一个事件转到用户配置文件(参与此事件),然后从用户配置文件可以返回此事件我需要一个引用句柄来删除特定观察者。因此,我编写了以下代码。

代码片段

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)
}
}
}

来自 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)
}
}

一个 UIViewController 的扩展

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()
}

}

崩溃堆栈

   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

最佳答案

我假设数据应用程序崩溃是由于运行时的功能不正确造成的。更改功能后,应用程序不再崩溃。

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")
}
}

}

关于iOS App 崩溃 com.apple.root.background-qos,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45613424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com