![simulator simulator](https://c1.lmlphp.com/image/static/default_avatar.gif)
本文介绍了firebase正在从数据库中检索已删除的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是Firebase实时数据库,对于我的应用程序的某些部分,它工作正常。我正在阅读YouTube上的一个教程,该教程向用户填充collectionView。它使用NSDictionary来获取照片URL和用户名,并把它们放在所有用户的集合视图中。我直接在Firebase控制台中删除了一些用户,现在只有一个用户。由于某种原因,它仍然拉我删除的用户。这是collectionView文件。
import UIKit
import FirebaseDatabase
$ b $ private let reuseIdentifier =UserSearchCell
class UsersCollectionViewController:UICollectionViewController {
var userDict = NSDictionary?()
var userNamesArray = [String]()
var userImagesArray = [String]()
重写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
让name = detail.objectForKey(username)as!String
self.userImagesArray.append(img)
self.userNamesArray.append(name)
self.collectionView?.reloadData()
}
})
self.collectionView!.registerClass(UICollectionVie wCell.self,forCellWithReuseIdentifier:reuseIdentifier)
}
覆盖func numberOfSectionsInCollectionView(collectionView:UICollectionView) - > Int {
return 1
}
覆盖func collectionView(collectionView:UICollectionView,numberOfItemsInSection section:Int) - > Int {
return self.userImagesArray.count
}
覆盖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操作。我之前遇到过这个问题,但是我的记忆不好,我不记得为什么这样做,或者如果我解决了它。
解决方案好吧,我想出了我自己的问题的答案。显然模拟器有一个firebase的问题,似乎形成某种缓存的firebase数据。我试着在我测试的手机上运行它,它没有这个缓存,一切正常。此外,我试着在模拟器上的另一个设备上运行,它也在那里工作得很好。所以我想留下来,因为我认为很多人可能会遇到与Firebase有关的问题,因为它的核心功能与IOS模拟器不兼容。
I'm using firebase realtime database and it's working fine for some parts of my app. I was going through a tutorial on youtube which populates a collectionView with users. It uses NSDictionary to get the photo URL and username and puts them in the collection view for all users. I deleted some of the users directly in the firebase console, and now have only one user. For some reason it's still pulling the users that I deleted. This is the collectionView file.
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
}
}
Shouldn't this always sync with the database? Where are these deleted users coming from? Also I didn't include the cell code because all it is is the imageView and Label actions being declared. I ran into this problem before but my memory is bad and I don't remember why it was doing it or if I ever solved it.
解决方案
Okay I figured out the answer to my own question. Apparently the simulator has a problem with firebase and seems to form some sort of cache of firebase data. I tried running it on a phone I had been testing on and it didn't have this cache and everything worked fine. Also I tried running on a different device IN the simulator and it worked fine there too. So I want to leave this up because I think a lot of people may have trouble with Firebase since it's core feature doesn't work well with the IOS simulator.
这篇关于firebase正在从数据库中检索已删除的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!