All Projects → ferrannp → react-native-sync-adapter

ferrannp / react-native-sync-adapter

Licence: MIT License
Schedule background data synchronization using React Native.

Programming Languages

java
68154 projects - #9 most used programming language
javascript
184084 projects - #8 most used programming language
Starlark
911 projects

Projects that are alternatives of or similar to react-native-sync-adapter

React Native Background Task
Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.
Stars: ✭ 873 (+679.46%)
Mutual labels:  sync, background-jobs
tsdav
WebDAV, CALDAV, and CARDDAV client for Nodejs and the Browser
Stars: ✭ 33 (-70.54%)
Mutual labels:  sync
S4
🔄 Fast and cheap synchronisation of files using Amazon S3
Stars: ✭ 69 (-38.39%)
Mutual labels:  sync
KoHighlights
KOHighlights is a utility for viewing KOReader's highlights and/or export them to simple text, csv or html files.
Stars: ✭ 62 (-44.64%)
Mutual labels:  sync
zenaton-ruby
💎 Ruby gem to run and orchestrate background jobs with Zenaton Workflow Engine
Stars: ✭ 32 (-71.43%)
Mutual labels:  background-jobs
TogetherStream
A social and synchronized streaming experience
Stars: ✭ 16 (-85.71%)
Mutual labels:  sync
dnd-sync
Sync DND state between Android phone and watch
Stars: ✭ 23 (-79.46%)
Mutual labels:  sync
procrastinate
PostgreSQL-based Task Queue for Python
Stars: ✭ 322 (+187.5%)
Mutual labels:  sync
docker base images
Vlad's Base Images for Docker
Stars: ✭ 61 (-45.54%)
Mutual labels:  sync
reddit-pocket-sync
No description or website provided.
Stars: ✭ 37 (-66.96%)
Mutual labels:  sync
hstdb
Better history management for zsh. Based on ideas from https://github.com/larkery/zsh-histdb.
Stars: ✭ 25 (-77.68%)
Mutual labels:  sync
docker-aws-s3-sync
Docker container to sync a folder to Amazon S3
Stars: ✭ 21 (-81.25%)
Mutual labels:  sync
atom-package-sync
Synchronize your atom packages and settings easily
Stars: ✭ 22 (-80.36%)
Mutual labels:  sync
sync-db
Utility to synchronize relational database objects across databases.
Stars: ✭ 15 (-86.61%)
Mutual labels:  sync
tarantool rs
Sync/Async tarantool database connector. WORK IN PROGRESS. DON'T SHARE THIS REPO
Stars: ✭ 14 (-87.5%)
Mutual labels:  sync
email-framework
A simple, gulp powered framework to develop and test responsive emails.
Stars: ✭ 19 (-83.04%)
Mutual labels:  sync
hangfire-dashboard-customize
Customize your Hangfire Dashboard (e.g. Change the Title of the Dashboard)
Stars: ✭ 19 (-83.04%)
Mutual labels:  background-jobs
multihack-brackets
Realtime collaboration for programmers. (Brackets Extension)
Stars: ✭ 24 (-78.57%)
Mutual labels:  sync
Tweet2Telegram
❤️ Liked Tweets to Telegram
Stars: ✭ 83 (-25.89%)
Mutual labels:  sync
git-documentdb
Offline-first Database that Syncs with Git
Stars: ✭ 20 (-82.14%)
Mutual labels:  sync

react-native-sync-adapter

Circle CI npm version

Intelligent Job-Scheduling port to React Native: Scheduling data background synchronizations that run in your JavaScript.

Read a broader introduction in the following post: React Native and Native Modules: The Android SyncAdapter

Requirements

  • React Native 0.60+

Pros

Under the hood, this library uses a SyncAdapter:

  • Android will trigger a sync using our syncFlexTime to decide when is the best moment to do so (battery efficiency)
  • No need to worry about internet connection
  • No need to worry about the user restarting the device
  • Compatible with all Android versions supported by RN (4.1+)

Caveats

This library is only for Android. If you want to do something similar on iOS, I recommend using react-native-background-fetch.

Getting started

yarn add react-native-sync-adapter

Installation

The library will be linked automatically by Autolinking. However, this library requires some manual steps:

Manual Android required step

Open up the string.xml file of your Android project. You need to add the following (just change the content):

<string name="app_name">YourAppName</string>
<string name="rnsb_sync_account_type" translatable="false">your.android.package.name</string>
<string name="rnsb_content_authority" translatable="false">your.android.package.name.provider</string>

This will override the default values from the library and make them unique for your app.

Usage

You need to register a task with a specific name and only with this specific name: TASK_SYNC_ADAPTER. You should do it in the same place where you register your app:

AppRegistry.registerComponent('MyApp', () => MyApp);
AppRegistry.registerHeadlessTask('TASK_SYNC_ADAPTER', () => TestTask);

Then, for example, on your root component:

import {useEffect} from 'react';
import SyncAdapter from 'react-native-sync-adapter';

useEffect(() => {
  SyncAdapter.init({
    syncInterval,
    syncFlexTime,
  });
}, []);

That is all! Some extras:

Timeout

The default timeout for your Headless JS task is 5 minutes (300000ms). If you want to override this value, you will also need to override strings.xml again:

<!-- Overrides default timeout to 10 minutes -->
<string name="rnsb_default_timeout" translatable="false">600000</string>

User visible

By default the user will not be able to manually enable/disable syncs through the Account settings. If you want the user to have this option, you need to override strings.xml:

<!-- Allows the user to enable/disable the sync functionality through the Account settings -->
<string name="rnsb_user_visible" translatable="false">true</string>

Running the task while the app is in the foreground

By default, the sync task will only run if the app is not in the foreground. This is one of the default caveats from HeadlessJS. If you want to override this behavior, you can, one more time overriding strings.xml:

<string name="rnsb_allow_foreground">true</string>

Broadcast Receiver

If you want to trigger a sync natively (e.g. responding to a broadcast receiver), you can call:

SyncAdapter.syncImmediately(Context context, int syncInterval, int syncFlexTime);

API

init

Schedules background syncs within your app.

Object: {
  syncInterval: number;
  syncFlexTime: number;
}
  • syncInterval: The amount of time in seconds that you wish to elapse between periodic syncs
  • syncFlexTime: The amount of flex time in seconds before syncInterval that you permit for the sync to take place. Must be less than syncInterval

A good example could be syncInterval: 12 * 60 * 60 (12 hours) and syncFlexTime: 0.5 * 60 * 60 (30 minutes).

Notice that syncFlexTime only works for Android 4.4+, for older versions, that value will be ignored and syncs will be always exact.

syncImmediately

Invoke the sync task. Use the same values as in the init call.

Object: {
  syncInterval: number;
  syncFlexTime: number;
}

Be aware that for this method to work (if you call it from inside your app) you need to allow the task to work on the foreground.

Running example

You can try this library running the example app:

cd example && yarn && npm start

Then just run:

yarn android

Be careful: The installed example app will trigger a sync around every minute (so it is easy to see that is working). If you debug the app, you should be able to see the HeadlessJS ouputing: Headless JS task was fired! (remember not to have the app on the foreground: Unless you override this behavior). After you try it, I recommend to uninstall the app so you don't harm your device battery life.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].