2

新しい Firebase で簡単なソーシャル メディアを作成し、文字列をデータベースに保存し、画像をストレージに保存することに成功しましたが、データを tableView に戻すと異常なことが起こります。

取得したすべての画像がランダムに表示され、継続的にシフトしますが、他の部分は完全に表示されるか、リターン posts.count tableView を使用すると投稿が表示されません。

誰かが親切に私にいくつかの提案をしてくれることを願っています

    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
    }

    }
4

2 に答える 2

6

ここでのベスト プラクティスは、ViewDidLoad メソッドで tableView データソース (posts 配列) を設定することです。次に、tableView は更新中にデータを取り込もうとしません。

また、.childAdded オブザーバーを追加しているため、firebase への追加がアプリに通知されるため、それが発生した場合は、新しいデータを posts 配列に追加し、tableView を更新するだけです。

変更されるまで静的であるため、Firebase から継続的にデータをプルする理由はありません。したがって、一度ロードしてからそれらの変更を待ちます。たとえば、誰かが自分の写真を更新した場合に備えて、.childChanged イベントを追加 (および削除) することを検討してください。

FirebaseのFirechatアプリは、これを行う最善の方法を理解するための優れたリソースです。その設計パターンに従えば、すべてのネットワークの問題を回避でき、個別の非同期呼び出しについて心配する必要がありません。

Firebase に任せれば、それ自体で処理を行うことができます。

編集...それはFirechatリンクですが、Obj-Cコードが欠落しているようです(Firebaseに感謝します。うーん)

そのため、必要なコードのパターンがあるため、この質問に対する私の回答を参照してください。

于 2016-06-18T13:24:25.927 に答える