0

Simply moved my live query to an isolate and it no longer works. I want my isolate to wait for a live query object concurrent to a few other simple tasks. I'm getting a pretty vague error message in the debug console and I don't have much to go off of. Really don't have a clue what might be causing this.

Does anyone have any ideas or starting points I could be digging deeper into? This code was working okay before refactoring into isolates... Is there something I'm doing wrong or is it not possible to run a live query in isolate?

EDIT: I've just created a simple dart-only demo project and getting exact same error in the same spot where initializing LiveQuery(). I've swapped code with this demo project because it's simpler and easier to reproduce.

Main isolate

Future<void> main(List<String> arguments) async {
  ...

  await Parse().initialize(
    keyApplicationId,
    keyParseServerUrl,
    clientKey: keyClientKey,
    liveQueryUrl: liveQueryURL,
    connectivityProvider: CustomParseConnectivityProvider(),
    autoSendSessionId: true,
    debug: false,
  );

  await incomingCreate();

  await Isolate.spawn(isolateLiveQuery, 'red');
}

class CustomParseConnectivityProvider extends ParseConnectivityProvider {
  @override
  Future<ParseConnectivityResult> checkConnectivity() async => ParseConnectivityResult.wifi;
  @override
  Stream<ParseConnectivityResult> get connectivityStream => Stream<ParseConnectivityResult>.empty();
}

Isolate

Future<void> isolateLiveQuery(String color) async {
  final LiveQuery liveQuery = LiveQuery();
  QueryBuilder<ParseObject> query = QueryBuilder<ParseObject>(ParseObject('Color'));
  Subscription subscription = await liveQuery.client.subscribe(query);

  List<String> colors = [];
   subscription.on(LiveQueryEvent.create, (value) {
    print('Added: ' + value['color']);
    colors.add(((value as ParseObject).get('color')));
    print(colors.toString());

    sleep(Duration(seconds: 2));

    value.delete();
    
  });
}

Error console

Unhandled exception:
LateInitializationError: Field '_instance@19240915' has not been initialized.
#0      ParseCoreData._instance (package:parse_server_sdk/src/data/parse_core_data.dart)
package:parse_server_sdk/…/data/parse_core_data.dart:1
#1      new ParseCoreData
package:parse_server_sdk/…/data/parse_core_data.dart:5
#2      isDebugEnabled
package:parse_server_sdk/…/utils/parse_utils.dart:8
#3      new LiveQuery
package:parse_server_sdk/…/network/parse_live_query.dart:432
#4      isolateLiveQuery
package:service_realtime_database/service_realtime_database.dart:25
#5      _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:286:17)
#6      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

JustAG33K
  • 1,403
  • 3
  • 13
  • 28
RobbB
  • 1,214
  • 11
  • 39

1 Answers1

0

Got some help from Back4App - awesome customer service as always. They proposed the answer to me and it worked, of course.

Feel pretty dumb I didn't realize what was wrong but hey thats part of learning... I'll leave this and not delete it in case someone else encounters the same issue ;p

Anyway the answer is to initialize Parse inside the isolate. Seeing as the isolate does not have access to Parse.initialize() it must be re-initialized to make network actions.

Simple fix:

Future<void> isolateLiveQuery(String color) async {
  await Parse().initialize(
    keyApplicationId,
    keyParseServerUrl,
    clientKey: keyClientKey,
    liveQueryUrl: liveQueryURL,
    connectivityProvider: CustomParseConnectivityProvider(),
    autoSendSessionId: true,
    debug: false,
  );

  final LiveQuery liveQuery = LiveQuery();
  QueryBuilder<ParseObject> query = QueryBuilder<ParseObject>(ParseObject('Color'));
  Subscription subscription = await liveQuery.client.subscribe(query);

  List<String> colors = [];
   subscription.on(LiveQueryEvent.create, (value) {
    print('Added: ' + value['color']);
    colors.add(((value as ParseObject).get('color')));
    print(colors.toString());

    sleep(Duration(seconds: 2));

    value.delete();
    
  });
}
RobbB
  • 1,214
  • 11
  • 39