swift - 使用 Geofire/Firebase 收集用户列表

标签 swift xcode firebase firebase-realtime-database geofire

我有一个名为 User 的类,它有一个使用 GeoFire 获取附近所有食品卡车的函数。我使用observeReadyWithBlock 获取GeoFire 返回的卡车ID,并使用Firebase 获取其余信息。但是,当我在添加名称和描述后从卡车对象数组中访问其中一辆卡车时,xCode 似乎告诉我该数组为空。

我计划在其他 Controller 类中使用这组附近的卡车,以填充向用户显示所有附近卡车和一些基本信息的表格。

如何正确填充我的卡车数组,以及根据下面的代码我可能会犯什么错误。非常感谢!

func getNearbyTrucks(){
    //Query GeoFire for nearby users
    //Set up query parameters
    let center = CLLocation(latitude: 37.331469, longitude: -122.029825)
    let circleQuery = geoFire.queryAtLocation(center, withRadius: 100)

    circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, location: CLLocation!) in

        let newTruck = Truck()
        newTruck.id = key
        newTruck.currentLocation = location
        self.nearbyTrucks.append(newTruck)

    }) //End truckQuery

    //Execute code once GeoFire is done with its' query!
    circleQuery.observeReadyWithBlock({

        for truck in self.nearbyTrucks{

            ref.childByAppendingPath("users/\(truck.id)").observeEventType(.Value, withBlock: { snapshot in
                print(snapshot.value["name"] as! String)

                truck.name = snapshot.value["name"] as! String
                truck.description = snapshot.value["selfDescription"] as! String
                let base64String = snapshot.value["profileImage"] as! String
                let decodedData = NSData(base64EncodedString: base64String as String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
                truck.photo = UIImage(data: decodedData!)!
            })
        }

    }) //End observeReadyWithBlock

    print(nearbyTrucks[0].id)
    //This line gives the error that the array index is out of range
}

最佳答案

来自 Geofire 和 Firebase 数据库其余部分的数据并不是简单地从数据库“获取”的。它是异步加载然后持续同步的。这会改变代码的流程。通过添加一些日志记录最容易看到这一点:

func getNearbyTrucks(){
    //Query GeoFire for nearby users
    //Set up query parameters
    let center = CLLocation(latitude: 37.331469, longitude: -122.029825)
    let circleQuery = geoFire.queryAtLocation(center, withRadius: 100)

    print("Before Geoquery")

    circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, location: CLLocation!) in
        print("In KeyEntered block ")

        let newTruck = Truck()
        newTruck.id = key
        newTruck.currentLocation = location
        self.nearbyTrucks.append(newTruck)

    }) //End truckQuery

    print("After Geoquery")
}

日志记录的输出顺序与您的预期不同:

Before Geoquery

After Geoquery

In KeyEntered block

In KeyEntered block

...

当从服务器检索地理 key 和用户时,代码会继续执行,并在返回任何 key 或用户之前 getNearbyTrucks() 退出。

处理此问题的一种常见方法是将您对代码的思考方式从“首先装载卡车,然后打印第一辆卡车”更改为“每当卡车装载时,打印第一辆卡车”。

在代码中这将转换为:

func getNearbyTrucks(){
    //Query GeoFire for nearby users
    //Set up query parameters
    let center = CLLocation(latitude: 37.331469, longitude: -122.029825)
    let circleQuery = geoFire.queryAtLocation(center, withRadius: 100)

    circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, location: CLLocation!) in

        let newTruck = Truck()
        newTruck.id = key
        newTruck.currentLocation = location
        self.nearbyTrucks.append(newTruck)

        print(nearbyTrucks[0].id)
    }) //End truckQuery

    //Execute code once GeoFire is done with its' query!
    circleQuery.observeReadyWithBlock({

        for truck in self.nearbyTrucks{

            ref.childByAppendingPath("users/\(truck.id)").observeEventType(.Value, withBlock: { snapshot in
                print(snapshot.value["name"] as! String)

                truck.name = snapshot.value["name"] as! String
                truck.description = snapshot.value["selfDescription"] as! String
                let base64String = snapshot.value["profileImage"] as! String
                let decodedData = NSData(base64EncodedString: base64String as String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
                truck.photo = UIImage(data: decodedData!)!
            })
        }

    }) //End observeReadyWithBlock
}

我已将第一辆卡车的打印移至按键输入事件的 block 中。根据您尝试运行的实际代码,您可以将其移动到不同的位置。

Firebase 数据库和 Geofire 本身使用的方法是一种更可重用的方法:将一个 block 传递到 observeEventType withBlock: 中,该 block 包含在 key 可用时要运行的代码。如果您将相同的模式应用于您的方法,它将变成:

func getNearbyTrucks(withBlock: (key: String) -> ()){
    //Query GeoFire for nearby users
    //Set up query parameters
    let center = CLLocation(latitude: 37.331469, longitude: -122.029825)
    let circleQuery = geoFire.queryAtLocation(center, withRadius: 100)

    circleQuery.observeEventType(GFEventTypeKeyEntered, withBlock: { (key: String!, location: CLLocation!) in

        let newTruck = Truck()
        newTruck.id = key
        newTruck.currentLocation = location
        self.nearbyTrucks.append(newTruck)

        withBlock(nearbyTrucks[0].id)
    }) //End truckQuery

    //Execute code once GeoFire is done with its' query!
    circleQuery.observeReadyWithBlock({

        for truck in self.nearbyTrucks{

            ref.childByAppendingPath("users/\(truck.id)").observeEventType(.Value, withBlock: { snapshot in
                print(snapshot.value["name"] as! String)

                truck.name = snapshot.value["name"] as! String
                truck.description = snapshot.value["selfDescription"] as! String
                let base64String = snapshot.value["profileImage"] as! String
                let decodedData = NSData(base64EncodedString: base64String as String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
                truck.photo = UIImage(data: decodedData!)!
            })
        }

    }) //End observeReadyWithBlock
}

在这里,您需要根据您的需要将 withBlock() 回调移动到更合适的位置。

关于swift - 使用 Geofire/Firebase 收集用户列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38581573/

相关文章:

ios - 在我的 IOS 框架中使用 dylib 在应用程序启动后出现错误

ios - 如何使用 IBAction 将数据写入 Firebase

javascript - angularfire,检索数据的回调

ios - Swift - 在保持 api 清晰度的同时传播错误

swift - 切换大小写以检查 ViewController 是否符合协议(protocol)

ios - 在 uitableview 添加索引在 Swift 2.0 中不起作用

javascript - Firebase - 将对象推送到嵌套的对象列表中

iOS Swift SQLite 添加列到表问题

iphone - 如何使用 NSURLRequest/NSURLConnection 将 mp3 文件下载到应用程序?

ios - 启动屏幕不能设置自定义类名