【问题标题】:Create an Array of Objects with Firebase Async Dictionary Download (Swift)使用 Firebase 异步字典下载 (Swift) 创建对象数组
【发布时间】:2016-09-10 03:17:48
【问题描述】:

我是 Swift 新手。我在下载 Firebase 字典并将它们转换为对象数组时遇到了问题。

下面的语法我做错了什么?在过去的两天里,我一直试图弄清楚这一点,但没有成功。以下给了我一个超出范围错误的索引。这是因为 Firebase 字典尚未完成下载,还是我的 for in loop 语法有缺陷?也许两者兼而有之?谢谢。

 // Array of Location Objects
 var locationsArray:[Location] = [Location]()

 var ref = Firebase(url: "<MYFIREBASEURL>")
 var dictionaryOfRecommendations:[NSDictionary] = [NSDictionary]()
 var currentlyConstructingLocation:Location = Location()

 func getLocationData() {

    let titleRef = self.ref.childByAppendingPath("events")
    titleRef.observeSingleEventOfType(.Value, withBlock: { snapshot in

        var tempDict = [NSDictionary]()

        for item in snapshot.children {

            let child = item as! FDataSnapshot
            let dict = child.value as! NSDictionary
            tempDict.append(dict)
        }

        self.dictionaryOfRecommendations = tempDict

    })

    // Parse data from Firebase

    // Loop through each dictionary and assign values to location object
    var index:Int
    for index in 0...dictionaryOfRecommendations.count {

        // Current Json dictionary
        let jsonDictionary:NSDictionary = self.dictionaryOfRecommendations[index]

        self.currentlyConstructingLocation.title = jsonDictionary["title"] as! String!
        self.currentlyConstructingLocation.locationsLatitudeArray = jsonDictionary["latitude"] as! Double
        self.currentlyConstructingLocation.locationsLongitudeArray = jsonDictionary["longitude"] as! Double

        // Append to Locations Array and start new Location
        self.locationsArray.append(currentlyConstructingLocation)
        self.currentlyConstructingLocation = Location()

    }

    // Notify the MainViewController that the Locations are ready.
    ...
}

【问题讨论】:

  • Firebase 是异步的 - 在您真正拥有该数据之前,您无法对 Firebase 数据进行操作。使用您的代码,通过每个字典代码的循环将在 firebase 返回其数据之前执行 WAY;您应用程序中的本地代码比互联网快得多!解决方案是处理 Firebase withBlock 部分中的数据。您可以将 for 索引保持在循环中,只需将其包含在一个函数中,然后从 Firebase 块中的 self.dictionary 行之后调用该函数。试一试,如果不起作用,请使用更新的代码更新您的帖子,我们会制定答案。
  • 是的!谢谢!这就是我需要的正确方向。我创建了一个函数来拆分数据的解析并在 Firebase 块中调用该函数。我还发现了导致超出范围错误的 for/in 循环中的错误。在下面发布正确的代码以供其他人受益。

标签: ios arrays swift dictionary firebase


【解决方案1】:

以下是根据 Jay 的有用指导更新的上述问题的正确代码:

// 模型下载事件的位置数据。

//Firebase reference
var ref = Firebase(url: "<MYFIREBASEURL")

var locationsArray:[Location] = [Location]()
var dictionaryOfRecommendations:[NSDictionary] = [NSDictionary]()
var currentlyConstructingLocation:Location = Location()

 func getLocationData() {

    let titleRef = self.ref.childByAppendingPath("events")
    titleRef.observeSingleEventOfType(.Value, withBlock: { snapshot in

        var tempDict = [NSDictionary]()

        for item in snapshot.children {

            let child = item as! FDataSnapshot
            let dict = child.value as! NSDictionary
            tempDict.append(dict)

        }

        self.dictionaryOfRecommendations = tempDict
        self.ParseFirebaseData()

    })
}

func ParseFirebaseData() {
    // Parse data from Firebase

    // Loop through each dictionary and assign values to location object
    var index:Int
    for index in 0...dictionaryOfRecommendations.count - 1 {

        // Current Json dictionary
        let jsonDictionary:NSDictionary = self.dictionaryOfRecommendations[index]

        self.currentlyConstructingLocation.title = jsonDictionary["title"] as! String!
        self.currentlyConstructingLocation.locationsLatitudeArray = jsonDictionary["latitude"] as! Double
        self.currentlyConstructingLocation.locationsLongitudeArray = jsonDictionary["longitude"] as! Double

        // Append to Locations Array and start new Location
        self.locationsArray.append(currentlyConstructingLocation)
        self.currentlyConstructingLocation = Location()

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多