# react-native-sync-adapter
[![Circle CI](https://circleci.com/gh/ferrannp/react-native-sync-adapter.svg?style=shield)](https://circleci.com/gh/ferrannp/react-native-sync-adapter) [![npm version](https://badge.fury.io/js/react-native-sync-adapter.svg)](https://badge.fury.io/js/react-native-sync-adapter)
[Intelligent Job-Scheduling](https://developer.android.com/topic/performance/scheduling.html) 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](https://blog.callstack.io/react-native-and-native-modules-the-android-syncadapter-517ddf851bf4#.qb5ed9din)
## Requirements
* React Native 0.60+
## Pros
Under the hood, this library uses a [SyncAdapter](https://developer.android.com/reference/android/content/AbstractThreadedSyncAdapter.html):
* 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](https://github.com/transistorsoft/react-native-background-fetch).
## Getting started
```
yarn add react-native-sync-adapter
```
### Installation
The library will be linked automatically by [Autolinking](https://github.com/react-native-community/cli/blob/master/docs/autolinking.md).
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):
```xml
<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](https://facebook.github.io/react-native/docs/headless-js-android.html#the-js-api) 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:
```js
AppRegistry.registerComponent('MyApp', () => MyApp);
AppRegistry.registerHeadlessTask('TASK_SYNC_ADAPTER', () => TestTask);
```
Then, for example, on your root component:
```js
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:
```xml
<!-- 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`:
```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](https://facebook.github.io/react-native/docs/headless-js-android.html#caveats) from HeadlessJS.
If you want to override this behavior, you can, one more time overriding `strings.xml`:
```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:
```java
SyncAdapter.syncImmediately(Context context, int syncInterval, int syncFlexTime);
```
## API
### init
Schedules background syncs within your app.
```js
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](#init) call.
```js
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](https://github.com/ferrannp/react-native-sync-adapter#running-the-task-while-the-app-is-in-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.
没有合适的资源?快使用搜索试试~ 我知道了~
使用ReactNative安排后台数据同步_Java_JavaScript_下载.zip
共70个文件
java:11个
png:10个
js:9个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 2 浏览量
2023-05-02
23:37:53
上传
评论
收藏 379KB ZIP 举报
温馨提示
使用ReactNative安排后台数据同步_Java_JavaScript_下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
使用ReactNative安排后台数据同步_Java_JavaScript_下载.zip (70个子文件)
react-native-sync-adapter-master
yarn.lock 300KB
__tests__
index.js 132B
babel.config.js 77B
.eslintrc.js 73B
.gitattributes 0B
circle.yml 396B
LICENSE 1KB
android
src
main
java
com
fnp
reactnativesyncadapter
SyncAdapterPackage.java 766B
SyncAdapter.java 4KB
HeadlessService.java 2KB
SyncService.java 693B
DummyAuthenticatorService.java 768B
SyncAdapterModule.java 2KB
DummyContentProvider.java 1KB
DummyAuthenticator.java 2KB
res
xml
authenticator.xml 303B
sync_adapter.xml 402B
values
strings.xml 485B
AndroidManifest.xml 2KB
build.gradle 5KB
README.md 662B
.flowconfig 2KB
.prettierrc.js 120B
package.json 2KB
.npmignore 8B
index.js 751B
.gitignore 196B
.eslintignore 57B
example
.watchmanconfig 2B
.buckconfig 114B
yarn.lock 262KB
.flowconfigDELETE 2KB
babel.config.js 77B
app.json 74B
metro.config.js 853B
.gitattributes 16B
android
gradle.properties 1KB
gradle
wrapper
gradle-wrapper.jar 54KB
gradle-wrapper.properties 202B
app
build_defs.bzl 602B
src
debug
java
com
example
ReactNativeFlipper.java 3KB
AndroidManifest.xml 368B
main
java
com
fnp
syncadapterexample
MainApplication.java 3KB
MainActivity.java 367B
res
mipmap-xxhdpi
ic_launcher_round.png 10KB
ic_launcher.png 6KB
mipmap-hdpi
ic_launcher_round.png 5KB
ic_launcher.png 3KB
mipmap-mdpi
ic_launcher_round.png 3KB
ic_launcher.png 2KB
mipmap-xxxhdpi
ic_launcher_round.png 15KB
ic_launcher.png 9KB
mipmap-xhdpi
ic_launcher_round.png 7KB
ic_launcher.png 4KB
values
strings.xml 288B
styles.xml 246B
AndroidManifest.xml 1KB
proguard-rules.pro 435B
build.gradle 9KB
debug.keystore 2KB
BUCK 1KB
gradlew.bat 3KB
build.gradle 1003B
settings.gradle 191B
gradlew 6KB
TestTask.js 125B
package.json 649B
index.js 1KB
.gitignore 813B
README.md 6KB
共 70 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9156
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- #P0015. 全排列 超级简单
- pta题库答案c语言之排序4统计工龄.zip
- pta题库答案c语言之树结构7堆中的路径.zip
- pta题库答案c语言之树结构3TreeTraversalsAgain.zip
- pta题库答案c语言之树结构2ListLeaves.zip
- pta题库答案c语言之树结构1树的同构.zip
- 基于C++实现民航飞行与地图简易管理系统可执行程序+说明+详细注释.zip
- pta题库答案c语言之复杂度1最大子列和问题.zip
- 三维装箱问题(Three-Dimensional Bin Packing Problem,3D-BPP)是一个经典的组合优化问题
- 以下是一些关于Linux线程同步的基本概念和方法.txt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功