Skip to content

Conversation

@avelino
Copy link
Contributor

@avelino avelino commented Sep 30, 2025

  • Introduce frontend.mobile.sync-background namespace to manage cloud sync when the mobile app is backgrounded.
  • Track app state, network connectivity, and background sync status using atoms.
  • Provide functions to handle app state and network changes.
  • Ensure sync only triggers when app is backgrounded, network is connected, and sync is enabled.
  • Integrate with storage and state modules for sync control.

This enables reliable background syncing for mobile users, improving data consistency and user experience.

ref: https://discuss.logseq.com/t/logseq-mobile-sync-in-background/17214

- Introduce `frontend.mobile.sync-background` namespace to manage cloud sync when the mobile app is backgrounded.
- Track app state, network connectivity, and background sync status using atoms.
- Provide functions to handle app state and network changes.
- Ensure sync only triggers when app is backgrounded, network is connected, and sync is enabled.
- Integrate with storage and state modules for sync control.

This enables reliable background syncing for mobile users, improving data consistency and user experience.

ref: https://discuss.logseq.com/t/logseq-mobile-sync-in-background/17214

Signed-off-by: Avelino <31996+avelino@users.noreply.github.com>
…emSynchronizedGroupsEnabled to true in the preferences.

Signed-off-by: Avelino <31996+avelino@users.noreply.github.com>
@avelino avelino changed the title feat(mobile): add background cloud sync for mobile app WIP: feat(mobile): add background cloud sync for mobile app Sep 30, 2025
- Added iOS background task registration and scheduling in AppDelegate.swift using BGTaskScheduler.
- Integrated background sync trigger via JavaScript bridge to webview.
- Ensured background sync is scheduled on app launch and when entering background.
- Updated Info.plist to support background tasks and required capabilities.
- Exposed background sync trigger to JS global in sync_background.cljs for iOS bridge.
- This enables Logseq mobile to sync notes in the background, improving reliability and user experience.

Signed-off-by: Avelino <31996+avelino@users.noreply.github.com>
- Add RECEIVE_BOOT_COMPLETED permission to AndroidManifest.xml
- Add MobileSyncService and MobileSyncBootReceiver for background sync
- Update MainActivity to support background sync triggers
- Enables background sync to run periodically and after device boot

Signed-off-by: Avelino <31996+avelino@users.noreply.github.com>
@avelino avelino changed the title WIP: feat(mobile): add background cloud sync for mobile app feat(mobile): add background cloud sync for mobile app Sep 30, 2025
@avelino
Copy link
Contributor Author

avelino commented Sep 30, 2025

@tiensonqin and @logseq-cldwalker see if this implementation makes sense for logseq's future

@vmesel
Copy link

vmesel commented Oct 2, 2025

it will make my life better on the mobile app

@vmesel
Copy link

vmesel commented Oct 7, 2025

@logseq-cldwalker @tiensonqin any updates here?

(when (should-trigger?)
(mark-running! true)
(let [start-ts (.now js/Date)]
(-> (p/let [_ (fs-sync/<sync-start)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The master branch doesn't support file graphs sync on mobile anymore, to start rtc sync, you can call frontend.handler.db-based.rtc/<rtc-start!.

Copy link
Contributor

@tiensonqin tiensonqin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply — I wanted to review it thoroughly after some testing, since background sync is critical and I really hope it’s resolved. Unfortunately, I haven’t had time this month because users have been running into bugs like invalid data that prevents them from using Logseq DB (we made some changes to enforce schema validations). So I’ll leave some notes for now.

Overall, the structure looks good! Some parts of the code seem AI-generated 😄

No worries though — we’re not against “vibe coding.” We also use AI quite a bit to generate native code (especially Swift).

We’ve done some research on background sync, and it seems the only truly reliable approach is native sync triggered by push notifications for local refreshes. However, that approach would take significant effort, so we’ve paused it for now.

As far as I know, BGAppRefreshTaskRequest has a hard 30s limit, while BGProcessingTaskRequest runs for around 5–10 minutes. That might actually be enough for our use case, since the DB version only syncs deltas instead of full markdown files.

I’ll run more tests this week.

print("⚠️ Bridge not ready for background sync")
return
}
bridge.webView?.evaluateJavaScript("window.logseqMobile?.backgroundSync?.trigger?.()") { result, error in
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can ensure “sync work is done” when there's no error, is that right?

The web view could be suspended, reloaded or crashed in background, I have no idea how BGTaskScheduler works and how long it can run in background, I'll give it a try later with rtc db graphs.

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

Successfully merging this pull request may close these issues.

3 participants