From 77fda0f987e3f0efdeeaa725b8d92aa12bc6d926 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Wed, 29 May 2019 19:46:15 +0200 Subject: [android] copy features array before passing them to core --- .../mapbox/mapboxsdk/style/sources/GeoJsonSource.java | 19 +++++++++++++------ .../mapboxsdk/testapp/style/GeoJsonSourceTests.java | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java index f2de07bdd0..407ec4aa36 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/style/sources/GeoJsonSource.java @@ -285,14 +285,21 @@ public class GeoJsonSource extends Source { * Updates the GeoJson. The update is performed asynchronously, * so the data won't be immediately visible or available to query when this method returns. * - * @param features the GeoJSON FeatureCollection + * @param featureCollection the GeoJSON FeatureCollection */ - public void setGeoJson(FeatureCollection features) { + public void setGeoJson(FeatureCollection featureCollection) { if (detached) { return; } checkThread(); - nativeSetFeatureCollection(features); + + List features = featureCollection.features(); + if (features != null) { + List featuresCopy = new ArrayList<>(features); + nativeSetFeatureCollection(FeatureCollection.fromFeatures(featuresCopy)); + } else { + nativeSetFeatureCollection(featureCollection); + } } /** @@ -445,7 +452,7 @@ public class GeoJsonSource extends Source { *

* * @param cluster cluster from which to retrieve leaves from - * @param limit limit is the number of points to return + * @param limit limit is the number of points to return * @param offset offset is the amount of points to skip (for pagination) * @return a list of features for the underlying leaves */ @@ -497,10 +504,10 @@ public class GeoJsonSource extends Source { private native Feature[] querySourceFeatures(Object[] filter); @Keep - private native Feature[] nativeGetClusterChildren(Feature feature); + private native Feature[] nativeGetClusterChildren(Feature feature); @Keep - private native Feature[] nativeGetClusterLeaves(Feature feature, long limit, long offset); + private native Feature[] nativeGetClusterLeaves(Feature feature, long limit, long offset); @Keep private native int nativeGetClusterExpansionZoom(Feature feature); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/GeoJsonSourceTests.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/GeoJsonSourceTests.java index 99e0ae4016..61086e1344 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/GeoJsonSourceTests.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/GeoJsonSourceTests.java @@ -23,6 +23,8 @@ import org.junit.Test; import org.junit.runner.RunWith; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import timber.log.Timber; @@ -101,6 +103,22 @@ public class GeoJsonSourceTests extends EspressoTest { }); } + @Test + public void testClearCollectionDuringConversion() { + // https://github.com/mapbox/mapbox-gl-native/issues/14565 + validateTestSetup(); + MapboxMapAction.invoke(mapboxMap, (uiController, mapboxMap) -> { + for (int j = 0; j < 1000; j++) { + List features = new ArrayList<>(); + for (int i = 0; i < 100; i++) { + features.add(Feature.fromGeometry(Point.fromLngLat(0, 0))); + } + mapboxMap.getStyle().addSource(new GeoJsonSource("source" + j, FeatureCollection.fromFeatures(features))); + features.clear(); + } + }); + } + @Test public void testPointFeature() { testFeatureFromResource(R.raw.test_point_feature); -- cgit v1.2.1