2016-07-30 97 views
1

我使用的是Firebase实时数据库,它对我应用程序的某些部分工作正常。我正在阅读YouTube上的一个教程,该教程向用户填充collectionView。它使用NSDictionary获取照片URL和用户名,并将它们放入所有用户的集合视图中。我直接在Firebase控制台中删除了部分用户,现在只有一个用户。出于某种原因,它仍在拉我删除的用户。这是collectionView文件。firebase正在检索从数据库中删除的数据

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操作。我之前遇到过这个问题,但是我的记忆很糟糕,我不记得为什么这样做,或者我曾经解决过这个问题。

回答

7

好吧,我想出了我自己的问题的答案。显然,模拟器有一个firebase的问题,似乎形成某种类型的firebase数据缓存。我试着在我测试过的手机上运行它,它没有这个缓存,一切正常。此外,我尝试在模拟器中的其他设备上运行,并且它在那里也工作得很好。所以我想留下来,因为我认为很多人可能在使用Firebase时遇到问题,因为它的核心功能不适合IOS模拟器。

+0

模拟器!他们造成世界上一半的问题:) – Fattie

+0

下午好!我现在正面临着一个真实设备上的这个问题。但我已经包含在缓存firebase – Alexander

+1

然后,如果您禁用此功能FIRDatabase.database(),一切正常工作。 PersistenceEnabled。也许这是我的其他问题的答案,我会检查。 – Alexander