Varun Hegde
Varun Hegde

Reputation: 135

Spawning Isolates in for loop and processing parallelly in Dart

for(int i=0;i<2;i++){
   ..............preprocessing(pr)...........
   ..... = await someFunc(someData[i]) (this line spawns off new Isolate)
   ..............postprocessing(po)..........
   }

I am writing this kind of function in dart and what I expect is to get this order pr0 -->someFunc(data[0]) --> pr1 --> someFunc(data[1]) --> post processing of whichever (Isolate finishes first ). But I know that I cant get the same effect using for loop. It is giving me of the order pr0 --> someFunc(data[0]) --> pr1 --> someFunc(data[1]) --> po0 --> po1. How should I implement to get the above effect.

Thanks in advance.

Upvotes: 0

Views: 1063

Answers (1)

mmcdon20
mmcdon20

Reputation: 6736

You could try using Future.wait instead (https://api.dart.dev/stable/2.16.1/dart-async/Future/wait.html).

final results = await Future.wait([
  for (int i = 0; i < 2; i++) someFunc(someData[i]),
]);

Upvotes: 1

Related Questions