Search code examples
iosobjective-carraysfirebase-realtime-databasefirebaseui

viewDidLoad getting called every time a child is added to firebase database


I am creating a chat view and below is my code to get messages from the db,

- (void)viewDidLoad {
FIRDatabaseReference *tenantRef = [[FIRDatabase database] reference];
    [[[[tenantRef child:@"tenantAgreements"] child:userId] child:_propertyId ] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot){
        //If no previous agreement in teenant agreements for this user or no agreements for this property ID
        if(snapshot.value == [NSNull null]) {
            FIRDatabaseReference *agreementCreateReference = [[[FIRDatabase database] referenceWithPath:@"/agreements/"] childByAutoId];
            agreementId = agreementCreateReference.key;
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
            NSString *url = [NSString stringWithFormat:@"https://krib-api-onbit.herokuapp.com/api/agreements?agreementId=%@&listingId=%@",agreementCreateReference.key,_propertyId];
            [request setURL:[NSURL URLWithString:url]];
            [request setHTTPMethod:@"POST"];
            [request setValue:idToken forHTTPHeaderField:@"X-FIREBASE-ID-TOKEN"];
            [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

            NSURLSession *session = [NSURLSession sharedSession];
            NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                NSString *res = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            }];
            [dataTask resume];
        }
        else{ //If already a agreements for this property for this user exist.
            agreementId = snapshot.value;
            FIRDatabaseReference *getMessagesRef = [[FIRDatabase database] referenceWithPath:[NSString stringWithFormat:@"/messages/%@",snapshot.value]];
            [getMessagesRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * snapshot) {
                NSLog(@"snapshotssnapshots %@",snapshot);
                if(snapshot != NULL){
                    for(snapshot in snapshot.children){
                        [self.arr_text addObject:snapshot];
                    }
                    [self.tableView reloadData];
                }
            }];
        }
    }];
}

Whenever I click the send button in the text field after typing something the viewDidLoad is getting called again and it is adding data to the self.arr_text again. Below is my code for send button click,

- (IBAction)getMessage:(id)sender {
    FIRDatabaseReference *firebaseMessagesRef = [[FIRDatabase database] reference];
    FIRDatabaseReference *id = [firebaseMessagesRef childByAutoId];
    [[[[firebaseMessagesRef child:@"messages"] child:agreementId] child:id.key] setValue:@{@"senderId":userId,@"text":_textField.text,@"timestamp":[FIRServerValue timestamp]}];
}

Below is my tableview code,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"myCell";
    ChatTableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    FIRDataSnapshot *snaps = [self.arr_text objectAtIndex:indexPath.row];
    cell.mylabel.text = snaps.value[@"text"];
    cell.mylabel.backgroundColor = [UIColor grayColor];
    cell.mylabel.layer.masksToBounds = YES;
    cell.mylabel.layer.cornerRadius = 8.0;

    cell.myImg.layer.cornerRadius = cell.myImg.frame.size.width/2;;
    cell.myImg.clipsToBounds = YES;
    [cell.myImg setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[profile valueForKey:@"photoUrl"]]]]];
    return cell;
}

I can't find why it is getting called whenever I add a new child to the db.


Solution

  • It's not your viewDidLoad that gets called multiple times, it's the observation block that does (which acts as a separate function).

    [.. observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot){
    

    According to the documentation, FIRDataEventTypeValue - "Read and listen for changes to the entire contents of a path.", so your block will get called whenever a change occurs in your firebase node.

    By the way, if you want the block to be called just once, there's an example here - you'll need to use the method observeSingleEventOfType:withBlock:withCancelBlock: (or observeSingleEventOfType:withBlock:) instead of observeEventType:withBlock: