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

Status emission problem #38

Open
SumitProgrammer5955 opened this issue Oct 25, 2024 · 3 comments
Open

Status emission problem #38

SumitProgrammer5955 opened this issue Oct 25, 2024 · 3 comments

Comments

@SumitProgrammer5955
Copy link

When I start download something and then i cancel after 2 to 3 second after repeating this 2 to 3 time in the 4th time when I finally downloaded that file all the previous emission of those file also emitted like PROGRESS 4 times, SUCCESS 4 times below

Logs :
collectLatest ketch QUEUED
collectLatest ketch QUEUED
collectLatest ketch QUEUED
collectLatest ketch QUEUED
collectLatest ketch STARTED
collectLatest ketch STARTED
collectLatest ketch STARTED
collectLatest ketch STARTED
collectLatest ketch PROGRESS
collectLatest ketch PROGRESS
collectLatest ketch PROGRESS
collectLatest ketch PROGRESS
collectLatest ketch SUCCESS
collectLatest ketch SUCCESS
collectLatest ketch SUCCESS
collectLatest ketch SUCCESS

What I expect is after QUEUED status all the STARTED, PROGRESS and SUCCESS should be called only once.

@khushpanchal
Copy link
Owner

which version you are using, in version 2.0.1, you can use observeDownloadById

You can see the function in DownloadManager, added distinctUntilChanged so it is only observed again when something changes in DownloadModel
fun observeDownloadById(id: Int): Flow<DownloadModel> { return downloadDao.getEntityByIdFlow(id).filterNotNull().distinctUntilChanged().map { entity -> entity.toDownloadModel() } }

@SumitProgrammer5955
Copy link
Author

Thanks for your time.

I alredy did this but it wont work here is the code.

suspend fun Context.myKetchObserver(id : Int, callback:(DownloadModel) -> Unit) {
ketchDownloadHelper().observeDownloadById(id)
.distinctUntilChangedBy{ it.status }
.collectLatest {
Log.d("sumit download", "collectLatest ketch ${it.status.name}")
callback(it)
}
}

@khushpanchal
Copy link
Owner

Are you cancelling the scope from which you are calling it, before downloading it again?

Since observeDownloadById is returning the flow every time you call it, it will start observing, so if your scope is not cancelled, it will call multiple times.

For e.g: something like this,

 job?.cancel()
            job = viewLifecycleOwner.lifecycleScope.launch {
                repeatOnLifecycle(Lifecycle.State.STARTED) {
                    ketch.observeDownloadById(id)
                        .flowOn(Dispatchers.IO)
                        .collect {
                            adapter.submitList(listOf(it))
                            Log.i("Testing", "${it.status}")
                        }
                }
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants