fix(live-room): 하트/캔 카운트 동시 업데이트 시 오차 수정

문제: LiveData.postValue 사용으로 연속 호출 시 병합(coalescing)으로 인해 로스트 업데이트가 발생하여 하트/캔 카운트 누락.
해결: ViewModel에서 메인 스레드 보장 후 setValue(value 할당)로 즉시 갱신하도록 변경. 비메인 스레드 호출 가능성에 대비해 mainHandler로 메인 재호출 처리.
영향: 빠르게 다수의 하트/캔 메시지가 도착해도 각 호출이 정확히 합산되며 오차 제거. 기존 서버 스냅샷 동기화(postValue)는 그대로 유지.
This commit is contained in:
2025-11-06 16:37:17 +09:00
parent f4244d5913
commit ed2258208b

View File

@@ -1,5 +1,6 @@
package kr.co.vividnext.sodalive.live.room
import android.os.Looper
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
@@ -98,6 +99,9 @@ class LiveRoomViewModel(
private val blockedMemberIdList: MutableList<Long> = mutableListOf()
// 메인 스레드 보장을 위한 Handler (postValue의 병합(coalescing) 이슈 방지 목적)
private val mainHandler = android.os.Handler(android.os.Looper.getMainLooper())
fun getUserNickname(memberId: Int): String {
for (manager in roomInfoResponse.managerList) {
if (manager.id.toInt() == memberId) {
@@ -677,11 +681,19 @@ class LiveRoomViewModel(
}
fun addDonationCan(can: Int) {
_totalDonationCan.postValue(totalDonationCan.value!! + can)
if (Looper.myLooper() == Looper.getMainLooper()) {
_totalDonationCan.value = (_totalDonationCan.value ?: 0) + can
} else {
mainHandler.post { addDonationCan(can) }
}
}
fun addHeartDonation(heartCount: Int = 1) {
_totalHeartCount.postValue(totalHeartCount.value!! + heartCount)
if (Looper.myLooper() == Looper.getMainLooper()) {
_totalHeartCount.value = (_totalHeartCount.value ?: 0) + heartCount
} else {
mainHandler.post { addHeartDonation(heartCount) }
}
}
fun donationStatus(roomId: Long, onSuccess: (GetLiveRoomDonationStatusResponse) -> Unit) {