2016-05-13 58 views
0

我是新來的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

回答

1

下面是上述基礎上週杰倫的有益的指導問題的更新正確的代碼:

//模型下載位置數據的事件。

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

    } 
}