Guest User

Untitled

a guest
May 24th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.61 KB | None | 0 0
  1. func getAllDevices(userID: String, completionHandler:@escaping (([DeviceRoom])->Void)){
  2.    
  3.     var homeID: String = ""
  4.    
  5.     _ = Firestore.firestore().collection("users/4oKS4F0uejdyxEZZ340v/homes" ).limit(to: 1).getDocuments{ (homeSnap,error) in
  6.       if error != nil {
  7.         print(error?.localizedDescription as Any)
  8.         return
  9.       }else {
  10.         for home in (homeSnap?.documents)! {
  11.           homeID = home.documentID
  12.           _ = Firestore.firestore().collection("users/4oKS4F0uejdyxEZZ340v/homes/\(homeID)/rooms").getDocuments(completion: { (roomsSnap, error) in
  13.             if error != nil{
  14.               print(error?.localizedDescription)
  15.               return
  16.             }else{
  17.               for room in (roomsSnap?.documents)! {
  18.                 _ = Firestore.firestore().collection("users/4oKS4F0uejdyxEZZ340v/homes/\(homeID)/rooms/\(room.documentID)/devices").getDocuments(completion: { (devicesSnap, error) in
  19.                   if error != nil{
  20.                     print(error.debugDescription)
  21.                     return
  22.                   }else{
  23.                     for device in (devicesSnap?.documents)! {
  24.                       do{
  25.                         let device = try DeviceRoom(fromDictionary: device.data())
  26.                         self.devices.append(device)
  27.                         print(self.devices)
  28.                       }catch {
  29.                         print("errore")
  30.                       }
  31.                     }
  32.                   }
  33.                 })
  34.               }
  35.             }
  36.             completionHandler(self.devices)
  37.           })
  38.         }
  39.       }
  40.     }
  41.   }
Advertisement
Add Comment
Please, Sign In to add comment