Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using activeSurveyFlow to make flow async #2890

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import timber.log.Timber

/** View model for the Data Collection fragment. */
Expand Down Expand Up @@ -97,7 +100,10 @@ internal constructor(
private var shouldLoadFromDraft: Boolean = savedStateHandle[TASK_SHOULD_LOAD_FROM_DRAFT] ?: false
private var draftDeltas: List<ValueDelta>? = null

private val activeSurvey: Survey = requireNotNull(surveyRepository.activeSurvey)
private val activeSurvey: Survey = runBlocking {
surveyRepository.activeSurveyFlow.filterNotNull().first()
}
Comment on lines +103 to +105
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen if the coroutine is never completed? Will the app freeze until ANR error occurs?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

During the standup today, @anandwana001 suggested adding a timeout here. @gino-m I suggested that we allow the timeout exception to propagate and crash the app, since not being able to load the survey from the local db would be an unrecoverable error.

Ideally, we would ensure the survey was active before rendering the DataCollectionFragment. We could then use SurveyRepository.activeSurvey directly rather than the Flow to get the current survey. @shobhitagarwal1612 Is this possible, based on how the fragment gets activated?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, we would ensure the survey was active before rendering the DataCollectionFragment. We could then use SurveyRepository.activeSurvey directly rather than the Flow to get the current survey. @shobhitagarwal1612 Is this possible, based on how the fragment gets activated?

I don't think so, the fragment gets restored when the activity is started and the injected dependencies are initialized after the fragment is created.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, the fragment gets restored when the activity is started and the injected dependencies are initialized after the fragment is created.

On configuration change, yes, but on app restart we explicitly redirect to this fragment if there's an unsaved draft, no? On configuration change the VM should still exist, so this wouldn't be an issue. In HomeScreenFragment.onViewCreated, however, we navigate to the data collection screen if there's an unsaved draft.

That said, I agree it would be more robust for the fragment which needs the survey to handle the edge cases, rather than the navigation source.

@anandwana001 Let's keep the fix in this PR plus timeout as a workaround, we can add other handle delayed load and other edge cases if it turns out to be an issue.


private val job: Job = activeSurvey.getJob(jobId) ?: error("couldn't retrieve job for $jobId")
private var customLoiName: String?
get() = savedStateHandle[TASK_LOI_NAME_KEY]
Expand Down
Loading