0
發送按鈕2個信息顯示

這裏是我的didpresssend邏輯 - 2條消息出現在視圖中,但頁面被刷新時,實際上只有1個消息是在我的消息數組。爲什麼當我按下JSQMessagesViewController

- (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 
     } 
    }]; 
} 

回答

0

它的東西做的動作被asyncronously執行 - 現在,我可以立即發佈消息,然後再刪除該消息,如果火力地堡操作失敗:

但沒有任何人知道爲什麼會這樣發生?這與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]; 
     } 
    }]; 
} 
相關問題