在应用程序中发现了应用程序崩溃。我怀疑这可能是因为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
答案 0 :(得分:0)
我认为数据应用程序崩溃是因为运行时中的函数不正确而发生的。更改功能后,我不再有应用程序崩溃。
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")
}
}
}