0 x00 preface

This article is about another way to create an ISOLATE.

0x01 Use of debilitating methods

Methods of use:

  1. Advanced API: Compute function (easy to use)
  2. Low-level API: ReceivePort

0 x02 Compute function

The Compute function encapsulates the creation of the ISOLATE and the underlying messaging so that we don’t have to worry about the underlying implementation, just the functional implementation.

First we need:

  1. A function: must be a top-level function or a static function
  2. A parameter: This parameter is the input parameter of the above function definition (the function has no parameters).

For example, calculate Fibonacci numbers again:

Void main() async{// call compute. The parameters of compute are the functions that you want to run on the ISOLATE and the parameters that the function needsprint( await compute(syncFibonacci, 20));
  runApp(MyApp());
}

int syncFibonacci(int n){
  return n < 2 ? n : syncFibonacci(n-2) + syncFibonacci(n-1);
}
Copy the code

The result after running is as follows:

flutter: 6765
Copy the code

Compute () {compute (); compute (); compute ();

ComputeCallback

is defined as follows:
,>

// typepedef ComputeCallback<Q, R> = R Function(Q message); // typepedef ComputeCallback<Q, R> = R Function(Q message);Copy the code

Official look at the source code:

// Compute takes two parameters, Future<R> compute<Q, R> ComputeCallback<Q, R> callback, Q message, {String debugLabel}) async {// Callback. ToString () profile() {debugLabel?? = callback.toString(); }); final Flow flow = Flow.begin(); Timeline.startSync('$debugLabel: start', flow: flow); final ReceivePort resultPort = ReceivePort(); Timeline.finishSync(); // Create the ISOLATE. This is the same as the previous method of creating the ISOLATE. Final Isolate Isolate = await ISOLateconfiguration <_IsolateConfiguration<Q, R>>(_spawn, _IsolateConfiguration<Q, R>( callback, message, resultPort.sendPort, debugLabel, flow.id, ), errorsAreFatal:true,
    onExit: resultPort.sendPort,
  );
  final R result = await resultPort.first;
  Timeline.startSync('$debugLabel: end', flow: Flow.end(flow.id));
  resultPort.close();
  isolate.kill();
  Timeline.finishSync();
  return result;
}

@immutable
class _IsolateConfiguration<Q, R> {
  const _IsolateConfiguration(
    this.callback,
    this.message,
    this.resultPort,
    this.debugLabel,
    this.flowId,
  );
  final ComputeCallback<Q, R> callback;
  final Q message;
  final SendPort resultPort;
  final String debugLabel;
  final int flowId;

  R apply() => callback(message);
}

void _spawn<Q, R>(_IsolateConfiguration<Q, R> configuration) {
  R result;
  Timeline.timeSync(
    '${configuration.debugLabel}',
    () {
      result = configuration.apply();
    },
    flow: Flow.step(configuration.flowId),
  );
  Timeline.timeSync(
    '${configuration.debugLabel}: returning result',
    () { configuration.resultPort.send(result); },
    flow: Flow.step(configuration.flowId),
  );
}

Copy the code

0x03 ReceivePort

import 'dart:async';
import 'dart:io';
import 'dart:isolate';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; Void main() async{runApp(MyApp()); void main() async{runApp(MyApp()); //asyncFibonacci creates a ISOLATE and returns the resultprint(await asyncFibonacci(20)); Future<dynamic> asyncFibonacci(int n) async{// Create a ReceivePort first. SendPort needs ReceivePort to create final Response = new ReceivePort(); Spawn function is the code in the ISOLate. dart function. _ISOLATE is a function we implemented ourselves. await Isolate.spawn(_isolate,response.sendPort); // Get sendPort to send data final sendPort = await response.first as sendPort; ReceivePort final answer = new ReceivePort(); Sendport.send ([n, answer.sendport]); // Get data and returnreturnanswer.first; Void _ISOLATE (SendPort initialReplyTo){final port = new ReceivePort(); / / bind initialReplyTo. Send (port. SendPort); // Listen to port.listen((message){final data = message[0] as int; final send = message[1] as SendPort; Send (syncFibonacci(data)); }); } int syncFibonacci(int n){return n < 2 ? n : syncFibonacci(n-2) + syncFibonacci(n-1);
}
Copy the code