从 Firebase Swift 检索信息的好方法 [英] Good way to Retrieve information from Firebase Swift

查看:27
本文介绍了从 Firebase Swift 检索信息的好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我和我的朋友正在 swift 上开发一个集成了(新的)Firebase 的 IOS 项目.我们能够写入 Firebase 数据库,但我们在检索数据时遇到了问题.

my friend and I are working on an IOS project on swift with (the new) Firebase integrated. We were able to write to the Firebase database, but we ran into problems in retrieve the data.

我们有一个 tableView 控制器,我们希望从 Firebase 数据库填充该表.我们的数据库是这样组织的:

We have a tableView controller and we wish to populate the table from the Firebase database. Our database is organized like this:

root 
|  
- Movies
    |__ -Wjdkfdmlksdf (unique ID made by Firebase)
        |
        |_Actors
          |_A1 = ajkdfnadfdf
          |_A2 = podifpadsfipasdip
        |_Title = "Mission Impossible"
        |_DirectedBy = "Christopher McQuarrie"
-Actors
  |_ -ajkdfnadfdf (unique ID made by Firebase)
         |_name : "Tom Cruise"
         |_Birthday: xx/yy/zz

在项目中,我们创建了一个文件/类,其中包含写入和检索 firebase 的方法.如果我们想从 Firebase 检索特定视图控制器的信息,我们只需调用该类中的特定方法.

In the project, we made a file/class, which has methods that writes to and retrieves from firebase. If we want to retrieve information from Firebase for a particular view controller, we just call a particular method from that class.

例如,这是类中的方法之一:

For example, this is one of the methods in the class:

func getListOfAllMovieIDs() -> [String] {
     let ref = FIRDatabase.database().reference();
     var ListOfMovieIDS = [String]();
    ref.child("Movies").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
     let postDict = snapshot.value as! [String : AnyObject]
     for (MID, _) in postDict {
       ListOfMovieIDS.append(MID);
     }

    }) { (error) in
        print(error.localizedDescription)
    }

    return listOfMoviesIDS
}

我们在tableViewControllerviewDidLoad() 中调用我刚刚在上面描述的这个方法.但是,该方法不返回任何内容.我们还调用了另一种方法,例如获取演员的唯一 IDS 列表,但也不返回任何内容.有什么建议吗?

we call this method that I just described above in the viewDidLoad() in the tableViewController. However, the method returns nothing. We also called another methods like to get the list of unique IDS for actors and that returns nothing either. Any suggestion?

推荐答案

Firebase 异步加载数据.因此,与其触发请求并等待响应,不如发送一个请求,然后在某个时刻收到响应通知.

Firebase loads data asynchronously. So instead of firing a request and waiting for a response, you send a request and then at some point will be notified of the response.

如果您在代码中放置一些位置良好的日志记录语句,您可以很容易地看到这一点:

You can easily see this if you put some well placed logging statements in your code:

func getListOfAllMovieIDs() {
    let ref = FIRDatabase.database().reference();
    print("Before listener");
    ref.child("Movies").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
      print("Inside block");
    })
    print("After listener");
}

打印语句的顺序是:

听众前

听后

内部块

因此,当您的函数退出时,数据尚不可用.这不是一个错误,而是一个非常有意的选择.通过允许代码继续运行,应用程序可以对您的用户保持响应.大多数现代互联网都使用这种模式,因此通常最好接受它(尽管一开始很难掌握).

So by the time your function exits, the data isn't available yet. This is not a mistake, but a very intentional choice. By allowing the code to continue, the app stays responsive for your users. Most of the modern internet works with this model, so it's typically best to embrace it (although it's tricky to get the hang of initially).

解决方案通常是重新措辞您的代码.与其说首先我们加载数据,然后我们做 xyz",不如说我们开始加载数据,一旦我们得到它我们就做 xyz".这就是 Firebase 数据加载方法采用块的原因:它包含xyz"一旦数据可用,就会发生这种情况.

The solution is usually to rephrase your code. Instead of saying "first we load the data, then we do xyz", say it as "we start loading the data, once we get it we do xyz". This is why the Firebase data loading methods take a block: this contains the "xyz" that should happen once the data is available.

这样做的一个很好的副作用是,此逻辑也适用于最初加载后可能会更新的数据.当您使用 objectEventOfType() 时,您可以轻松处理开始同步数据,每当我们获取数据时执行 xyz"的情况.并拥有一个处理实时更新的应用.

A nice side-effect of this is that this logic also works great for data that may be updated after you initially loaded it. When you use objectEventOfType(), you can easily handle situations as "start synchronizing the data, whenever we get the data do xyz" and have an app that handles realtime updates.

这篇关于从 Firebase Swift 检索信息的好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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