2017-01-05 11 views
0

My tree after writing record 私はいくつかのレコードを書き、同じレコードを検索します。クエリ中に空を示します。上記のレコードでアプリを再起動し、今回は3つのレコードを検索します。 saveUserData関数を呼び出します。 facebookIdを含むレコードが存在するかどうかを問い合わせます。それ以外の場合は、新しいレコードを書き換えます。以来、それはレコードを見つけることはありません。それはそれぞれの時間ごとに新しいレコードを作成します。だから、私はそれを3回呼んで、新しい記録を3回作りました。IOS:エンドポイントにオブジェクトがあるにもかかわらず、Firebaseクエリが空の配列を返す

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

    }]; 
+0

、これを試してみてください、あなたのFirebase DBツリーを共有してください。 –

答えて

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

     }]; 
+0

同じ問題。まだ同じfacebookIdで3枚のコピーがあります。 –

+0

はい、あなたの必要に応じて、子供、すなわち '@" - K_iqRNNxg_Pjwbz6SNN "を動的に渡す必要があります。 'array'と' forループ 'を使うかもしれません。 –

関連する問題