2016-09-17 35 views
0
func EmptySearchBarList(){ 

self.PersonalSearchesList = [] 

currentUserFirebaseReference.child("rooms").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in 


    if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] { 
     for snap in snapshots { 

      print("snap") 
      print(self.PersonalSearchesList.count) // Calling 0 every single time 

      DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in 

       if snapshot.value != nil { 

        if let users = (snapshot.value! as? NSDictionary)?["users"] as? Dictionary<String,AnyObject> { 

         DataService.ds.REF_USERS.child(topUser).child("pictureURL").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in 

          self.PersonalSearchesList.append(eachSearch) 
          print("first one") 
          print(self.PersonalSearchesList.count) 
         }) 
        } 
       } 
      }) 

     } 

     print("Second one") 
     print(self.PersonalSearchesList.count) 

     print("Calling to set my sorted PersonalSearchesList") 
     self.mySortedList = self.PersonalSearchesList.sorted{ $0.users > $1.users } 

    } 
} 
initialLoadOver = true 
} 

The print logs无法理解阵列附加

我想要什么样的代码,最终运行是这样的:

var mySortedList = [Interest](){ 
    didSet{ 
     print("this was called") 
     let myCount = mySortedList.count 
     print(self.PersonalSearchesList.count) 
     print(myCount) 

     self.tableView.reloadData() 
    } 
} 

的尝试是加载了我的PersonalSearchesList阵列,而一旦快照快照已完成运行,我设置MySortedList等于PersonalSearchesList并重新加载tableview。

我不明白的是为什么打印出来就像他们一样。 snap/0来自我的顶部,用于捕捉快照。它看起来应该是快照/ 1,快照/ 2,快照/ 3.

快照完成时要调用的代码在时间轴中正确,一旦快照经过代码运行。没有道理的是,为什么直到这些项目实际上被追加到PersonalSearchesList之后。因为我是如何将所有过滤的数组设置为一个空的个人搜索数组,然后我将其填充。

这里的任何想法?

编辑:当他们从DataService的所谓

var dispatchGroup = DispatchGroup() 
     dispatchGroup.enter() 
     dispatchGroup.leave() 

     dispatchGroup.notify(queue: DispatchQueue.global(), execute: { 

     }) 

回答

1

DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent运行异步所以所有这些回调(在这里你居然填你列出)的运行。

你可以用dispatch group来做你想做的。 http://commandshift.co.uk/blog/2014/03/19/using-dispatch-groups-to-wait-for-multiple-web-services/

func EmptySearchBarList(){ 
var dispatchGroup = DispatchGroup()  
self.PersonalSearchesList = [] 


currentUserFirebaseReference.child("rooms").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in 


    if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] { 
     for snap in snapshots { 
      dispatchGroup.enter() 

单时间

  DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in 

       if snapshot.value != nil { 

        if let users = (snapshot.value! as? NSDictionary)?["users"] as? Dictionary<String,AnyObject> { 

         DataService.ds.REF_USERS.child(topUser).child("pictureURL").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in 

          self.PersonalSearchesList.append(eachSearch) 
          print("snap") 
          print(self.PersonalSearchesList.count) 
         }) 
        } 
       } 
       dispatchGroup.leave() 
      }) 

     } 

     dispatchGroup.notify(queue: DispatchQueue.global(), execute: { 
      print("Second one") 
      print(self.PersonalSearchesList.count) 

      print("Calling to set my sorted PersonalSearchesList") 
      self.mySortedList = self.PersonalSearchesList.sorted{ $0.users > $1.users } 
     }) 


    } 
} 
initialLoadOver = true 
} 

我不能完全肯定,你可以进入并在同一个线程离开组几次,所以你可能需要包装的代码输入到年底在另一个线程的回调,并称之为异步

+0

我在代码中编辑我想我是从该网站拉。它看起来像一般的概念是建立一个队列,输入它,退出它,然后在你退出队列的时候?或者它已经完成了队列中的所有事情?......然后在那时你运行你想要运行的代码。 所以在我的情况下,我会在顶部定义队列,在我的观察者开始时输入。?结束在我现在打电话“第二个”,然后在通知内部运行我的排序列表位? 我可能会在这里走,所以请耐心等待。 – user6820041

+0

继续前进,给了你答案,因为我认为你已经告诉我所有我需要能够解决这个问题,但我用Whatcha给了我同样的结果。 当我打印PersonalSearchesList.count我越来越0. 因此,随着你的第二次reccomendation我会创建另一个组,在我进入之前开始,结束后我在第一个结束,然后调用通知在整个事情在别处? – user6820041