ios - Deinit 永远不会被调用,内存问题

标签 ios swift scenekit

所以我的 deinit 函数从未被调用,我已经在寻找答案,但没有一个对我有用。由于内存问题, View Controller 不断崩溃。感谢您的帮助!

这是我的代码:

@IBOutlet weak var scnView: SCNView!
@IBOutlet weak var artInfoView: UIView!
@IBOutlet weak var mainTitleLbl: UILabel!
@IBOutlet weak var textView: UITextView!
@IBOutlet weak var timeLbl: UILabel!
@IBOutlet weak var stackView: UIStackView!
@IBOutlet weak var artistImg: RoundImage!
@IBOutlet weak var artistNameLbl: UILabel!
@IBOutlet weak var artistView: UIView!

var artRoomScene = ArtRoomScene(create: true)
var artImage = UIImage()
var artInfo: [Any] = []
var posts = [Art]()
var post: Art!
var user: Users!
var showInfo: Bool = false
var showSimilar: Bool = false
let alert = Alerts()

override func viewDidLoad() {
    super.viewDidLoad()

    scnView = self.scnView!
    let scene = artRoomScene
    scnView.scene = scene
    scnView.autoenablesDefaultLighting = true
    scnView.isJitteringEnabled = true
    scnView.backgroundColor = UIColor.white


    if let info = self.artInfo[1] as? Art {
        let image = self.artInfo[0] as? UIImage
        let height = (image?.size.height)! / 900
        let width = (image?.size.width)! / 900
        self.artRoomScene.setup(artInfo: image, height: height, width: width)
        self.mainTitleLbl.text = info.title

        let date = info.postDate/1000
        let foo: TimeInterval = TimeInterval(date)
        let theDate = NSDate(timeIntervalSince1970: foo)
        let time = timeAgoSinceDate(date: theDate as Date, numericDates: true)
        self.timeLbl.text = "\(time)"
        self.textView.text = "\(info.artHeight)'H x \(info.artWidth)'W - \(info.price)$ / month - \(info.type) \n \(info.description)."

        DataService.instance.REF_USERS.child("\(info.userUid)").observe(.value, with: { (snapshot) in
            if let postDict = snapshot.value as? Dictionary<String, AnyObject> {
                let key = snapshot.key
                self.user = Users(key: key, artistData: postDict)

                if let user = self.user {
                    self.artistNameLbl.text = user.name
                    self.artistImg.sd_setImage(with: URL(string: "\(user.profilePicUrl!)") , placeholderImage: UIImage(named:"Placeholder") , options: .continueInBackground)
                }
            }
        })
    }
 }


deinit {
    print("viewcontroller is being deallocated")
}

最佳答案

您可能需要在提供给 DataService 的闭包中使用对 self 的弱引用或无主引用。或者,您可能想要查看该代码并确保它在您期望的时候释放它对该闭包的引用。给定 observe 动词,我希望它无限期地持有对这个闭包的引用。所以我推荐这个:

  1. 在闭包中使用对 self 的弱引用
  2. 在您的 dealloc 中,或者像 viewWillDisappear 这样的其他时间,告诉 DataService 您希望取消订阅/停止观察。这很重要,这样您就不会遇到相反的问题:闭包中的悬垂指针。

对于 #1,关键部分就在你的闭包中,Swift 有一个指定的方法来声明 self 应该是一个弱指针:

DataService.instance.REF_USERS.child("\(info.userUid)").observe(.value, with: { (snapshot) in

成为

DataService.instance.REF_USERS.child("\(info.userUid)").observe(.value, with: { [weak self] (snapshot) in

请注意,您也可以使用 [unowned self],但您会断言您知道 self 在该 block 执行时永远不会为非零。我不认为当你传递给第 3 方时你会知道这一点。所以使用 [weak self] 然后你必须将 self 视为一个可选的,这对这种情况非常有用!

关于ios - Deinit 永远不会被调用,内存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42310840/

相关文章:

ios - 无法在我的 Mac OS X 终端中导航目录。我得到 “No such file or directory”

ios - 使用推送通知发送数据 - 也不返回整个推送

ios - Xcode 6 Beta 5 中的 UIView.animateWithDuration 更改

ios - iOS 上的 SCNRenderer 分配时出现错误

ios - 有人可以清楚地解释 FIRDataEventType 的 .Value、.ChildAdded、.ChildChanged、.ChildRemoved 之间的区别吗?

ios - 对于 UICollectionViewController 来说,正确的 MVVM 架构是什么

ios - 从 URL 将图像保存到库,重命名并使用 Swift 共享

swift - 如何使 ScrollView 内的 TableView 的滚动行为自然

scenekit - SCNNode.flattenedClone() 不会减少绘制调用

swift - 将 SCNNodes 排列成圆形