ViewController实例是否被删除?

时间:2018-05-21 01:05:41

标签: ios swift xcode instance viewcontroller

我正面临一个非常令人沮丧的问题..

所以我的申请工作得很好,直到现在......

该应用有一个 TabController:带有5个viewControllers

在这个TabController上,我有一个每10秒获取一次数据的计时器

此数据与通知一起发送到其名为"WorldMessages"的子VC之一,然后更新它的tableView

我不知道如何或为什么,但每当我在我的应用程序中注销/登录时,WorldMessages ViewController实例都会卡住..

所以例如在5次重新登录之后,我在内存中有5个WorldMessages VC。

  

// Relogin意味着TabController及其childViews正在获取   销毁,然后登录后创建一个新的实例   TabController //

我有一个线索,那是因为线程,但我不确定。当我删除几行时,它正常工作。任何人都可以帮助我吗?

(如果我删除 WorldMessages viewController中的那些行

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 viewController:

@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个实例)

enter image description here

该错误的视频:(证明如果删除这4行,一切正常)

https://www.youtube.com/watch?v=7xfTzbZplBA

1 个答案:

答案 0 :(得分:1)

经过几个小时的尝试并重新阅读我收到的回复,我意识到了一些事情。

我的自定义单元格上有gestureRecognizers,因此我必须向它们添加一个委托:并且(现在,似乎)这导致了问题

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 {

一切正常。

我可能是对的吗?