如何使用类返回Firebase数据? [英] How to return firebase data using a class?

查看:98
本文介绍了如何使用类返回Firebase数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UICollectionView,我使用一个类来填充数据。到目前为止,我正在填充虚拟数据,如下面的代码所示。我试图用我的Firebase数据库中的数据替换这个虚拟数据。

 导入UIKit 

类MrNobody
{
// MARK: - 公共API
var title =
var description =
var numberOfMembers = 0
var numberOfPosts = 0
var featuredImage:UIImage!

(标题:字符串,描述:字符串,精确的图像:UIImage!)
{
self.title = title
self.description = description
self.featuredImage = featuredImage
numberOfMembers = 1
numberOfPosts = 1
}

// MARK: - Private
//虚拟数据
static func createMrNobody() - > (MrNobody)
{
return [
MrNobody(title:some text,description:some text,featuredImage:UIImage(named:r1)!),
MrNobody(title:some text,description:some text,featuredImage:UIImage(named:r1)!),
MrNobody(title:some text,description:some text, featuredImage:UIImage(命名为r1)!),

]
}
}

为了包含数据,我包含了导入Firebase ,使用 let ref = Firebase(url:my app url)和使用

  ref.observeEventType(。值,withBlock:{快照在
print(snapshot.value)
},withCancelBlock:{
print(error.description)
})

我可以读取子数据,但是我无法在返回功能块。理想情况下,我想要使用 enumerator = snapshot.childrenCount ,反复使用返回函数为尽可能多的孩子那里的节点感兴趣。

任何帮助/方向将不胜感激。

解决方案

createMrNobody()函数调用是同步的,在从Firebase检索数据时不会有同样的行为。 p>

Firebase本质上是异步的,因此当您要求数据时,必须等待数据在处理之前被检索。否则,处理代码将在Firebase准备好数据之前执行。



与整个过程同步的函数调用的SQL查询不同。



不知道这是否有帮助,但是这里有一个示例:假设我们有一个存储人员的数组,并且需要从Firebase进行填充。



我们首先定义了person类。

pre $ class Person {
var title:NSString?
var aDescription:NSString?





和数组存储人员

  var myArray = [Person]()



<当你准备好从Firebase加载它们时,就可以这样做了。

  func loadMyPeople(){

myUsersRef.observeSingleEventOfType(.Value,withBlock:{snapshot in

为snapshot.children中的子元素{
let title = child.value [title] as!String
let description = child.value [description] as!String
let dude = Person()
dude.title = title
dude.aDescription = description
self .myArray.append(dude)
}

//将所有的人加载到数组中后,打印它的内容
//你也可以在这里重新载入一个tableView 。
for self in.myArray {
print(email:\(dude.title!)desc:\(dude.descr ))
}
})
}


I have a UICollectionView where I populate data using a class. As of now, I am populating it with dummy data, as shown in the code below. I have trying to replace this dummy data with data from my Firebase database.

import UIKit

class MrNobody
{
// MARK: - Public API
var title = ""
var description = ""
var numberOfMembers = 0
var numberOfPosts = 0
var featuredImage: UIImage!

init(title: String, description: String, featuredImage: UIImage!)
{
    self.title = title
    self.description = description
    self.featuredImage = featuredImage
    numberOfMembers = 1
    numberOfPosts = 1
}

// MARK: - Private
// dummy data
static func createMrNobody() -> [MrNobody]
{
    return [
        MrNobody(title: "some text" , description: "some text", featuredImage: UIImage(named: "r1")!),
        MrNobody(title: "some text" , description: "some text", featuredImage: UIImage(named: "r1")!),
        MrNobody(title: "some text" , description: "some text", featuredImage: UIImage(named: "r1")!),

    ]
}
}

In order to include data, I have included import Firebase, identifying my firebase database using let ref = Firebase(url: "my app url") and reading data using

 ref.observeEventType(.Value, withBlock: { snapshot in
print(snapshot.value)
}, withCancelBlock: { error in
print(error.description)
})

I am able to read the child data, but I am not being able to use it in the return block of the function. Ideally, I would want to use enumerator = snapshot.childrenCount, and reiteratively use the return function for as many children are there in the node of interest.

Any help/direction will be greatly appreciated.

解决方案

The createMrNobody() function call is synchronous and will not behave the same way when you are retrieving data from Firebase.

Firebase is asynchronous in nature so when you ask it for data, you have to wait for that data to be retrieved before processing it. Otherwise the processing code will execute before Firebase has the data ready.

It's different than a SQL query of function call where the entire process is synchronous.

Not sure if this will help, but here's a sample: Suppose we have an array that stores people and we need to populate it from Firebase.

We first define the person class

class Person {
        var title: NSString?
        var aDescription: NSString?
    }

and the array to store the people

var myArray = [Person]()

and when you are ready to load them from Firebase, this will do it

func loadMyPeople() {

    myUsersRef.observeSingleEventOfType(.Value, withBlock: { snapshot in

        for child in snapshot.children {
            let title = child.value["title"] as! String
            let description = child.value["description"] as! String
            let dude = Person()
            dude.title = title
            dude.aDescription = description
            self.myArray.append(dude)
        }

        //after all your people are loaded in the array, print it's contents
        // you could also reload a tableView here as well.
        for dude in self.myArray {
            print("email: \(dude.title!)  desc: \(dude.description!)")
        }
    })
}

这篇关于如何使用类返回Firebase数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