-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Utilise compute()
#158
Comments
I forgot to mention: |
We'll look into implementing this feature ASAP. @JEuler @lejard-h we should look into how Dio does it (thanks @nodinosaur for the readme link!) and implement our own solution. |
It should be possible via class FlutterConverter implements Converter {
Future<Response> decodeJson<BodyType, InnerType>(Response response) {
var contentType = response.headers[contentTypeKey];
var body = response.body;
if (contentType != null && contentType.contains(jsonHeaders)) {
body = utf8.decode(response.bodyBytes);
}
body = _tryDecodeJson(body);
if (isTypeOf<BodyType, Iterable<InnerType>>()) {
body = body.cast<InnerType>();
} else if (isTypeOf<BodyType, Map<String, InnerType>>()) {
body = body.cast<String, InnerType>();
}
return response.copyWith<BodyType>(body: body);
}
///
/// Compute HERE
///
Future<dynamic> _tryDecodeJson(String data) {
try {
return compute(json.decode, data);
} catch (e) {
chopperLogger.warning(e);
return data;
}
}
Future<Response<BodyType>> convertResponse<BodyType, InnerType>(
Response response,
) async {
return decodeJson<BodyType, InnerType>(response);
}
} I partially copied class FlutterConverter extends JsonConverter {
Future<dynamic> tryDecodeJson(String data) {
return compute(tryDecodeJson, data);
}
} |
Did anything ever get merged / what is the way to go if usage of compute is desired? |
I am still hoping that there will be some official documentation with a recommended example on this, I could not get the above example to work 🤔 |
I got it working, I can see isolates being spawned and the data is loaded in our app. Two steps:
|
Hey everybody, Example from @JanKn works well if you have a demanding requests or response once in a while. If your responses are small, using The best solution I found seems to be spawning one Isolate at the app startup and then using it only for converting requests & responses. This can be done using You spawn the late final IsolateRunner isolateRunner;
Future<void> main() async {
isolateRunner = await IsolateRunner.spawn();
runApp(const ExampleApp());
} import 'package:isolate/isolate_runner.dart';
class _IsolateConverter extends Converter {
_IsolateConverter(
this.converter, {
this.isolateRunner,
});
final JsonSerializableConverter converter;
final IsolateRunner? isolateRunner;
bool get runIsolate => isolateRunner != null;
@override
FutureOr<Request> convertRequest(Request request) {
if (runIsolate) {
return isolateRunner!.run(converter.convertRequest, request);
} else {
return converter.convertRequest(request);
}
}
@override
FutureOr<Response<BodyType>> convertResponse<BodyType, InnerType>(
Response response,
) {
if (runIsolate) {
return isolateRunner!.run(
(argument) {
return converter.convertResponse<BodyType, InnerType>(
argument as Response<dynamic>,
);
},
response,
);
} else {
return converter.convertResponse<BodyType, InnerType>(response);
}
}
} My code example falls back to main Isolate when you do not provide custom This There are also few packages for isolate (thread) pooling which allow you to spawn multiple Isolates at startup (say 2 or 4) and then split the work load between them. This seems overkill for my JSON parsing. |
I think we should apply something like |
Any news on this one? As @lejard-h pointed out above, it would be very helpful if all the EDIT: I've started adding these types to a fork here https://github.com/techouse/chopper/tree/futureor-json-converter and will play around with EDIT 2: My preliminary tests using the above fork seem to work fine with and without I'll make a draft PR. |
Thank you so much for your contribution. I will have a look soon! |
Sure, no problem 😊 All the breaking changes are limited to classes extending |
@techouse have you tried calling Apps can easily run many API calls on startup. I would be careful with feature that can have unexpected results. I never really managed to do the benchmark myself. It would be good to start discussion about this. Also, the implementation might have changed since then. |
@vaetas I did. But only something like 5-10. It worked just fine on a real iPhone or Android. The simulator however is a different story. Nonetheless, this PR just wraps the |
@techouse I understand that your PR allows async converters. This will be useful for any Isolate implementation. Thanks! I just wanted to get some discussion going for the next steps. I will try to benchmark |
Hello, @vaetas I tried to play around json converter, but didn't find solution. I have to do concurrent API call and parse json on isolate and return to main thread with parsed data.
|
#352 - now we need to merge this and after that, I will create PR for the master branch which will upgrade the pub.dev version. :) |
It seems that 5.0.0 version is on pub.dev :) |
I must say squadron is amazing and the Flutter example is a good starting point. I've just implemented it using a worker pool in a live app together with Chopper v5.0.0 and it's just amazing. Check out the example here on how to implement it in about 30min. |
I wrote an example using Squadron here. |
With Chopper is there a way that we can utilise
Isolates
/compute()
for loading and parsing large documents?https://flutter.dev/docs/cookbook/networking/background-parsing
https://api.flutter.dev/flutter/dart-isolate/Isolate-class.html
I have seen that some Devs are moving to Dio as it can already do this 👉 README.md, but I am quite invested in Chopper and don't really want to rework my network layer.
Is it possible, or could such a feature be added?
Thanks!
The text was updated successfully, but these errors were encountered: