为什么当我在 JSQMessagesViewController 中按下发送按钮时出现 2 条消息

Why do 2 messages appear when I press the send button in JSQMessagesViewController

这是我的 didpresssend 逻辑 - 视图中出现了 2 条消息,但是当页面刷新时,我的消息数组中实际上只有 1 条消息。

- (void)didPressSendButton:(UIButton *)button withMessageText:(NSString *)text senderId:(NSString *)senderId senderDisplayName:(NSString *)senderDisplayName date:(NSDate *)date {

    JSQMessage *message = [[JSQMessage alloc] initWithSenderId:senderId senderDisplayName:senderDisplayName date:date text:text];

    NSDictionary *firebaseMessage = @{@"avatar":self.avatarURL,@"handle":senderDisplayName,@"message":text,@"profileId":self.profID,@"timestamp":[self getTimestamp]};
    [[self.threadRef childByAutoId] setValue:firebaseMessage withCompletionBlock:^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {
        if (!error) {
            [self.messages addObject:message];
            [self finishSendingMessageAnimated:YES];
            [self sendToServer:text];
        } else {
            //Message Failed
        }
    }];
}

它与异步执行的操作有关 - 现在,我可以立即 post 消息,如果 Firebase 操作失败,稍后删除消息:

但是有人知道为什么会这样吗?与 JSQMessage 数据源的工作方式有关吗?

- (void)didPressSendButton:(UIButton *)button withMessageText:(NSString *)text senderId:(NSString *)senderId senderDisplayName:(NSString *)senderDisplayName date:(NSDate *)date {

    JSQMessage *message = [[JSQMessage alloc] initWithSenderId:senderId senderDisplayName:senderDisplayName date:date text:text];
    [self.messages addObject:message];
    [self finishSendingMessageAnimated:YES];

    NSDictionary *firebaseMessage = @{@"avatar":self.avatarURL,@"handle":senderDisplayName,@"message":text,@"profileId":self.profID,@"timestamp":[self getTimestamp]};
    [[self.threadRef childByAutoId] setValue:firebaseMessage withCompletionBlock:^(NSError * _Nullable error, FIRDatabaseReference * _Nonnull ref) {
        if (!error) {
            [self sendToServer:text];
        } else {
            //Message Failed
            [self.messages removeObject:message];
            [self.collectionView reloadData];
        }
    }];
}