public async Task SyncAsync() { bool lockTaken = false; try { lockTaken = await _semaphore.WaitAsync(0); if (lockTaken) { string userId = Utilities.GetUserId(); var lastSyncedTicks = Utilities.GetLongFromPreferences(Application.Context, $"{userId}_LastSyncedOn"); var newTasksFromServer = await _syncHelper.PullAsync(lastSyncedTicks); var newTasksInClient = await _dataHelper.GetAllFromDateTimeAsync(userId, lastSyncedTicks); if (newTasksInClient != null && newTasksInClient.Count > 0) { await _syncHelper.PushAsync(newTasksInClient); } if (newTasksFromServer != null && newTasksFromServer.Any()) { await UpdateTasksAsync(newTasksFromServer); // Invoke event NewTasksAvailable?.Invoke(this, new EventArgs()); } Utilities.SaveLongToPreferences(Application.Context, $"{userId}_LastSyncedOn", DateTime.UtcNow.Ticks); } } catch (Exception ex) { Log.WriteLine(LogPriority.Error, "Planner Error", ex.Message); } finally { if (lockTaken) { _semaphore.Release(); } } }