gpt4 book ai didi

ios - 泄漏的监听器 firebase ios

转载 作者:行者123 更新时间:2023-11-28 12:50:36 24 4
gpt4 key购买 nike

我正在尝试为聊天功能加载消息框数据。

消息框加载为:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

if (PFUser.currentUser()!["firebaseUID"] !== nil)
{
print(PFUser.currentUser()!["firebaseUID"])
self.updateResultArray(PFUser.currentUser()!["firebaseUID"] as! String)
}
}

func updateResultArray(uid: String) {


let userName = String(PFUser.currentUser()!["username"])

//print("updateResultArray is getting called")
let userhandle = self.firebase.childByAppendingPath("users").childByAppendingPath(uid).childByAppendingPath("rooms").queryOrderedByValue()
.observeSingleEventOfType(.Value, withBlock: { roomsnapshot in

let enumerator = roomsnapshot.children
while let rest = enumerator.nextObject() as? FDataSnapshot {
self.roomArray.append(rest.key)
}

//get the latest message from all the rooms
if self.roomArray.isEmpty == false
{
for i in 0...self.roomArray.count-1
{
print("in the room loop \(self.roomArray[i])")

let messagehandle = self.messagesRef.childByAppendingPath(self.roomArray[i]).queryOrderedByKey().queryLimitedToFirst(1).observeSingleEventOfType(.Value, withBlock: {
messagesnapshot in

print("the messagesnapshot child count is \(messagesnapshot.childrenCount)")
let enumerator = messagesnapshot.children
while let rest = enumerator.nextObject() as? FDataSnapshot {

let sender = rest.value.objectForKey("sender") as? String
let reciever = rest.value.objectForKey("reciever") as? String

//print("sender is \(sender!) and reciever is \(reciever!)")


let eventhandle = self.firebase.childByAppendingPath("rooms").childByAppendingPath(self.roomArray[i]).observeSingleEventOfType(.Value, withBlock: { eventsnapshot in

if eventsnapshot.value is NSNull {
// The value is null
}
else
{
let eventAttr = eventsnapshot.value.objectForKey("eventAttributes") as? String
let eventDetails = eventsnapshot.value.objectForKey("eventDetails") as? String


//print("userName is \(userName)")
//print("sender is \(sender)")
if (userName != sender!) //for event joinee
{
let firstname1 = eventsnapshot.value.objectForKey("firstname1") as? String
self.otherNames.append(sender!)
self.resultsNameArray.append(firstname1!)
self.base4String = eventsnapshot.value.objectForKey("img1") as! String
self.resultsImageFiles.append(self.base4String)

}
else //for event creator
{
let firstname2 = eventsnapshot.value.objectForKey("firstname2") as? String
self.otherNames.append(reciever!)
self.resultsNameArray.append(firstname2!)
self.base4String = eventsnapshot.value.objectForKey("img2") as! String
self.resultsImageFiles.append(self.base4String)

}

let newlineChars = NSCharacterSet.newlineCharacterSet()
let evntArray = eventDetails!.componentsSeparatedByCharactersInSet(newlineChars).filter{!$0.isEmpty}
self.eventArray.append(evntArray[0])

self.eventdetailsArray.append(eventAttr!)

dispatch_async(dispatch_get_main_queue()) { () -> Void in
self.resultsTable.reloadData()
}

}

})

// self.firebase.removeAuthEventObserverWithHandle(eventhandle)
}

})

//self.messagesRef.removeAuthEventObserverWithHandle(messagehandle)
}

}
})


//firebase.removeAuthEventObserverWithHandle(userhandle)


}

因为我使用的是 observeSingleEventOfType,所以我没有编写代码来删除处理程序(我也尝试过)。

在个人聊天中,代码是这样的:

func refreshResults() {

print("the roomid is \(roomid)")

//update from firebase
let messagehandle = self.messagesRef.childByAppendingPath(roomid).queryOrderedByKey()
.observeEventType(.Value, withBlock: { messageTextsnapshot in

self.messageArray.removeAll()
self.senderArray.removeAll()


// print("the messageTextsnapshot child count is \(messageTextsnapshot.childrenCount)") // I got the expected number of items
let enumerator = messageTextsnapshot.children
while let rest = enumerator.nextObject() as? FDataSnapshot {


let text = rest.value.objectForKey("message") as? String
let sender = rest.value.objectForKey("sender") as? String


if text != nil && text != ""
{

self.messageArray.append(text!)
self.senderArray.append(sender!)

}

}


for subView in self.resultsScrollView.subviews {
subView.removeFromSuperview()
}

for var i = 0; i <= self.messageArray.count-1; i++ {


if self.senderArray[i] == userName {


if (self.messageArray[i].rangeOfString(self.acceptMessage) != nil)
{

let chatBubbleData = ChatBubbleData(text: self.messageArray[i], image:self.myImg, date: NSDate(), type: .AcceptMine)

self.addChatBubble(chatBubbleData)

}
else
{

let chatBubbleData = ChatBubbleData(text: self.messageArray[i], image:self.myImg, date: NSDate(), type: .Mine)

self.addChatBubble(chatBubbleData)

}

} else {

if (self.messageArray[i].rangeOfString(self.acceptMessage) != nil)
{

let chatBubbleData = ChatBubbleData(text: self.messageArray[i], image:self.otherImg, date: NSDate(), type: .Accept)

self.addChatBubble(chatBubbleData)

}
else
{

let chatBubbleData = ChatBubbleData(text: self.messageArray[i], image:self.otherImg, date: NSDate(), type: .Opponent)

self.addChatBubble(chatBubbleData)
}
}

let bottomOffset:CGPoint = CGPointMake(0, self.resultsScrollView.contentSize.height - self.resultsScrollView.bounds.size.height)
self.resultsScrollView.setContentOffset(bottomOffset, animated: false)

}


})

self.messagesRef.removeAuthEventObserverWithHandle(messagehandle)
}

还有一些其他类似的监听器。问题是当我从这个 View 返回时(个人聊天到消息框,内存消耗增加。我已经清除了所有数组并在使用后立即关闭了处理程序。但内存消耗仍然增加,有时在消息框中再次复制相同的行.我应该怎么解决这个问题。我试过使用observeSingleEventOfType 但它不是正确的解决方案,因为数据同步停止。

以此为引用: https://www.firebase.com/blog/2015-10-15-best-practices-uiviewcontroller-ios-firebase.html

最佳答案

由于持有对消息框对象的引用的监听器回调 block 引起的保留周期,您的消息框对象似乎未被释放。您可以通过在传递给其他对象的 block 中使用 [weak self] 来缓解这种情况。例如:

            .observeSingleEventOfType(.Value, withBlock: 
{
[weak self] roomsnapshot in

let enumerator = roomsnapshot.children
...

这使得“self”成为可选类型,然后您可以添加:

guard let strongSelf = self else { ... }

关于ios - 泄漏的监听器 firebase ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36654604/

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