Home > Net >  How to create multi infinity loops with coroutine in Kotlin
How to create multi infinity loops with coroutine in Kotlin

Time:12-05

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:

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)
            }
        }


    }

CodePudding user response:

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.

  • Related