firebase查询无法正常工作并崩溃

时间:2016-03-10 03:26:39

标签: objective-c firebase

我终于得到了这个查询(差不多)。我正在将数组的值读入NSMutable数组,然后遍历数组并找到==到PFUser的那个(这是我试图从数组中删除的用户的对象ID)...然后我用我在上一个查询中创建的快照进行第二次查询,直接进入不同位置的路径,然后将数组放回来替换旧数组......

     PFUser *user = [PFUser currentUser];
Firebase *firebase = [[Firebase alloc] initWithUrl:[NSString stringWithFormat:@"%@/Recent", FIREBASE]];
FQuery *query = [[firebase queryOrderedByChild:@"groupId"] queryEqualToValue:group.objectId];

        [query observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

            //this is the -KBnmV51zbE5kiHesc1Z
            NSString *nodeKey = snapshot.key;

            //the values are all of the key:value pairs within the node
            NSDictionary *dict = snapshot.value;

            //get the array from the dictionary as mutable so we can mutate is
            NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:[dict valueForKey:@"members"]];

            //remove the object
            [mutableArray removeObject:user];

            //now build a path to write the array back out to
            //for the first node, it will be -KBnmV51zbE5kiHesc1Z/members
            Firebase *path = [firebase childByAppendingPath:nodeKey];
            Firebase *pathToWhereTheMembersArrayIs = [path childByAppendingPath:@"members"];

            //write the mutated array back out to the same place it came from
            [pathToWhereTheMembersArrayIs setValue:mutableArray]; 

        }];

THE JSON

        "-KBnmV51zbE5kiHesc1Z" : {
          "counter" : 220,
          "date" : "2016-03-07T17:43:37",
          "description" : "office",
          "groupId" : "aVIweSJKwn",
          "lastMessage" : "AwEUcyXa0hgPxxAf3G1QjqhsNSv2nthtNBLFfD5WUydrMk0Vm2HwQlZUPyRTYOXZkW3+NCKY5/OM/5JBsn9+cQ7W/0rcFH4a75q2SNU8aaHnGw==",
          "members" : [ "pQJlYrHbq2", "PxOJQe8BdD", "4nJOfpNDzp", "BC8MRaq7Zl", "MvKHg4Hh3L" ],
          "password" : "XClOrgepBf",
          "profileId" : "MvKHg4Hh3L",
          "recentId" : "-KBnmV51zbE5kiHesc1Z",
          "type" : "group",
          "userId" : "4nJOfpNDzp"
        },
        "-KBnmV52zFbMFKI1A9Ye" : {
          "counter" : 220,
          "date" : "2016-03-07T17:43:37",
          "description" : "office",
          "groupId" : "aVIweSJKwn",
          "lastMessage" : "AwEUcyXa0hgPxxAf3G1QjqhsNSv2nthtNBLFfD5WUydrMk0Vm2HwQlZUPyRTYOXZkW3+NCKY5/OM/5JBsn9+cQ7W/0rcFH4a75q2SNU8aaHnGw==",
          "members" : [ "pQJlYrHbq2", "PxOJQe8BdD", "4nJOfpNDzp", "BC8MRaq7Zl", "MvKHg4Hh3L" ],
          "password" : "XClOrgepBf",
          "profileId" : "MvKHg4Hh3L",
          "recentId" : "-KBnmV52zFbMFKI1A9Ye",
          "type" : "group",
          "userId" : "BC8MRaq7Zl"
        }

关于我如何设置我的setvalue我也很困惑...我需要做我先做的相同查询,但我不需要快照,我需要setValue of member其中equalTo:得到满足

2 个答案:

答案 0 :(得分:1)

鉴于Firebase结构的减少

    "-KBnmV51zbE5kiHesc1Z" : {
      "groupId" : "aVIweSJKwn",
      "members" : [ "pQJlYrHbq2", "PxOJQe8BdD", "4nJOfpNDzp", "BC8MRaq7Zl", "MvKHg4Hh3L" ],
    },
    "-KBnmV52zFbMFKI1A9Ye" : {
      "groupId" : "aVIweSJKwn",
      "members" : [ "pQJlYrHbq2", "PxOJQe8BdD", "4nJOfpNDzp", "BC8MRaq7Zl", "MvKHg4Hh3L" ],
    }

当您通过.childAdded事件观察节点时,在快照中一次返回一个观察到(或通过添加子节点查询)的每个节点。

所以返回的第一个节点是

    "-KBnmV51zbE5kiHesc1Z" : {
      "groupId" : "aVIweSJKwn",
      "members" : [ "pQJlYrHbq2", "PxOJQe8BdD", "4nJOfpNDzp", "BC8MRaq7Zl", "MvKHg4Hh3L" ],
    },

然后返回的第二个节点是

    "-KBnmV52zFbMFKI1A9Ye" : {
      "groupId" : "aVIweSJKwn",
      "members" : [ "pQJlYrHbq2", "PxOJQe8BdD", "4nJOfpNDzp", "BC8MRaq7Zl", "MvKHg4Hh3L" ],
    }

对于每个节点,您拥有完成任务所需的一切,即:删除成员数组的成员

所以代码

    [query observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {

        //this is the -KBnmV51zbE5kiHesc1Z node from the snapshot
        NSString *nodeKey = snapshot.key; 

        //the values are all of the key:value pairs within the node
        NSDictionary *dict = snapshot.value; 

        //get the array from the dictionary as mutable so we can mutate it
        NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:[dict valueForKey:@"members"]];

        //remove the object
        [mutableArray removeObject@"PxOJQe8BdD"];

        //now build a path to write the array back out to
        //for the first node, it will be -KBnmV51zbE5kiHesc1Z/members
        Firebase *path = [ref childByAppendingPath:nodeKey];
        Firebase *pathToWhereTheMembersArrayIs = [path childByAppendingPath:@"members"];

        //write the mutated array back out to the same place it came from
        [pathToWhereTheMembersArrayIs setValue:mutableArray]; 

    }];

答案 1 :(得分:0)

我正在提供Android特定答案,但这可能有助于您解决问题。

setValue只是替换引用的节点。因此,如果要检查该节点中是否已存在数据,则必须使用addListenerForSingleValueEvent。这将给出节点的快照。然后,您必须检查数据快照是否存在。因此,如果检查一个成员,其中equalTo:满足,则需要首先转到该特定节点。然后将addListenerForSingleValueEvent添加到该节点,该节点将为您提供该数据的快照。你可以在这里做这样的事情。

firebaseRef.child(member)
    .addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        if (dataSnapshot.exists()) {
            YourClass mClass = dataSnapshot.getValue(YourClass.class);
            // Check if the data has the desired value and take 
            // necessary actions.
        }
        else {
            // setValue here if the data doesn't exists
        }

    }
}