Closed Pepslee closed 1 year ago
Is it possible to make the computation part as async (isolate) functions ?
When I`m using supercluster for map markers clustering, the huge number of points clusters computation blocks the main thread and Ui starts to freeze.
Definitely. I was already hoping to get to this soon along with an indication of the loading state.
@Pepslee what do you think about an option to wrap the loading with whatever function you like? That way you can call Flutter's compute
which spawns a new isolate or, if you spawn and manage your own isolates, you can use them instead which will save the time required to spawn the isolate.
@Pepslee So I mistakenly though that this question was in the flutter_map_supercluster
repo which is where I wanted to implement this and have now done so. I have also published a new version of this package which was necessary to make those changes.
So if you are using the flutter_map_supercluster
package for flutter_map
then this is resolved as it now creates the supercluster in a separate isolate by default or allows you to manage the creation how you like. If instead you have written your own package based on this one then you can do the isolate computation in a couple of different ways. First you need to define a static or top level (not inside a class) function like so:
Supercluster<YourPointType> loadSupercluster(List<YourPointType> points) {
final supercluster = SuperclusterImmutable<YourPointType>(/*your options here*/);
return supercluster..load(points);
}
Then you can either call compute
:
class YourClass {
Future<Supercluster<YourPointType>> createSupercluster(List<YourPointType> points) {
return compute(loadSupercluster, points);
}
}
Or you can handle isolates directly (flutter just makes this easier with compute
) or using one of the many packages which manage isolates for you. The advantage of using a package that manages isolates is that you can pre-create an isolate and keep it loaded to avoid the delay caused by creating a new isolate when calling compute
. That said compute
is certainly simpler.
I'm going to close this for now but let me know if this has not resolved your problem.
@Pepslee So I mistakenly though that this question was in the
flutter_map_supercluster
repo which is where I wanted to implement this and have now done so. I have also published a new version of this package which was necessary to make those changes.So if you are using the
flutter_map_supercluster
package forflutter_map
then this is resolved as it now creates the supercluster in a separate isolate by default or allows you to manage the creation how you like. If instead you have written your own package based on this one then you can do the isolate computation in a couple of different ways. First you need to define a static or top level (not inside a class) function like so:Supercluster<YourPointType> loadSupercluster(List<YourPointType> points) { final supercluster = SuperclusterImmutable<YourPointType>(/*your options here*/); return supercluster..load(points); }
Then you can either call
compute
:class YourClass { Future<Supercluster<YourPointType>> createSupercluster(List<YourPointType> points) { return compute(loadSupercluster, points); } }
Or you can handle isolates directly (flutter just makes this easier with
compute
) or using one of the many packages which manage isolates for you. The advantage of using a package that manages isolates is that you can pre-create an isolate and keep it loaded to avoid the delay caused by creating a new isolate when callingcompute
. That saidcompute
is certainly simpler.I'm going to close this for now but let me know if this has not resolved your problem.
My question was in context of flutter_map_supercluster so you have already helped me a lot
Is it possible to make the computation part as async (isolate) functions ?
When I`m using supercluster for map markers clustering, the huge number of points clusters computation blocks the main thread and Ui starts to freeze.