Firebase正在从数据库中检索已删除的数据。

3
我正在使用Firebase实时数据库,对于我的应用程序的某些部分它工作正常。我正在观看一个关于在collectionView中填充用户数据的YouTube教程。它使用NSDictionary获取照片URL和用户名,并将它们放入所有用户的collectionView中。我直接在Firebase控制台中删除了一些用户,现在只剩下一个用户。由于某种原因,它仍然拉取我已删除的用户。这是collectionView文件。
import UIKit
import FirebaseDatabase

private let reuseIdentifier = "UserSearchCell"

class UsersCollectionViewController: UICollectionViewController {

var usersDict = NSDictionary?()
var userNamesArray = [String]()
var userImagesArray = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    DataService.ds.REF_USERS.observeEventType(.Value, withBlock :{
        (snapshot) in
        self.usersDict = snapshot.value as? NSDictionary
        for(userId,details) in self.usersDict!{
            let img = details.objectForKey("profileThumbUrl") as! String
            let name = details.objectForKey("username") as! String
            self.userImagesArray.append(img)
            self.userNamesArray.append(name)
            self.collectionView?.reloadData()
        }
    })

    self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
}

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.userImagesArray.count
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! UserSearchCell

    let imageUrl = NSURL(string:userImagesArray[indexPath.row])
    let imageData = NSData(contentsOfURL: imageUrl!)
    cell.userImage.image = UIImage(data:imageData!)
    cell.userName.text = userNamesArray[indexPath.row]

    return cell
}

}

这个应该总是与数据库同步吗?这些被删除的用户从哪里来的?另外,我没有包含单元格代码,因为它只是声明imageView和Label的操作。我以前遇到过这个问题,但我的记忆不好,我不记得为什么会这样做或者我是否解决了它。
2个回答

8

好的,我找到了自己问题的答案。显然模拟器与Firebase存在问题,似乎会形成某种Firebase数据缓存。我尝试在我一直在测试的手机上运行它,没有这个缓存,一切正常。而且我也尝试在模拟器中使用另一台设备运行,那里也可以正常工作。因此,我想把这个留下来,因为我认为很多人可能会在IOS模拟器中遇到Firebase的问题,因为它的核心功能无法很好地与之配合。


1
模拟器!它们引起了世界问题的一半 :) - Fattie
下午好!我现在面临一个在真实设备上的问题。但是我已经将它包含在Firebase缓存中。 - Alexander Khitev
1
如果禁用此功能FIRDatabase.database().PersistenceEnabled,那么一切都能正常工作。也许这就是我其他问题的答案,我会检查一下。 - Alexander Khitev
我觉得你想要持久性,如果你希望你的应用程序在离线时将数据存储起来,然后在联网时执行。每当我在实际设备上遇到这种问题时,都是因为我做错了什么,但我能够修复它。 - Wayne Filkins

0

如果您目前正在使用模拟器,您可以处理持久性。


#if targetEnvironment(simulator)
        // Disable persistence if on a simulator
        Database.database().isPersistenceEnabled = false
#else
        // Enable persistence if on a real device
        Database.database().isPersistenceEnabled = true
#endif


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接