尝试检索我的子类别中的数据

时间:2018-11-20 22:30:34

标签: ios swift firebase firebase-realtime-database

我正在尝试从子类别中获取信息,我设法使用此代码检索了类别中的子类别。

但是,在调用子类别中的数据时遇到了一些麻烦。

//让我们下载食谱列表及其详细信息

        self.ref.child("Categories").observe(.childAdded) { (snapshot) in
        self.subcategories.removeAll()

        let enumerator = snapshot.children
        while let rest = enumerator.nextObject() as? DataSnapshot {
            self.subcategories.append(rest.key)
        }

        self.subcollectionView.reloadData()

    }

}

我以前曾发布过此邮件,但帐户有问题。

这是我用来获取子类别信息的代码。

firebase数据示例:
An example of the firebase data

错误:
the error

非常感谢我应该做的一个例子,因为我是快速编码的新手

@IBOutlet weak var card_cv: UICollectionView!

reference for the database
var refCategories: DatabaseReference!

var subcategories = [String]()

//  declare a string to receive the category name
var subCategoryName:String!


override func viewDidLoad() {
    super.viewDidLoad()

    //  declare the reference to database
    refCategories = Database.database().reference()


    //  setting the subcategory name as view controller's table
    self.title = subCategoryName

    //  let's set delegate for the tableview
    self.card_cv.dataSource = self
    self.card_cv.delegate = self

    // function to download  list from database
    self.downloadSubCategoriesFromDatabase()

}
Custom function download
func downloadSubCategoriesFromDatabase(){


    self.subcategories.removeAll()

    // download the snapshot
    self.refCategories.child("Categories").child(subCategoryName).observeSingleEvent(of: .value) { (snapshot) in

        self.subcategories.removeAll()

        let enumerator = snapshot.children
        while let rest = enumerator.nextObject() as? DataSnapshot {
            self.subcategories.append(rest.key)
        }

        self.card_cv.reloadData()

    }

}

0 个答案:

没有答案