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

标签 ios swift firebase uicollectionview firebase-realtime-database

我正在尝试用帖子填充 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 类或填充 Collection View 没有问题。问题出在我发布的这段代码中。我想我有一个额外的循环或任何人都可以帮忙吗?

最佳答案

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

 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

关于ios - 重新加载 collectionView 时内存泄漏 : Firebase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40118865/

相关文章:

ios - 如何进行延迟深度链接?

ios - 如何在 Swift 3 中获取 UIBarButtonItem 按钮中的自定义图像名称?

ios - Swift 2,无法识别的选择器发送到 UIAlertController 的实例

c# - 统一的 Firebase 身份验证登录总是返回一个新用户

firebase - 如何在Firebase中查询相关记录?

ios - 如何设置状态栏背景颜色 iOS 7

iphone - UITableView(IOS)尝试删除但从未显示删除按钮

ios - 为什么我的 MFMailComposeViewController 实例只关闭一次?

ios - UITableViewCell 被格式化为默认大小

javascript - 我无法使用聚合物在 firestore 上分离监听器