-1

So I'm trying to sync my database in an isolate, since UI doesn't care about the sync.

Database _backgroundConnection(File dbFile) {
  final database = VmDatabase(dbFile);
  var connection = DatabaseConnection.fromExecutor(database);
  return Database.connect(connection);
}

Dio _getDio(String urlBase, String authToken) {
  var dio = Dio();
  dio.options.baseUrl = urlBase;
  dio.options.headers = {"Authorization": authToken};
  dio.options.connectTimeout = 5000; //5s
  dio.options.receiveTimeout = 3000;
  return dio;
}

Future<void> _syncUser(UserDao dao, DateTime lastSynced, Dio dio) async {
  var users = await dao.getUser();
  if (users.isEmpty) {
    return;
  }
  var user = users[0];
  var userDto = UserDto.fromUser(user);
  //if (user.lastUpdated.isAfter(lastSynced)) {

  var result = await dio.put("/api/v1/user", data: userDto.toJson());
  print(result);
  //}
}

Future<void> _startSync(Map<String, dynamic> info) async {
  String dbPath = info["dbPath"];
  DateTime lastSynced = info["lastSynced"];
  String urlBase = info["url"];
  String token = info["token"];
  var dio = _getDio(urlBase, token);
  File dbFile = File(dbPath);
  var database = _backgroundConnection(dbFile);
  _syncUser(UserDao.fromDatabase(database), lastSynced, dio);
}

class DatabaseSync {
  Future<void> syncDatabase() async {
    final dbFolder = await getApplicationDocumentsDirectory();
    var envVars = GetIt.instance.get<EnvVariables>();
    var authRepo = GetIt.instance.get<FirebaseAuthRepository>();
    compute(_startSync, {
      "lastSynced": DateTime.now(),
      "dbPath": p.join(dbFolder.path, 'db.sqlite'),
      "url": envVars.url,
      "token": await authRepo.getAuthToken(),
    });
  }
} 

I've removed the compute portion, and just ran the code, and it works. It makes the request. However when I put a breakpoint at just before the request, it hits, but when I put it on the print statement, it never hits. Nothing is ever written to the console. This happens on both iOS and android. I tried changing the urlBase from my localhost server to https://google.com to make sure it would hit something https (since iOS gets picky) and still nothing.

Cate Daniel
  • 724
  • 2
  • 14
  • 30

1 Answers1

-1

So this is kinda strange but calling await _syncUser(...) fixed the problem.

Cate Daniel
  • 724
  • 2
  • 14
  • 30