gpt4 book ai didi

javascript - 有没有办法以我的示例通过 React Native Expo 来执行后台任务?

转载 作者:行者123 更新时间:2023-11-28 03:12:23 28 4
gpt4 key购买 nike

有没有办法以 React Native Expo 为例来执行后台任务?我只想每 5 秒创建一次 post 方法,并使用 console.log 来查看它是否有效这个例子:

this the site of the example

我想改变这个例子,我想尝试使用它来满足我的需要,那么我如何每5秒创建一次post方法以及console.log在后台?

import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';

const FETCH_TASKNAME = 'test_task'
const INTERVAL = 60

function test() {
console.log('function is running')
}

export async function registerFetchTask() {
TaskManager.defineTask(FETCH_TASKNAME, test);

const status = await BackgroundFetch.getStatusAsync();
switch (status) {
case BackgroundFetch.Status.Restricted:
case BackgroundFetch.Status.Denied:
console.log("Background execution is disabled");
return;

default: {
console.debug("Background execution allowed");

let tasks = await TaskManager.getRegisteredTasksAsync();
if (tasks.find(f => f.taskName === FETCH_TASKNAME) == null) {
console.log("Registering task");
await BackgroundFetch.registerTaskAsync(FETCH_TASKNAME);

tasks = await TaskManager.getRegisteredTasksAsync();
console.debug("Registered tasks", tasks);
} else {
console.log(`Task ${FETCH_TASKNAME} already registered, skipping`);
}

console.log("Setting interval to", INTERVAL);
await BackgroundFetch.setMinimumIntervalAsync(INTERVAL);
}
}

}

最佳答案

试试这个 expo-background-fetch图书馆。这正是您在世博会上所需要的。

import * as BackgroundFetch from 'expo-background-fetch';
import * as TaskManager from 'expo-task-manager';

TaskManager.defineTask(YOUR_TASK_NAME, () => {
try {
const receivedNewData = // do your background fetch here
return receivedNewData ? BackgroundFetch.Result.NewData : BackgroundFetch.Result.NoData;
} catch (error) {
return BackgroundFetch.Result.Failed;
}
});

希望对你有帮助

关于javascript - 有没有办法以我的示例通过 React Native Expo 来执行后台任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59981781/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com