Skip to content

Pass parameters to the callback function #11

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

Closed
darshansatra1 opened this issue Jul 30, 2021 · 9 comments
Closed

Pass parameters to the callback function #11

darshansatra1 opened this issue Jul 30, 2021 · 9 comments

Comments

@darshansatra1
Copy link

Future<void> startPeriodicTask({
  required int steps,
  required int yesterdaySteps,
}) async {
  await FlutterForegroundTask.start(
      notificationTitle: "Notification", 
      callback: periodicTaskFun,
);
}

Right now I'm using this code to register the callback function, but can I pass parameters like String or Int to periodicTaskFun? If not please can you add such a feature?

@Dev-hwang
Copy link
Owner

Since the periodicTaskFun function is a top-level function, parameters cannot be passed directly. Use a mobile database like sqlite.

This is an example using sqflite. #9 (comment)

@darshansatra1
Copy link
Author

Can you please go through this Object Box Issue. I have been trying to get the same instance of Store but as I'm making an instance from 2 different isolates the HashCodes are not the same because of which I'm not able to access the same store.

@Dev-hwang
Copy link
Owner

I've checked the requirements, but there doesn't seem to be a way right now. I'll keep thinking about how to pass a SendPort or parameters.

@darshansatra1
Copy link
Author

Yes, it would be of great help if we can send some parameters in the callback function or can communicate with the main isolate.

@darshansatra1
Copy link
Author


Future<void> startPeriodicTask({
  required int steps,
  required int yesterdaySteps,
  required Store store,
}) async {
  var port = ReceivePort();
  IsolateNameServer.registerPortWithName(port.sendPort, mainIsolatePortName);
  await FlutterForegroundTask.start(
    notificationTitle: "Today: $steps steps",
    notificationText: 'Yesterday: $yesterdaySteps steps',
    callback: periodicTaskFun,
  );
  final taskPort = (await port.first) as SendPort;
  taskPort.send(store.reference);
  IsolateNameServer.removePortNameMapping(mainIsolatePortName);
}


void periodicTaskFun() async {
  Stream<StepCount> _stepCountStream = Pedometer.stepCountStream;
  StreamSubscription<StepCount>? streamSubscription;
  final port = ReceivePort();
  IsolateNameServer.lookupPortByName(mainIsolatePortName)!.send(port.sendPort);
  final storeRef = (await port.first) as ByteData;
  LocalDataSource localDataSource = LocalDataSourceImpl();
  await localDataSource.initializeForForegroundService(reference: storeRef);
  FlutterForegroundTask.initDispatcher((timeStamp) async {
    if (streamSubscription != null) {
      return;
    }...

So this was the solution I got from Internet and it's working now. But when I kill the app and reopen it, It's throwing me an error.

Error:

 Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: step_count. Response ID: 0

Do you have any idea why this is happening?

@Dev-hwang
Copy link
Owner

The above problem occurs if the cancel function of streamSubscription is not called. If the problem persists despite writing the code, check if the onDestroy in the initDispatcher function is working properly.

@darshansatra1
Copy link
Author

So I checked the onDestroy function and it is working. But this error actually occurs when in the following steps:

  1. I run the foreground service.
  2. I kill the app.
  3. And I walk to see whether the stream is updating or not.
  4. It is updating but at the same time, it is throwing me the error.

Is this supposed to happen?

@Dev-hwang
Copy link
Owner

objectbox/objectbox-dart@e61ea42

I tested with the above file and there was no problem.
It may be a BroadcastStream problem, so can you try adding onCancel as below?

final _stepCountStream =
    Stream<StepCount?>.periodic(const Duration(seconds: 10))
        .asBroadcastStream(onCancel: (subscription) => subscription.cancel());

@darshansatra1
Copy link
Author

I tried this but nothing changed. I think there is some issue with flutter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants