Guest User

Untitled

a guest
Jan 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. //will show the vents that a user is attending
  2. static func Events(for user: User, completion: @escaping ([Event]) -> Void)
  3. {
  4. var currentEvents = [Event]()
  5.  
  6. //Getting firebase root directory
  7. let ref = Database.database().reference().child("users").child(user.uid).child("Attending")
  8.  
  9.  
  10. ref.observe(.value, with: { (snapshot) in
  11. // print(snapshot)
  12.  
  13. // guard snapshot.children.allObjects is [DataSnapshot] else {
  14. // return completion([])
  15. // }
  16.  
  17. guard let eventDictionary = snapshot.value as? [String: Any] else {
  18. return completion([])
  19. }
  20.  
  21. // print(snapshot)
  22. let dispatchGroup = DispatchGroup()
  23.  
  24. eventDictionary.forEach({ (key,value) in
  25. // print(key)
  26. // print(value)
  27. EventService.show(forEventKey: key , completion: { (event) in
  28. dispatchGroup.enter()
  29. AttendService.isEventAttended(event, byCurrentUserWithCompletion: { (isAttended) in
  30. event?.isAttending = isAttended
  31. dispatchGroup.leave()
  32.  
  33. })
  34. currentEvents.append(.init(currentEventKey: key , dictionary: (event?.eventDictionary)!))
  35. })
  36. })
  37.  
  38. dispatchGroup.notify(queue: .main, execute: {
  39. completion(currentEvents)
  40. })
  41. })
  42. }
Add Comment
Please, Sign In to add comment