2016-05-13 2 views
0

私はSwiftを使い慣れました。 Firebase辞書をダウンロードしてオブジェクトの配列に変換するのに問題がありました。Firebase Async Dictionaryダウンロード(Swift)でオブジェクトの配列を作成

以下の構文で何が間違っていますか?私は最後の2日間、これを理解しようとして失敗した。以下は私に範囲外のエラーのインデックスを与えます。これは、Firebase Dictionaryがまだダウンロードを完了していないか、またはループsytaxのために私のために欠陥があるためですか?多分両方?ありがとう。イベント用の位置データをダウンロードする

//モデル:

// 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がデータを返す前に各辞書コードのループが実行されます。あなたのアプリのローカルコードは、インターネットよりもはるかに高速です!解決策は、Firebase withBlockセクション内のデータを処理することです。 forインデックスをループ内に保持し、それを関数に囲み、Firebaseブロック内からその関数をself.dictionary行の後にコールするだけです。試してみてください。うまくいかない場合は、更新されたコードで投稿を更新してください。私たちは答えを作ります。 – 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() 

    } 
} 
関連する問題