Firebase observeSingleEventOfType为同一个孩子多次调用ChildAdded

时间:2016-02-24 11:41:47

标签: swift firebase

我目前在Firebase中的应用程序遇到了一个奇怪的问题。

这是我的JSON结构:

{
  "groups" : {
    "-KAv867tzVgIghmr15CM" : {
      "author" : "ruben",
      "name" : "Item A"
    },
    "-KAv87nqLEG1Jtc04Ebn" : {
      "author" : "ruben",
      "name" : "Item B"
    },
    "-KAv88yZe8KTfkjAE7In" : {
      "author" : "ruben",
      "name" : "Item C"
    }
  },
  "users" : {
    "rsenov : {
      "avatar" : "guest",
      "email" : "ruben@ruben.com",
      "groups" : {
        "-KAv867tzVgIghmr15CM" : "true",
        "-KAv87nqLEG1Jtc04Ebn" : "true",
        "-KAv88yZe8KTfkjAE7In" : "true"
      }
    }
  }
}

这是我的DataService类实现:

class DataService {
    static let dataService = DataService()
    private var _USERS_REF = Firebase(url: "\(BASE_URL)/users")
    private var _GROUPS_REF = Firebase(url: "\(BASE_URL)/groups")

    var USERS_REF: Firebase {
        return _USERS_REF
    }

    var GROUPS_REF: Firebase {
        return _GROUPS_REF
    }

    var CURRENT_USER_REF: Firebase {
        let userID = NSUserDefaults.standardUserDefaults().valueForKey("uid") as! String
        let currentUser = USERS_REF.childByAppendingPath(userID)

        return currentUser!
    }

    var CURRENT_USER_ID: String {
        let currentUserId = NSUserDefaults.standardUserDefaults().valueForKey("uid") as! String        
        return currentUserId
    }

    func addGroup(group: Dictionary<String, String>, members: [Person], newPersons: [Person]) {

        let groupsRefAutoId = GROUPS_REF.childByAutoId()
        groupsRefAutoId.setValue(group)
        groupsRefAutoId.childByAppendingPath("members").setValue(personsToArray(members))

        CURRENT_USER_REF.childByAppendingPath("groups").updateChildValues([groupsRefAutoId.key : "true"])
    }

    func personsToArray(person: [Person]) -> AnyObject{

        var personsArray: [String:Bool] = [:]

        for persons in person {
            personsArray[persons.autoId] = true

        }
        return personsArray
    }

这是我的ViewController.swift中的代码:

self.handle = DataService.dataService.USERS_REF.childByAppendingPath(DataService.dataService.CURRENT_USER_ID).childByAppendingPath("groups").observeEventType(.ChildAdded, withBlock: { snapshot in

    self.handleArray.append(self.handle)

    let snapKey = snapshot.key
    self.counter++

    DataService.dataService.GROUPS_REF.childByAppendingPath(snapKey).observeSingleEventOfType(.Value, withBlock: {snapshot2 in
        if snapshot2.exists() {

              if (self.notLoaded) {

                      if let childDictionary = snapshot2.value as? Dictionary<String, AnyObject> {
                            let group = Group(key: snapshot2.key, dictionary: childDictionary)
                            self.groups.append(group)
                        }

              }  else {
        }

         if self.countChildren() == self.counter{

                self.counter = 0
                self.notLoaded = false
                self.miTabla.reloadData()
         }
      }
   })
})


DataService.dataService.GROUPS_REF.queryOrderedByChild("author").queryEqualToValue(self.currentUser.email).queryLimitedToLast(1).observeSingleEventOfType(.ChildAdded, withBlock: {snapshotAdded in

            print(snapshotAdded)

                if let childDictionary = snapshotAdded.value as? Dictionary<String, AnyObject> {
                    let group = Group(key: snapshotAdded.key, dictionary: childDictionary)
                    self.groups.append(group)
                    self.miTabla.reloadData()
                }

 })

在这个函数中,最初它首次加载所有数据,然后,当添加一些Child时,它只会调用 DataService.dataService.GROUPS_REF.queryOrderedByChild("author").queryEqualToValue(self.currentUser.email).queryLimitedToLast(1).observeSingleEventOfType(.ChildAdded, withBlock: {snapshotAdded in函数,这里应该只有新孩子。

当我添加一个组时会出现问题,函数DataService.dataService.GROUPS_REF.queryOrderedByChild("author").queryEqualToValue(self.currentUser.email).queryLimitedToLast(1).observeSingleEventOfType(.ChildAdded, withBlock: {snapshotAdded in被调用并且它会随机打印3,4,5 ...同一个孩子,如下所示:

Snap (-KBHzShgnET9_Rg1qLJY) {
    author = "rsenov";
    members =     {
        "rsenov" = 1;
    };
    name = "Item 6";
}
Snap (-KBHzShgnET9_Rg1qLJY) {
    author = "rsenov";
    members =     {
        "rsenov" = 1;
    };
    name = "Item 6";
}
Snap (-KBHzShgnET9_Rg1qLJY) {
    author = "rsenov";
    members =     {
        "rsenov" = 1;
    };
    name = "Item 6";
}

有人知道为什么这个只为被加入的孩子被召唤一次?

0 个答案:

没有答案