使用Firebase异步字典创建对象数组下载(Swift)

问题描述:

我是新来的Swift。我一直无法下载Firebase字典并将它们转换为一组对象。使用Firebase异步字典创建对象数组下载(Swift)

我在做什么错误的语法如下?我花了两天的时间试图弄清楚这一点。以下给我一个索引超出范围错误。这是因为Firebase字典还没有完成下载,或者是我的for循环系列错误?也许都是?谢谢。

// 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. 
    ... 
} 
+0

Firebase是异步的 - 只有在实际拥有该数据之后,您才能对Firebase数据进行操作。使用您的代码,通过每个字典代码的循环将在firebase返回其数据之前执行WAY;您的应用中的本地代码比互联网快得多!解决方案是在Firebase withBlock部分内处理数据。您可以将for索引保留在循环中,只需将其放在函数中,然后在self.dictionary行之后的Firebase块内调用该函数即可。尝试一下,如果它不起作用,更新您的文章与更新的代码,我们将制定一个答案。 – Jay

+0

是的!谢谢!这是我需要的正确方向。我创建了一个函数来拆分数据解析并在Firebase块中调用该函数。我也想出了我的for/in循环中导致超出范围错误的错误。在下面发布正确的代码以供其他人从中受益。 – Ben

下面是上述基础上周杰伦的有益的指导问题的更新正确的代码:

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

//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() 

    } 
}