从Firebase观察者获取数据到另一个功能

时间:2019-04-16 07:40:10

标签: ios swift

获取Firebase的数据后,我遇到了问题。我在模型中编写了函数getData(),使用委托在UITableViewController上调用它们并将数据设置为TableView。 但是,当我创建新数组以从func getData()获取数据时,该数组为nil。

这是我的模特

import Foundation
import Firebase

protocol myDelegate: class {
    func didFetchData(datas: [Book])
}
class Book {
    var Id: String?
    var Author: String?
    var ChapterCount: Int?
    var CoverPhoto: String?
    var Genre: String?
    var Image: String?
    var Intro: String?
    var Like: Int?
    var Name: String?
    var Status: String?
    var UpdateDay: String?
    var UploadDay: String?
    var View: Int?

    var ref: DatabaseReference!
    weak var delegate: myDelegate?

    init()
    {

    }

    init(Id: String,Author: String,Image: String,Name: String,Status: String,UpdateDay: String,View: Int)
    {
        self.Id = Id
        self.Author = Author
        self.Image = Image
        self.Name = Name
        self.Status = Status
        self.UpdateDay = UpdateDay
        self.View = View
    }

    func getListBook() {
        ref = Database.database().reference()
        ref.child("Book").observe(.value, with: { snapshot in
            var newNames: [Book] = []
            let value = snapshot.value as? NSDictionary

            for nBook in value! {
                let val = nBook.value as? NSDictionary
                self.Name = val?["Name"] as? String ?? ""

                self.Author = val?["Author"] as? String ?? ""

                self.View = val?["View"] as? Int ?? 0

                self.Status = val?["Status"] as? String ?? ""

                self.Id = val?["Id"] as? String ?? ""

                self.Image = val?["Image"] as? String ?? ""

                self.UpdateDay = val?["UpdateDay"] as? String ?? ""

                newNames.append(Book(Id: self.Id!, Author: self.Author!, Image: self.Image!, Name: self.Name!, Status: self.Status!, UpdateDay: self.UpdateDay!, View: self.View!))
            }

            self.delegate?.didFetchData(datas: newNames)
        })
    }
}

还有类UITableViewController

import Firebase

class ListStoryTableView: UITableViewController, myDelegate {
    var ref: DatabaseReference!

    var book = Book()

    var listBook: [Book] = []

    func didFetchData(datas: [Book]) {
        listBook = datas
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let nib = UINib.init(nibName: "ListStoryTableViewCell", bundle: nil)
        self.tableView.register(nib, forCellReuseIdentifier: "ListStoryTableViewCell")

        book.delegate = self
        book.getListBook()

        print("\(listBook)") //this is return 0
    }```

2 个答案:

答案 0 :(得分:1)

一种解决方案是更改protocol实现并在getListBook函数中使用完成块。从您的myDelegate中删除ListStoryTableView引用,然后进行以下更改:

func getListBook(completion: @escaping (_ books: [Book]) -> Void) {
      ref = Database.database().reference()
      ref.child("Book").observe(.value, with: { snapshot in
      var newNames: [Book] = []
      let value = snapshot.value as? NSDictionary

      for nBook in value! {
        let val = nBook.value as? NSDictionary
        self.Name = val?["Name"] as? String ?? ""

        self.Author = val?["Author"] as? String ?? ""

        self.View = val?["View"] as? Int ?? 0

        self.Status = val?["Status"] as? String ?? ""

        self.Id = val?["Id"] as? String ?? ""

        self.Image = val?["Image"] as? String ?? ""

        self.UpdateDay = val?["UpdateDay"] as? String ?? ""

        newNames.append(Book(Id: self.Id!, Author: self.Author!, Image: self.Image!, Name: self.Name!, Status: self.Status!, UpdateDay: self.UpdateDay!, View: self.View!))
      }

      completion(newNames)
    })
  }

,然后在您的viewDidLoad或任何其他函数中,使用以下命令获取数据:

book.getListBook { books in
  listBook = books
  tableView.reloadData()
}

希望对您有所帮助。

答案 1 :(得分:0)

func didFetchData(datas: [Book]) {
    listBook = datas
    print("\(listBook)")
}

使用此功能打印listBook,您将获得数据。