Skip to content

Commit

Permalink
Using semaphore rather than synchronizing on generic object.
Browse files Browse the repository at this point in the history
  • Loading branch information
cafesilencio committed Jul 8, 2024
1 parent 9b40acb commit 51988a2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.mapbox.navigation.examples.mincompile
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.mapbox.navigation.examples.mincompile.databinding.LayoutActivityDropinBinding
import com.mapbox.navigation.examples.mincompile.databinding.LayoutActivityMainBinding
import com.mapbox.navigation.examples.mincompile.databinding.LayoutActivityNavigationBinding

class DropInUIActivity : AppCompatActivity() {
private lateinit var binding: LayoutActivityDropinBinding
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.mapbox.navigation.base.internal.route

import android.util.Log
import android.util.LruCache
import com.mapbox.api.directions.v5.models.DirectionsRoute
import com.mapbox.navigation.base.route.NavigationRoute

/**
* **Internal** cache object that aims to improve the performance performance of compatibility functions
* **Internal** cache object that aims to improve the performance of compatibility functions
* that transform the [DirectionsRoute] route into a [NavigationRoute].
*
* It caches up to 3 randomly created [NavigationRoute] instances,
Expand Down Expand Up @@ -34,6 +35,19 @@ object RouteCompatibilityCache {
* Use to put all routes tracked by `MapboxDirectionsSession` to cache (and clear everything else).
*/
fun setDirectionsSessionResult(routes: List<NavigationRoute>) {
// A crash was reported due to an LruCache inconsistency.
// The caching implementation is using java.concurrent
// classes for synchronization. This method is getting called
// from a coroutine upstream. Putting this call on it's own thread
// should make the synchronization effective.
Thread {
run {
setDirectionsSessionResultInternal(routes)
}
}
}

private fun setDirectionsSessionResultInternal(routes: List<NavigationRoute>) {
synchronized(lock) {
creationCache.evictAll()
directionsSessionCache.clear()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.mapbox.navigation.core

import android.Manifest.permission.ACCESS_COARSE_LOCATION
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.util.Log
import androidx.annotation.RequiresPermission
import androidx.annotation.UiThread
import androidx.annotation.VisibleForTesting
Expand Down Expand Up @@ -54,6 +55,7 @@ import com.mapbox.navigation.core.arrival.AutoArrivalController
import com.mapbox.navigation.core.directions.LegacyNavigationRouterAdapter
import com.mapbox.navigation.core.directions.LegacyRouterAdapter
import com.mapbox.navigation.core.directions.session.DirectionsSession
import com.mapbox.navigation.core.directions.session.DirectionsSessionRoutes
import com.mapbox.navigation.core.directions.session.RoutesExtra
import com.mapbox.navigation.core.directions.session.RoutesObserver
import com.mapbox.navigation.core.directions.session.RoutesSetStartedParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ internal class MapboxDirectionsSession(
) {
return
}

RouteCompatibilityCache.setDirectionsSessionResult(routes.acceptedRoutes)

val result = routes.toRoutesUpdatedResult().also { routesUpdatedResult = it }
Expand Down

0 comments on commit 51988a2

Please sign in to comment.