M Zubair Shamshad
M Zubair Shamshad

Reputation: 2741

Firebase observeEventType:FIRDataEventTypeChildChanged callback never called

I want to to get real time updated database object. Whenever a new message added in chat list.

If I Use

observeSingleEventOfType:FIRDataEventTypeValue  

it works and respond well. But ofcourse it will give me results once.

If I call the below method:

observeEventType:FIRDataEventTypeChildChanged

as of my knowledge It will give results first time and every time when the child is changed. (new message added)

but this never called back. Not first time not if I add some new message in database.

This is how I m calling these methods:

In my.h file

@property (strong, nonatomic) FIRDatabaseReference *ref;

In viewDidLoad

_ref = [[FIRDatabase database] reference];
[self getChats];

and finally:

-(void) getChats {

//  this is working
[[[_ref child:@"chat_rooms"] child:userID] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

    NSDictionary *dict = snapshot.value;
    NSLog(@"%@",dict);

}];

// this is not working / Never calling
[[[_ref child:@"chat_rooms"] child:userID] observeEventType:FIRDataEventTypeChildChanged
 withBlock:^ (FIRDataSnapshot * _Nonnull snapshot) {

     NSDictionary *dict = snapshot.value;
     NSLog(@"%@",dict);

 }];


 // this is not working / Never calling too
[[[_ref child:@"chat_rooms"] child:userID] observeEventType:FIRDataEventTypeChildAdded
 withBlock:^ (FIRDataSnapshot * _Nonnull snapshot) {

     NSDictionary *dict = snapshot.value;
     NSLog(@"%@",dict);

 }];

}

//this is how I m sending message to database. 
//and this is working good. message added in node. verified at firebase console
- (void)sendMessage : (NSDictionary*) dict
{
    [[[[_ref child:@"chat_rooms"] child:userID] childByAutoId] setValue:dict];
}

Guide me where I m wrong, OR what should I have to use to get (real time) updated chats.

Upvotes: 0

Views: 1312

Answers (3)

M Zubair Shamshad
M Zubair Shamshad

Reputation: 2741

I have Figured out the solution,

For the first time If we don't have the Child Node created under firebase database. In my case it is chat_room with userID

So the observer observeEventType:FIRDataEventTypeChildChanged will never called because observer did not find which child it has to observe. And I guess it is not even registered to observe.

So when I have added first message in chat_room. Node is created for that user. Then I have to get all the records with observeSingleEventOfType:FIRDataEventTypeValue

Then registered the Child Changed observer and finally on every change observeEventType:FIRDataEventTypeChildChanged is working as expected.

Upvotes: 1

Arpit Jain
Arpit Jain

Reputation: 1690

You are re-initializing your FIRDatabaseReference each time you are adding an observer. So try as follows : In viewDidLoad method,

_ref = [[FIRDatabase database] reference];
_ref = [[_ref child:@"chat_rooms"] child:userID]; // Pass Your userID here

Then, call your observer as,

[_ref observeEventType:FIRDataEventTypeChildAdded
 withBlock:^ (FIRDataSnapshot * _Nonnull snapshot) {

     NSDictionary *dict = snapshot.value;
     NSLog(@"%@",dict);

}];

Hope, it works

Upvotes: 1

Caplin YT
Caplin YT

Reputation: 265

You’re totally wrong, if you want to get the realtime value of an object you may wanted to use FIRDataEventTypeAdded on the type of Firebase database reference

Upvotes: 0

Related Questions