IOS:即使端点中有对象,Firebase查询也会返回空数组

时间:2017-01-05 13:42:17

标签: ios objective-c firebase firebase-realtime-database

My tree after writing record 我写了一些记录,而不是搜索相同的记录。它在查询期间显示为空。我用上面的记录重新启动应用程序,这次我的查询找到3条记录。我调用saveUserData函数。它查询是否存在包含facebookId的任何记录。如果是这样,那么它会更新其他新的记录。因为它没有找到任何记录。它每次创造新的记录。所以,我打了三次电话,所以它创造了3次新记录。

//new part: write User table
+(BOOL) writeOnUserWithValue:(NSMutableDictionary *)value having: (FIRDatabaseReference *)ref{
    FIRDatabaseReference *newRef = [ref child:@"User"].childByAutoId;

    [newRef setValue:value withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref){
        if(error){
            NSLog(@"write on user error %@",[error description]);
        }else{
            NSLog(@"New Url %@",ref.URL);
            [[[[ref child:@"User"] queryOrderedByChild:@"facebookId"] queryEqualToValue:value[@"facebookId"] ] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot){
                NSLog(@"Count after insert: %lu",(unsigned long)snapshot.childrenCount);
            }withCancelBlock:^(NSError *error){
                NSLog(@"Error found in callback query: %@",error);
            }];
        }
    }];
    return true;
}
//new part: update User table
+(BOOL) updateUserWithValue: (NSMutableDictionary *)value having: (FIRDatabaseReference *)ref with: (NSString *)key{
    FIRDatabaseReference *newRef = [[ref child:@"User"] child:value[@"facebookId"]];
    NSLog(@"Key: %@",key);
    [newRef updateChildValues:value withCompletionBlock:^(NSError *error, FIRDatabaseReference *ref){
        if(error){
            NSLog(@"update on user error %@",[error description]);
        }else{
            NSLog(@"Update %@",ref.URL);

        }
    }];
    return true;
}
+(BOOL) saveUserData: (NSMutableDictionary *)value having: (FIRDatabaseReference *)ref{
    //FIRDatabaseReference *newRef = [ref child:@"User"];
    NSLog(@"Reference URL: %@",ref.URL);
    NSLog(@"Value to be saved: %@",[value description]);
    [[[[ref child:@"User"] queryOrderedByChild:@"facebookId"] queryEqualToValue:value[@"facebookId"] ] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot){
        if(snapshot.childrenCount==0){
            NSLog(@"DO NOT EXIST");
            NSLog(@"key before write: %@ %@",snapshot.key,snapshot.value);
            [self writeOnUserWithValue:value having:ref];

        }else{
            NSLog(@"key before write: %lu %@ %@",(unsigned long)snapshot.childrenCount, snapshot.key,snapshot.value);
            NSLog(@"ALREADY EXIST");
            [self updateUserWithValue:value having:ref with:snapshot.key];
        }
    }withCancelBlock:^(NSError *error){

    }];

1 个答案:

答案 0 :(得分:0)

试试这个,

 [[[[ref child:@"User"] child:@"-K_iqRNNxg_Pjwbz6SNN"]] //Make this value dynamic according to your need.

observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot){
            if(snapshot.childrenCount==0){
                NSLog(@"DO NOT EXIST");
                NSLog(@"key before write: %@ %@",snapshot.key,snapshot.value);
                [self writeOnUserWithValue:value having:ref];

            }else{
                NSLog(@"key before write: %lu %@ %@",(unsigned long)snapshot.childrenCount, snapshot.key,snapshot.value);
                NSLog(@"ALREADY EXIST");
                [self updateUserWithValue:value having:ref with:snapshot.key];
            }
        }withCancelBlock:^(NSError *error){

        }];