Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
VLC-Android
Manage
Activity
Members
Labels
Plan
Issues
532
Issue boards
Milestones
Wiki
Code
Merge requests
12
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
VideoLAN
VLC-Android
Commits
875376d4
Commit
875376d4
authored
3 years ago
by
Nicolas Pomepuy
Browse files
Options
Downloads
Patches
Plain Diff
Add a new waitForML method to block the current coroutine while ML init
parent
cb877634
No related branches found
No related tags found
1 merge request
!1128
Wait for the medialibrary to be ready before launching the paged queries
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
+32
-0
32 additions, 0 deletions
...s/src/main/java/org/videolan/resources/util/Extensions.kt
with
32 additions
and
0 deletions
application/resources/src/main/java/org/videolan/resources/util/Extensions.kt
+
32
−
0
View file @
875376d4
...
...
@@ -11,6 +11,11 @@ import java.io.File
import
kotlin.coroutines.resume
/**
* Allows getting a ready medialibrary to query it.
* @param block: the unit to invoke when the medialibrary is ready
*/
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
suspend
inline
fun
<
reified
T
>
Context
.
getFromMl
(
crossinline
block
:
Medialibrary
.()
->
T
)
=
withContext
(
Dispatchers
.
IO
)
{
val
ml
=
Medialibrary
.
getInstance
()
...
...
@@ -36,6 +41,33 @@ suspend inline fun <reified T> Context.getFromMl(crossinline block: Medialibrary
}
}
/**
* Blocks the current coroutine while the medialibrary is not ready.
* Useful when we know the medialibrary init has been launched,
* we already have an instance of it and we want to wait that it's ready to query it
*/
@ExperimentalCoroutinesApi
suspend
inline
fun
waitForML
()
=
withContext
(
Dispatchers
.
IO
)
{
val
ml
=
Medialibrary
.
getInstance
()
if
(!
ml
.
isStarted
){
suspendCancellableCoroutine
<()
->
Unit
>
{
continuation
->
val
listener
=
object
:
Medialibrary
.
OnMedialibraryReadyListener
{
override
fun
onMedialibraryReady
()
{
val
cb
=
this
if
(!
continuation
.
isCompleted
)
launch
(
start
=
CoroutineStart
.
UNDISPATCHED
)
{
continuation
.
resume
{}
yield
()
ml
.
removeOnMedialibraryReadyListener
(
cb
)
}
}
override
fun
onMedialibraryIdle
()
{}
}
continuation
.
invokeOnCancellation
{
ml
.
removeOnMedialibraryReadyListener
(
listener
)
}
ml
.
addOnMedialibraryReadyListener
(
listener
)
}
}
}
fun
Context
.
startMedialibrary
(
firstRun
:
Boolean
=
false
,
upgrade
:
Boolean
=
false
,
parse
:
Boolean
=
true
,
removeDevices
:
Boolean
=
false
,
coroutineContextProvider
:
CoroutineContextProvider
=
CoroutineContextProvider
())
=
AppScope
.
launch
{
if
(
Medialibrary
.
getInstance
().
isStarted
||
!
canReadStorage
(
this
@startMedialibrary
))
return
@launch
val
prefs
=
withContext
(
coroutineContextProvider
.
IO
)
{
Settings
.
getInstance
(
this
@startMedialibrary
)
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment