Kotlin Coroutines wait for job to complete - android

I have a ViewModel. Inside it, I have a function which fetches some images from the phones internal storage.
Before the fetching is complete it is exposing livedata in mainactivity. How to make coroutines to wait for the task to complete and expose the live data.
// This is my ViewModel
private val _image = MutableLiveData<ArrayList<File>>()
val images: LiveData<ArrayList<File>> get() = _image
fun fetchImage(){
val file = Path.imageDirectory // returns a directory where images are stored
val files = arrayListOf<File>()
viewModelScope.launch {
withContext(Dispatchers.IO) {
if (file.exists()) {
file.walk().forEach {
if (it.isFile && it.path.endsWith("jpeg")) {
files.add(it)
}
}
}
files.sortByDescending { it.lastModified() } // sort the files to get newest
// ones at top of the list
}
}
_image.postValue(files)
}
Is there any other approach to make this code much faster by any other methods?

Do it like this:
fun fetchImage() = viewModelScope.launch {
val file = Path.imageDirectory // returns a directory where images are stored
val files = arrayListOf<File>()
withContext(Dispatchers.IO) {
if (file.exists()) {
file.walk().forEach {
if (it.isFile && it.path.endsWith("jpeg")) {
files.add(it)
}
}
}
files.sortByDescending { it.lastModified() } // sort the files to get newest
// ones at top of the list
}
_image.value = files
}

Related

Data stored in realm is getting overwritten by initial values

In my KMM project I have configured Realm in my shared module. When I try to write in the realm it works fine as shown in picture below, that data is saved,
But when I try to retrieve this data back, orderId field gets overwritten by it's default value which is 0.
this is how I am storing and retrieving data from realm.
In realm service class
class RealmServiceImpl(private val realm: Realm) {
fun insertPackedOrder(order: OrderDomainModel) {
CoroutineScope(Dispatchers.Default).async {
writeOrderPacked(order)
}
}
private suspend fun writeOrderPacked(order: OrderDomainModel) =
realm.write {
copyToRealm(
order.asOrderEntity()
)
}
fun removePackedOrder(order: OrderDomainModel) {
CoroutineScope(Dispatchers.Default).launch {
removeOrder(order)
}
}
private suspend fun removeOrder(order: OrderDomainModel) {
realm.write {
val deletable = this.query<OrderEntity>("orderId == ${order.orderItemId}").find().first()
delete(deletable)
}
}
fun getPackedOrders() = realm.query<OrderEntity>().find()
}
And this is how I am fetching data in my repository,
suspend fun getShippingBookedOrders(): DataState<List<OrdersDTO>> {
return withContext(Dispatchers.Default + SupervisorJob()) {
val ktor = async {
ktorService.getShippingBookedOrders(supplierId = supplier!!.sid)
}
val orders = async {
realmServiceImpl.getPackedOrders()
}
val ktorServiceList = ktor.await()
val ordersList = orders.await()
ktorServiceList.data?.asDomainModel()?.map { item ->
item.isPacked = ordersList.any {
item.orderItemId == it.orderId
}
}
ktorServiceList
}
}
I am getting data from remote server as well using Ktor and then mapping it with data being retrieved from Realm.

App slow after making a request inside another request

I am making a request with coroutines based on a user name, which returns a list of Object<Profile>, and with that list I am making another request with each object, and then switching and passing the info to another screen, but such process is making the app super slow and I would like to find a better way or a way to not making this process so slow. Here my code
Fragment from where I am starting the process and where the app is getting super slow
emptyHomeViewModel.getPlayersListByName(text)
emptyHomeViewModel.listOfPlayersByNameLiveData.observe(viewLifecycleOwner) { playersByName ->
emptyHomeViewModel.getPlayersProfileByName(playersByName)
emptyHomeViewModel.listOfProfilesByID.observe(viewLifecycleOwner) { profiles ->
if (profiles != null) {
val list: Array<Profile> = profiles.toTypedArray()
bundle = Bundle().apply {
putSerializable("user", list)
}
findNavController().navigate(
R.id.action_emptyHomeFragment_to_selectUserFragment,
bundle
)
}
}
}
ViewModel from where I am executing the coroutines and making the request to the API
fun getPlayersListByName(playerName: String) = viewModelScope.launch {
val playersList = getPlayersByPersonaNameUseCase.getPlayersByName(playerName)
if (playersList != null) {
_listOfPlayersByNameLiveData.postValue(playersList)
}
}
fun getPlayersProfileByName(playersByName: List<PlayerByPersonaNameItem>?) =
viewModelScope.launch {
var playersProfileList: ArrayList<Profile> = arrayListOf()
if (playersByName != null) {
for (player in playersByName) {
getPlayerByIDUseCase.getPlayerById(player.accountId)
?.let { playersProfileList.add(it) }
}
_listOfProfilesByID.postValue(playersProfileList)
}
}
You can actually load profiles in parallel, preventing loading them one after another, to decrease time of loading data:
fun getPlayersProfileByName(playersByName: List<PlayerByPersonaNameItem>?) =
viewModelScope.launch {
val playersProfileList: List<Profile> = playersByName?.map { player ->
async {
getPlayerByIDUseCase.getPlayerById(player.accountId)
}
}.awaitAll().filterNotNull()
_listOfProfilesByID.postValue(playersProfileList)
}
Also you can improve it a little bit by removing additional LiveData observer and calling getPlayersProfileByName right after you get playersList:
fun getPlayersListByName(playerName: String) = viewModelScope.launch {
val playersList = getPlayersByPersonaNameUseCase.getPlayersByName(playerName)
getPlayersProfileByName(playersList)
}

Using nested CoroutineScopes to upload images and keeping track of them

I am a newbie to android coroutines my requirements
Need to upload 20 images
Keep track of upload(at least when it gets finished I need to hide progressBar of each image)
After uploading all the images need to enable a "next" button also
Here is my try:
private fun startUploading(){
// Get AWS data
val accessKey = sharedPreferences.getString(getString(R.string.aws_access_key), "").toString()
val secretKey = sharedPreferences.getString(getString(R.string.aws_secret_key), "").toString()
val bucketName = sharedPreferences.getString(getString(R.string.aws_bucket_name), "").toString()
val region = sharedPreferences.getString(getString(R.string.aws_region), "").toString()
val distributionUrl = sharedPreferences.getString(getString(R.string.aws_distribution_url), "").toString()
var totalImagesNeedToUpload = 0
var totalImagesUploaded = 0
CoroutineScope(Dispatchers.IO).launch {
for (i in allCapturedImages.indices) {
val allImageFiles = allCapturedImages[i].viewItem.ImageFiles
totalImagesNeedToUpload += allImageFiles.size
for (j in allImageFiles.indices) {
CoroutineScope(Dispatchers.IO).launch {
while (true) {
val internetActive = utilsClassInstance.hasInternetConnected()
if (internetActive){
try {
val file = allImageFiles[j]
if (!file.uploaded) {
// Upload the file
val cfUrl = utilsClassInstance.uploadFile(file.imageFile, accessKey, secretKey, bucketName, region, distributionUrl)
// Set the uploaded status to true
file.uploaded = true
file.uploadedUrl = cfUrl
// Increment the count of total uploaded images
totalImagesUploaded += 1
// Upload is done for that particular set image
CoroutineScope(Dispatchers.Main).launch {
mainRecyclerAdapter?.uploadCompleteForViewItemImage(i, j, cfUrl)
// Set the next button enabled
if (totalImagesUploaded == totalImagesNeedToUpload){
binding.btnNext.isEnabled = true
}
}
break
}else{
totalImagesUploaded += 1
break
}
} catch (e: Exception) {
println(e.printStackTrace())
}
}
}
CoroutineScope(Dispatchers.Main).launch {
if (totalImagesUploaded == totalImagesNeedToUpload){
updateProgressForAllImages()
binding.btnNext.isEnabled = true
}
}
}
}
}
}
}
fun uploadFile(file: File, accessKey:String, secretKey:String, bucketName: String, region:String, distributionUrl: String): String{
// Create a S3 client
val s3Client = AmazonS3Client(BasicAWSCredentials(accessKey, secretKey))
s3Client.setRegion(Region.getRegion(region))
// Create a put object
val por = PutObjectRequest(bucketName, file.name, file)
s3Client.putObject(por)
// Override the response headers
val override = ResponseHeaderOverrides()
override.contentType = "image/jpeg"
// Generate the url request
val urlRequest = GeneratePresignedUrlRequest(bucketName, file.name)
urlRequest.responseHeaders = override
// Get the generated url
val url = s3Client.generatePresignedUrl(urlRequest)
return url.toString().replace("https://${bucketName}.s3.amazonaws.com/", distributionUrl)
}
There are total "n" images that I need to upload
every image is getting uploaded in different Coroutine because I need to do the parallel upload
The whole question is how to know that all the images are uploaded and enable a next button?
Your code seems very unstructured. You have an infinite loop checking for network availability. You have a nested loop here to upload images (Why?). You are creating a lot of coroutine scopes and have no control over them
Based on the 3 requirements that you mentioned in the question, you can do something like this:
val imagesToUpload: List<File> = /* ... */
var filesUploaded = 0
lifecycleScope.launchWhenStarted {
coroutineScope { // This will return only when all child coroutines have finished
imagesToUpload.forEach { imageFile ->
launch { // Run every upload in parallel
val url = utilsClassInstance.uploadFile(file.imageFile, ...) // Assuming this is a non-blocking suspend function.
filesUploaded++
// Pass the `url` to your adapter to display the image
binding.progressBar.progress = (filesUploaded * 100) / imagesToUpload.size // Update progress bar
}
}
}
// All images have been uploaded at this point.
binding.btnNext.enabled = true
}
Ideally you should have used a viewModelScope and the upload code should be in a repository, but since you don't seem to have a proper architecture in place, I have used lifecycleScope which you can get inside an Activity or Fragment

