重新加载collectionView:Firebase时内存泄漏

时间:2016-10-18 22:04:28

标签: ios swift firebase uicollectionview firebase-realtime-database

我正在尝试用posts填充collectionView。我必须得到帖子,然后为发布它们的用户获取一些数据。由于某种原因,它无法正常工作。

DataService.ds.REF_POSTS.child("\(self.loggedInUser!.uid)").queryLimitedToLast(30).observeSingleEventOfType(.Value, withBlock: { postDictionary in
            if postDictionary.exists() {
                if let snapshots = postDictionary.children.allObjects as? [FIRDataSnapshot] {
                    self.posts = [Post]()
                    for snap in snapshots {
                        if let postDict = snap.value as? NSDictionary {
                            for(name, value) in postDict {
                                let interval = postDict.objectForKey("timePosted") as! Double
                                let formattedDate = NSDate(timeIntervalSince1970: interval)
                                let timeAgo = self.getDate(formattedDate)
                                if name as! String == "postedBy" {
                                    DataService.ds.REF_USERS.child(value as! String).observeSingleEventOfType(.Value, withBlock: { (userDictionary) in
                                        let userDict = userDictionary.value as! NSDictionary
                                        let username = userDict.objectForKey("username")!
                                        let profileThumbUrl = userDict.objectForKey("profileThumbUrl")!
                                        let key = snap.key
                                        let post = Post(postKey: key, dictionary: postDict, username: username as! String, profileThumbUrl: profileThumbUrl as! String, timeAgo: timeAgo)
                                        self.posts.append(post)
                                    })
                                }
                            }
                        }
                    }
                }
            }
            self.collectionView?.reloadData()
        })

如果我在追加帖子后立即执行reload(),它会有效,但是存在某种内存泄漏。如果我使用虚拟值,Post类中没有问题或填充集合视图。问题出在我发布的代码中。我想我有一个额外的循环或任何人可以帮助吗?

1 个答案:

答案 0 :(得分:2)

如果你担心reload()是内存泄漏的原因,你可以使用这个hack: -

 if name as! String == "postedBy" {
                                DataService.ds.REF_USERS.child(value as! String).observeSingleEventOfType(.Value, withBlock: { (userDictionary) in
                                    let userDict = userDictionary.value as! NSDictionary
                                    let username = userDict.objectForKey("username")!
                                    let profileThumbUrl = userDict.objectForKey("profileThumbUrl")!
                                    let key = snap.key
                                    let post = Post(postKey: key, dictionary: postDict, username: username as! String, profileThumbUrl: profileThumbUrl as! String, timeAgo: timeAgo)
                                    self.posts.append(post)

                                if posts.count == postDictionary.childrenCount{

                                 self.collectionView?.reloadData()

                                    } 
                                })
                            }

然后还会看到这个答案: - Firebase observeSingleEventOfType stays in memory