Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create multi infinity loops with coroutine in Kotlin

I have a method in viewmodel that I want to execute infinity till client stop that.

This loop should work for each button separately and stop that too.
But when I execute the loop for fourth time, application hangs.
How can I manage the loop and run it for four separate objects

This is my method in viewmodel:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

fun getLocationInfinity(context: Context, tripId: Long, passengerId: Int) =
    viewModelScope.launch {
            val gpsTracker = LocationGpsTracker(context, 0, 0)
            val netGpsTracker = LocationGpsTrackerNetwork(context)
            var way = Way()
            way.latitude1 = gpsTracker.getLatitude()
            way.longitude1 = gpsTracker.getLongitude()
            way.accuracy1 = gpsTracker.getAccuracy()
            way.latitudeNet1 = netGpsTracker.getLatitude()
            way.longitudeNet1 = netGpsTracker.getLongitude()
            way.accuracyNet1 = netGpsTracker.getAccuracy()

            while (isActive) {

                if (_passengerSwitch.value?.get(passengerId - 1) == true) {
                way.latitude2 = way.latitude1
                way.longitude2 = way.longitude1
                way.accuracy2 = way.accuracy1
                way.latitudeNet2 = way.latitudeNet1
                way.longitudeNet2 = way.longitudeNet1
                way.accuracyNet2 = way.accuracyNet1
                way.latitude1 = gpsTracker.getLatitude()
                way.longitude1 = gpsTracker.getLongitude()
                way.accuracy1 = gpsTracker.getAccuracy()
                way.latitudeNet1 = netGpsTracker.getLatitude()
                way.longitudeNet1 = netGpsTracker.getLongitude()
                way.accuracyNet1 = netGpsTracker.getAccuracy()
                _way.postValue(way)
                val tripDetails = TripDetails(
                    latitude1 = way.latitude1,
                    latitude2 = way.latitude2,
                    longitude1 = way.longitude1,
                    longitude2 = way.longitude2,
                    accuracy1 = way.accuracy1,
                    accuracy2 = way.accuracy2,
                    latitudeNet1 = way.latitudeNet1,
                    latitudeNet2 = way.latitudeNet2,
                    longitudeNet1 = way.longitudeNet1,
                    longitudeNet2 = way.longitudeNet2,
                    accuracy1Net = way.accuracyNet1,
                    accuracy2Net = way.accuracyNet2,
                    distance = null,
                    isCalculated = false,
                    tripId = tripId.toInt(),
                    isEnded = false
                )
                localRepository.insertLocation(tripDetails)
                delay(2000)
            }
        }


    }

>Solution :

The delay() call needs to be outside your if-block. Otherwise, if the condition is false, this loop will never suspend so it never relinquishes the thread.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading