简体   繁体   English

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

[英]IOS: Firebase Query Returns Empty Array even though there are objects in the endpoint

My tree after writing record I write some record and than search for the same record. 写记录后,我的树会写一些记录,然后搜索相同的记录。 It shows empty during query. 查询期间显示为空。 I restart the app with the above record and this time my query find 3 records. 我用上面的记录重新启动应用程序,这一次我的查询找到了3条记录。 I call the saveUserData function. 我调用saveUserData函数。 It query if any record exist containing facebookId. 它查询是否存在任何包含facebookId的记录。 If so than it update else it write new record. 如果是,则更新,否则将写入新记录。 Since it do not find any record. 由于没有找到任何记录。 It creates new record each of the time. 每次都创建新记录。 So, I called it three times so it create new record 3times. 因此,我叫了3次,所以它创造了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){

    }];

Try this, 尝试这个,

 [[[[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){

        }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM