新的Firebase检索数据并放在tableView(Swift) [英] New Firebase retrieve data and put on the tableView (Swift)
问题描述
所有的图像随机显示并不断移动,但其他部分显示完美或当我使用返回posts.count tableView显示没有帖子。
希望有人能够给我一些建议
导入UIKit
导入Firebase
导入FirebaseStorage
class timelineTableViewController:UITableViewController {
var posts = [Post]()
var databaseRef:FIRDatabaseReference!
var storageRef:FIRStorageReference!
$ b $重写func viewDidLoad(){
super.viewDidLoad()
databaseRef = FIRDatabase.database()。reference()
storageRef = FIRStorage.storage()。引用()
}
覆盖func numberOfSectionsInTableView(tableView:UITableView) - > Int {
// #warning不完整的实现,返回部分的数目
返回1
}
重写func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int {
// #warning不完整的实现,返回行数
返回posts.count
重写func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath: NSIndexPath) - > UITableViewCell {
let cellIdentifier =postCell
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier,forIndexPath:indexPath)as! timelineTableViewCell
let userPostRef = self.databaseRef.child(posts)
userPostRef.observeEventType(.ChildAdded,withBlock:{(snapshot)in
if let postAdd = snapshot。值为as NSDictionary {
let myPost = Post(data:postAdd)
self.posts.insert(myPost,atIndex:0)
cell.usernameLabel.text = self。 posts [indexPath.row] .username
cell.postText.text = self.posts [indexPath.row] .postText
cell.timeLabel.text = self.posts [indexPath.row] .time
let url = snapshot.value?[postPhoto] as!String
let userPhotoUrl = snapshot.value?[userPhoto] as!String
FIRStorage.storage()。 referenceForURL(url).dataWithMaxSize(10 * 1024 * 1024,完成:$(
)中的{ b FIRStorage.storage()。referenceForUR L(userPhotoUrl).dataWithMaxSize(10 * 1024 * 1024,完成:$(
)中的{(数据,错误)let userPhoto = UIImage(data:data!)
cell.userPhoto.image = userPhoto
})
$ b})
}
})
return cell
$ b
这里的最佳做法是在ViewDidLoad方法中填充tableView数据源(posts数组)。然后,你的tableView不会尝试拉下数据,因为它刷新。
另外,由于您添加.childAdded观察者,您的应用程序将被通知任何添加到firebase所以当发生这种情况时,只需将新数据添加到posts数组并刷新tableView即可。
没有理由继续从Firebase提取数据,因为数据是静态的,直到数据发生更改。因此,加载一次,然后等待这些变化 - 你可能要考虑添加一个.childChanged事件(并删除),以防有人更新他们的照片。
来自Firebase的Firechat 应用程序是理解最佳方式的一个很好的资源 - 如果遵循这种设计模式,可以避免所有网络问题,而不必担心单独的异步调用。
您可以简单地依靠Firebase自行处理。
编辑...那么,虽然这是一个Firechat的链接,Obj-C代码似乎已经丢失(感谢Firebase,唉)
所以 - 鉴于此,请参阅我的答案此问题,因为它有你需要的代码模式。
I made a simple social media with new Firebase, and I successfully save string with database and image with storage, but when it comes to retrieve data back to the tableView the unusual things happen!
all the images retrieve back randomly show up and continually shift, but other part shows perfectly or when I using return posts.count tableView shows no post.
Hope someone can kindly give me some suggestion
import UIKit
import Firebase
import FirebaseStorage
class timelineTableViewController: UITableViewController {
var posts = [Post]()
var databaseRef: FIRDatabaseReference!
var storageRef: FIRStorageReference!
override func viewDidLoad() {
super.viewDidLoad()
databaseRef = FIRDatabase.database().reference()
storageRef = FIRStorage.storage().reference()
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return posts.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "postCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath)as! timelineTableViewCell
let userPostRef = self.databaseRef.child("posts")
userPostRef.observeEventType(.ChildAdded, withBlock: {(snapshot) in
if let postAdd = snapshot.value as? NSDictionary{
let myPost = Post(data: postAdd)
self.posts.insert(myPost, atIndex:0)
cell.usernameLabel.text = self.posts[indexPath.row].username
cell.postText.text = self.posts[indexPath.row].postText
cell.timeLabel.text = self.posts[indexPath.row].time
let url = snapshot.value?["postPhoto"] as! String
let userPhotoUrl = snapshot.value?["userPhoto"] as! String
FIRStorage.storage().referenceForURL(url).dataWithMaxSize(10 * 1024 * 1024, completion: { (data, error) in
let postPhoto = UIImage(data: data!)
cell.postPhoto.image = postPhoto
FIRStorage.storage().referenceForURL(userPhotoUrl).dataWithMaxSize(10 * 1024 * 1024, completion: { (data, error) in
let userPhoto = UIImage(data: data!)
cell.userPhoto.image = userPhoto
})
})
}
})
return cell
}
}
The best practice here to to populate your tableView datasource (posts array) in your ViewDidLoad method. Then your tableView isn't trying to pull down data as it's refreshing.
Also, since you are adding a .childAdded observer your app will be notified of any additions to firebase so when that happens, just add the new data to the posts array and refresh the tableView.
There's no reason to continually pull data from Firebase as it's static until it changes. So load once and then wait for those changes - you may want to consider adding a .childChanged event (and removed) in case someone updates their pic for example.
The Firechat app from Firebase is a great resource for understanding the best way to do this - if you follow that design pattern, you can avoid all networking issues and not have to worry about separate async calls.
You can simply rely on Firebase to take care of itself.
Edit... Well, while that is a Firechat link, the Obj-C code seems to be missing (thanks Firebase. ugh)
So - in light of that, see my answer to this question as it has pattern for the code you need.
这篇关于新的Firebase检索数据并放在tableView(Swift)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!