From b42fd2f36ae4004f6a7a2530258f88caf4368976 Mon Sep 17 00:00:00 2001 From: juhwankim-dev Date: Wed, 3 Jun 2026 22:55:03 +0900 Subject: [PATCH] =?UTF-8?q?NR-152=20datastore=20=EC=82=AC=EC=9A=A9?= =?UTF-8?q?=EC=8B=9C=20runBlocking=20=EC=82=AC=EC=9A=A9=ED=95=98=EB=8A=94?= =?UTF-8?q?=20=EB=B6=80=EB=B6=84=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 왜? 이전 담당자가 datastore를 동기로 실행하기 위해 runBlocking을 사용한듯하다. 이 때문에 ANR이 발생하는 케이스가 생기고 있다. runBlocking을 모두 제거하도록 한다. --- .../data/datasource/SettingDataSource.kt | 27 +++++++++-------- .../repository/DataStoreRepositoryImpl.kt | 29 +++++++++---------- .../domain/repository/DataStoreRepository.kt | 8 ++--- .../RecommendBackgroundCustomViewModel.kt | 6 +++- .../ui/theme_select/ThemeSelectViewModel.kt | 2 +- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/data/src/main/java/com/nextroom/nextroom/data/datasource/SettingDataSource.kt b/data/src/main/java/com/nextroom/nextroom/data/datasource/SettingDataSource.kt index 87cce9fa..4eb2ca9d 100644 --- a/data/src/main/java/com/nextroom/nextroom/data/datasource/SettingDataSource.kt +++ b/data/src/main/java/com/nextroom/nextroom/data/datasource/SettingDataSource.kt @@ -7,7 +7,6 @@ import com.nextroom.nextroom.data.db.dataStore import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.map -import kotlinx.coroutines.runBlocking import javax.inject.Inject class SettingDataSource @Inject constructor( @@ -41,17 +40,17 @@ class SettingDataSource @Inject constructor( } } - fun setLastLaunchDate() = runBlocking { + suspend fun setLastLaunchDate() { dataStore.updateData { it.copy(lastLaunchDate = System.currentTimeMillis()) } } - fun getLastLaunchDate(): Long = runBlocking { - data.first().lastLaunchDate + suspend fun getLastLaunchDate(): Long { + return data.first().lastLaunchDate } - fun setNetworkDisconnectedCount(count: Int) = runBlocking { + suspend fun setNetworkDisconnectedCount(count: Int) { dataStore.updateData { it.copy(networkDisconnectedCount = count) } @@ -79,32 +78,32 @@ class SettingDataSource @Inject constructor( } } - fun setEmailSaveChecked(emailSaveChecked: Boolean) = runBlocking { + suspend fun setEmailSaveChecked(emailSaveChecked: Boolean) { dataStore.updateData { it.copy(emailSaveChecked = emailSaveChecked) } } - fun getEmailSaveChecked() = runBlocking { - data.first().emailSaveChecked + suspend fun getEmailSaveChecked(): Boolean { + return data.first().emailSaveChecked } - fun saveUserEmail(userEmail: String) = runBlocking { + suspend fun saveUserEmail(userEmail: String) { dataStore.updateData { it.copy(userEmail = userEmail) } } - fun getUserEmail() = runBlocking { - data.first().userEmail + suspend fun getUserEmail(): String { + return data.first().userEmail } - fun setRecommendBackgroundCustomDialogHidden(time: Long) = runBlocking { + suspend fun setRecommendBackgroundCustomDialogHidden(time: Long) { dataStore.updateData { it.copy(backgroundCustomDialogHideUntil = time) } } - fun getRecommendBackgroundCustomDialogHiddenUntil() = runBlocking { - data.first().backgroundCustomDialogHideUntil + suspend fun getRecommendBackgroundCustomDialogHiddenUntil(): Long { + return data.first().backgroundCustomDialogHideUntil } suspend fun saveAppPassword(password: String) { diff --git a/data/src/main/java/com/nextroom/nextroom/data/repository/DataStoreRepositoryImpl.kt b/data/src/main/java/com/nextroom/nextroom/data/repository/DataStoreRepositoryImpl.kt index 2bf6e5f5..720a6c5e 100644 --- a/data/src/main/java/com/nextroom/nextroom/data/repository/DataStoreRepositoryImpl.kt +++ b/data/src/main/java/com/nextroom/nextroom/data/repository/DataStoreRepositoryImpl.kt @@ -9,23 +9,22 @@ import javax.inject.Inject class DataStoreRepositoryImpl @Inject constructor( private val settingDataSource: SettingDataSource, ) : DataStoreRepository { - override val isFirstInitOfDay: Boolean - get() = run { - val util = DateTimeUtil() - val pattern = "yyyy-MM-dd" - val today = util.longToDateString(System.currentTimeMillis(), pattern) - val lastLaunchedDate = util.longToDateString(settingDataSource.getLastLaunchDate(), pattern) - Timber.tag("MANGBAAM-DataStoreRepositoryImpl()").d("마지막 접속 일자: $lastLaunchedDate") - today != lastLaunchedDate - }.also { firstInit -> - if (firstInit) settingDataSource.setLastLaunchDate() - } - - override fun setRecommendBackgroundCustomDialogHidden(time: Long) { + override suspend fun isFirstInitOfDay(): Boolean { + val util = DateTimeUtil() + val pattern = "yyyy-MM-dd" + val today = util.longToDateString(System.currentTimeMillis(), pattern) + val lastLaunchedDate = util.longToDateString(settingDataSource.getLastLaunchDate(), pattern) + Timber.tag("MANGBAAM-DataStoreRepositoryImpl()").d("마지막 접속 일자: $lastLaunchedDate") + val firstInit = today != lastLaunchedDate + if (firstInit) settingDataSource.setLastLaunchDate() + return firstInit + } + + override suspend fun setRecommendBackgroundCustomDialogHidden(time: Long) { settingDataSource.setRecommendBackgroundCustomDialogHidden(time) } - override fun getRecommendBackgroundCustomDialogHiddenUntil(): Long { + override suspend fun getRecommendBackgroundCustomDialogHiddenUntil(): Long { return settingDataSource.getRecommendBackgroundCustomDialogHiddenUntil() } @@ -33,7 +32,7 @@ class DataStoreRepositoryImpl @Inject constructor( return settingDataSource.getNetworkDisconnectedCount() } - override fun setNetworkDisconnectedCount(count: Int) { + override suspend fun setNetworkDisconnectedCount(count: Int) { settingDataSource.setNetworkDisconnectedCount(count) } diff --git a/domain/src/main/java/com/nextroom/nextroom/domain/repository/DataStoreRepository.kt b/domain/src/main/java/com/nextroom/nextroom/domain/repository/DataStoreRepository.kt index 5b2ad2f6..bf42beca 100644 --- a/domain/src/main/java/com/nextroom/nextroom/domain/repository/DataStoreRepository.kt +++ b/domain/src/main/java/com/nextroom/nextroom/domain/repository/DataStoreRepository.kt @@ -2,12 +2,12 @@ package com.nextroom.nextroom.domain.repository interface DataStoreRepository { suspend fun getNetworkDisconnectedCount(): Int - fun setNetworkDisconnectedCount(count: Int) + suspend fun setNetworkDisconnectedCount(count: Int) - val isFirstInitOfDay: Boolean + suspend fun isFirstInitOfDay(): Boolean - fun setRecommendBackgroundCustomDialogHidden(time: Long) - fun getRecommendBackgroundCustomDialogHiddenUntil(): Long + suspend fun setRecommendBackgroundCustomDialogHidden(time: Long) + suspend fun getRecommendBackgroundCustomDialogHiddenUntil(): Long suspend fun setHasSeenGuidePopup() suspend fun getHasSeenGuidePopup(): Boolean diff --git a/presentation/src/main/java/com/nextroom/nextroom/presentation/ui/theme_select/RecommendBackgroundCustomViewModel.kt b/presentation/src/main/java/com/nextroom/nextroom/presentation/ui/theme_select/RecommendBackgroundCustomViewModel.kt index 1d78f445..59f826d0 100644 --- a/presentation/src/main/java/com/nextroom/nextroom/presentation/ui/theme_select/RecommendBackgroundCustomViewModel.kt +++ b/presentation/src/main/java/com/nextroom/nextroom/presentation/ui/theme_select/RecommendBackgroundCustomViewModel.kt @@ -1,8 +1,10 @@ package com.nextroom.nextroom.presentation.ui.theme_select import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.nextroom.nextroom.domain.repository.DataStoreRepository import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel @@ -12,6 +14,8 @@ class RecommendBackgroundCustomViewModel @Inject constructor( fun onDismissClicked() { val oneWeekLater = System.currentTimeMillis() + (7 * 24 * 60 * 60 * 1000) // 일주일 밀리세컨드 표시 - dataStoreRepository.setRecommendBackgroundCustomDialogHidden(oneWeekLater) + viewModelScope.launch { + dataStoreRepository.setRecommendBackgroundCustomDialogHidden(oneWeekLater) + } } } diff --git a/presentation/src/main/java/com/nextroom/nextroom/presentation/ui/theme_select/ThemeSelectViewModel.kt b/presentation/src/main/java/com/nextroom/nextroom/presentation/ui/theme_select/ThemeSelectViewModel.kt index d8736472..a2ab3612 100644 --- a/presentation/src/main/java/com/nextroom/nextroom/presentation/ui/theme_select/ThemeSelectViewModel.kt +++ b/presentation/src/main/java/com/nextroom/nextroom/presentation/ui/theme_select/ThemeSelectViewModel.kt @@ -154,7 +154,7 @@ class ThemeSelectViewModel @Inject constructor( }.onFailure(::handleResultError) } - private fun shouldHideRecommendBackgroundCustomDialogUntil(): Boolean { + private suspend fun shouldHideRecommendBackgroundCustomDialogUntil(): Boolean { val hideUntil = dataStoreRepository.getRecommendBackgroundCustomDialogHiddenUntil() return System.currentTimeMillis() < hideUntil }