How to Wait response from Server in forEach with Coroutines

I recently started working with coroutines.
The task is that I need to check the priority parameter from the List and make a request to the server, if the response from the server is OK, then stop the loop.
var minPriority = 0
list.forEach { model ->
if (model.priority > minPriority) {
makeRequest(model.value)
minPriority = model.priority
}
}
private fun makeRequest(value: String) {
scope.launch() {
val response = restApi.makeRequest()
if response.equals("OK") {
**stop list foreach()**
}
}
}
In RxJava, this was done using the retryWhen() operator, tell me how to implement this in Coroutines?
I suggest making your whole code suspendable, not only the body of makeRequest() function. This way you can run the whole operation in the background, but internally it will be sequential which is easier to code and maintain.
It could be something like this:
scope.launch() {
var minPriority = 0
list.forEach { model ->
if (model.priority > minPriority) {
val response = restApi.makeRequest()
if response.equals("OK") {
return#forEach
}
minPriority = model.priority
}
}
}
Of if you need to keep your makeRequest() function separate:
fun myFunction() {
scope.launch() {
var minPriority = 0
list.forEach { model ->
if (model.priority > minPriority) {
if (makeRequest(model.value)) {
return#forEach
}
minPriority = model.priority
}
}
}
}
private suspend fun makeRequest(value: String): Boolean {
val response = restApi.makeRequest()
return response.equals("OK")
}

Download images from a URL, save them to App Internal Storage without blocking calls (multiple files in parallel). Using Kotlin Coroutines on Android

Basically, I am trying to download three different images(bitmaps) from a URL and save them to Apps Internal storage, and then use the URI's from the saved file to save a new Entity to my database. I am having a lot of issues with running this in parallel and getting it to work properly. As ideally all three images would be downloaded, saved and URI's returned simultaneously. Most of my issues come from blocking calls that I cannot seem to avoid.
Here's all of the relevant code
private val okHttpClient: OkHttpClient = OkHttpClient()
suspend fun saveImageToDB(networkImageModel: CBImageNetworkModel): Result<Long> {
return withContext(Dispatchers.IO) {
try {
//Upload all three images to local storage
val edgesUri = this.async {
val req = Request.Builder().url(networkImageModel.edgesImageUrl).build()
val response = okHttpClient.newCall(req).execute() // BLOCKING
val btEdges = BitmapFactory.decodeStream(response.body?.byteStream())
return#async saveBitmapToAppStorage(btEdges, ImageType.EDGES)
}
val finalUri = this.async {
val urlFinal = URL(networkImageModel.finalImageUrl) // BLOCKING
val btFinal = BitmapFactory.decodeStream(urlFinal.openStream())
return#async saveBitmapToAppStorage(btFinal, ImageType.FINAL)
}
val labelUri = this.async {
val urlLabels = URL(networkImageModel.labelsImageUrl)
val btLabel = BitmapFactory.decodeStream(urlLabels.openStream())
return#async saveBitmapToAppStorage(btLabel, ImageType.LABELS)
}
awaitAll(edgesUri, finalUri, labelUri)
if(edgesUri.getCompleted() == null || finalUri.getCompleted() == null || labelUri.getCompleted() == null) {
return#withContext Result.failure(Exception("An image couldn't be saved"))
}
} catch (e: Exception) {
Result.failure<Long>(e)
}
try {
// Result.success( db.imageDao().insertImage(image))
Result.success(123) // A placeholder untill I actually get the URI's to create my Db Entity
} catch (e: Exception) {
Timber.e(e)
Result.failure(e)
}
}
}
//Save the bitmap and return Uri or null if failed
private fun saveBitmapToAppStorage(bitmap: Bitmap, imageType: ImageType): Uri? {
val type = when (imageType) {
ImageType.EDGES -> "edges"
ImageType.LABELS -> "labels"
ImageType.FINAL -> "final"
}
val filename = "img_" + System.currentTimeMillis().toString() + "_" + type
val file = File(context.filesDir, filename)
try {
val fos = file.outputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (e: Exception) {
Timber.e(e)
return null
}
return file.toUri()
}
Here I am calling this function
viewModelScope.launch {
val imageID = appRepository.saveImageToDB(imageNetworkModel)
withContext(Dispatchers.Main) {
val uri = Uri.parse("$PAINT_DEEPLINK/$imageID")
navManager.navigate(uri)
}
}
Another issue I am facing is returning the URI in the first place and handling errors. As if one of these parts fails, I'd like to cancel the whole thing and return Result.failure(), but I am unsure on how to achieve that. As returning null just seems meh, I'd much prefer to have an error message or something along those lines.

Categories

Resources