我正面临一个令人心碎的问题。。
所以我的申请一直都很顺利,直到现在。。
应用程序有一个TabController:有5个viewcontroller
在这个TabController上,我有一个计时器,每10秒获取一次数据
此数据将与通知一起发送到其子VCs之一"WorldMessages"
,后者随后更新其tableView
我不知道如何或为什么,但是每当我在应用程序中注销/登录时,worldwessages ViewController实例就会卡住。。
例如,在5次重新登录之后,我在内存中有5条WorldMessages VC。。
//重新登录意味着TabController及其子视图
已销毁,然后重新登录将创建
制表符//
我知道是因为穿线,但我不确定。当我删除几行时,它工作正常。有人能帮我吗?
(如果删除WorldMessages视图控制器中的这些行:
tableView.beginUpdates()
tableView.deleteRows(at: tableViewDeletes, with: .fade)
tableView.insertRows(at: tableViewInserts, with: .fade)
tableView.endUpdates()
那么这个应用程序运行良好……)
TabController,每隔10秒调用一次的函数:
@objc func fetchWorldMessages(scrollToTop: Bool){
worldMessagesFetch.fetchWorldMessages() { response, worldMessageData in
if let response = response {
if response.type == 1 {
// Fetched data
if let worldMessageData = worldMessageData {
DispatchQueue.main.async {
NotificationCenter.default.post(name: .updateWorldMessages, object: nil, userInfo: worldMessageData)
}
}
} else {
// Can not fetch data
self.handleResponses.displayError(title: response.title, message: response.message)
WorldMessagesStore.shared.clear()
NotificationCenter.default.post(name: .reloadWorldMessagesTableView, object: nil)
}
}
}
}
WorldMessages视图控制器:
@objc func notification_updateWorldMessages(notification: NSNotification){
self.refreshControl.endRefreshing()
if let newWorldMessages = notification.userInfo?["newWorldMessages"] as? [WorldMessage], let newDeleteArray = notification.userInfo?["newDeleteArray"] as? [Int], let newAppendArray = notification.userInfo?["newAppendArray"] as? [Int], let newWorldMessagesCount = notification.userInfo? ["newWorldMessagesCount"] as? Int, let worldMessagesCount = notification.userInfo? ["worldMessagesCount"] as? Int {
if (newWorldMessagesCount == 0 && noWorldMessages.count == 0){
noWorldMessages = [1]
tableView.reloadSections(IndexSet(integersIn: 1...1), with: .automatic)
} else if (newWorldMessagesCount != 0 && noWorldMessages.count != 0) {
noWorldMessages = []
tableView.reloadSections(IndexSet(integersIn: 1...1), with: .automatic)
}
var count = 0
count = newWorldMessagesCount
if count != 0 {
var tableViewDeletes : [IndexPath] = []
for i in stride(from: count - 1, to: -1, by: -1) {
// WHAT SHOULD BE DELETED
WorldMessagesStore.shared.worldMessages.remove(at: i)
}
var tableViewInserts : [IndexPath] = []
for i in stride(from: count - 1, to: -1, by: -1) {
// WHAT SHOULD BE ADDED
WorldMessagesStore.shared.worldMessages.insert(newWorldMessages[i], at: 0)
}
tableView.beginUpdates()
tableView.deleteRows(at: tableViewDeletes, with: .fade)
tableView.insertRows(at: tableViewInserts, with: .fade)
tableView.endUpdates()
}
}
}
图片:
(在我的应用程序中,“WorldMessages VC”称为主VC)
所以在2次重新登录之后,它被卡住了2次:(你看到2个实例)
错误视频:(演示如果我删除这4行,一切正常)
https://www.youtube.com/watch?v=7xfTzbZplBA
最佳答案
经过数小时的反复阅读,我意识到了一些事情。
我的自定义单元格上有GestureRecognizer,因此必须向它们添加一个委托:而且(目前看来)这导致了问题
import UIKit
import SwipeCellKit
class WorldMessageCell: SwipeTableViewCell {
var worldMessageData : WorldMessage!
@IBOutlet var bubbleView: UIView!
@IBOutlet var bubbleButton: UIButton!
override var canBecomeFirstResponder: Bool {
return true
}
var delegate2: WorldMessageDelegate?
override func awakeFromNib() {
super.awakeFromNib()
// Handling press, longpress
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(bubbleTapped))
self.bubbleButton.addGestureRecognizer(tapGestureRecognizer)
let longPressGestureRecognizer = UILongPressGestureRecognizer(target: self, action: #selector(bubbleLongPressed))
longPressGestureRecognizer.minimumPressDuration = 0.5
self.addGestureRecognizer(longPressGestureRecognizer)
self.bubbleButton.isUserInteractionEnabled = true
self.isUserInteractionEnabled = true
}
@objc func bubbleTapped(sender: UITapGestureRecognizer) {
delegate2?.bubbleTappedHandler(sender: sender)
}
@objc func bubbleLongPressed(sender: UILongPressGestureRecognizer) {
delegate2?.bubbleLongPressHandler(sender: sender)
}
}
protocol WorldMessageDelegate {
func bubbleTappedHandler(sender: UITapGestureRecognizer)
func bubbleLongPressHandler(sender: UILongPressGestureRecognizer)
}
代表们很坚强。回复后
var delegate2: WorldMessageDelegate?
具有
weak var delegate2: WorldMessageDelegate?
改变协议
protocol WorldMessageDelegate {
到
protocol WorldMessageDelegate: class {
一切正常。
我能说对吗?