BackgroundUseCase<T, Params> class
abstract
A specialized type of UseCase that executes on a different isolate. The purpose is identical to UseCase, except that this runs on a different isolate. A BackgroundUseCase is useful when performing expensive operations that ideally should not be performed on the main isolate.
The code that is to be run on a different isolate can be provided through a
static method of the usecase. Then, a reference of that method should be returned
by overriding the buildUseCaseTask as shown below. Input data for the isolate
is provided by inside the params
variable in BackgroundUseCaseParams, which is
passed to the static method of type UseCaseTask.
Output data can be passed back to the main isolate through port.send()
provided
in the port
member of BackgroundUseCaseParams. Any and all output should be
wrapped inside a BackgroundUseCaseMessage. Data can be passed by specifying the
data
parameter, while errors can be reported through the error
parameter.
In addition, a done
flag can be set to indicate that the isolate has completed its task.
An example would be a usecase that performs matrix multiplication.
class MatMulUseCase extends BackgroundUseCase<List<List<double>>, MatMulUseCaseParams> {
@override
buildUseCaseTask() {
return matmul;
}
static void matmul(BackgroundUseCaseParams params) async {
MatMulUseCaseParams matMulParams = params.params as MatMulUseCaseParams;
List<List<double>> result = List<List<double>>.generate(
10, (i) => List<double>.generate(10, (j) => 0));
for (int i = 0; i < matMulParams.mat1.length; i++) {
for (int j = 0; j < matMulParams.mat1.length; j++) {
for (int k = 0; k < matMulParams.mat1.length; k++) {
result[i][j] += matMulParams.mat1[i][k] * matMulParams.mat2[k][j];
}
}
}
params.port.send(BackgroundUseCaseMessage(data: result));
}
}
Just like a regular UseCase, a parameter class is recommended for any BackgroundUseCase. An example corresponding to the above example would be
class MatMulUseCaseParams {
List<List<double>> mat1;
List<List<double>> mat2;
MatMulUseCaseParams(this.mat1, this.mat2);
MatMulUseCaseParams.random() {
var size = 10;
mat1 = List<List<double>>.generate(size,
(i) => List<double>.generate(size, (j) => i.toDouble() * size + j));
mat2 = List<List<double>>.generate(size,
(i) => List<double>.generate(size, (j) => i.toDouble() * size + j));
}
}
Constructors
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- isRunning → bool
-
no setter
- logger → Logger
-
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- state → BackgroundUseCaseState
-
no setter
Methods
-
buildUseCaseStream(
Params? params) → Future< Stream< T?> > -
Builds the
Stream
to be subscribed to.Params
is required by the UseCase to retrieve the appropraite data from the repositoryoverride -
buildUseCaseTask(
) → UseCaseTask - Provides a UseCaseTask to be executed on a different isolate. Must be overridden.
-
dispose(
) → void -
Disposes (unsubscribes) from the Stream
override
-
execute(
Observer< T> observer, [Params? params]) → void -
Executes the usecase on a different isolate. Spawns
_isolate
using the static method provided by buildUseCaseTask and listens to aBehaviorSubject
using theobserver
provided by the user. AllParams
are sent to the_isolate
through BackgroundUseCaseParams.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited