gpt4 book ai didi

ios - expo.io 的 BackgroundFetch 没有运行定义的任务

转载 作者:行者123 更新时间:2023-12-05 00:22:26 30 4
gpt4 key购买 nike

我目前正在使用 为 iOS 开发一个应用程序世博 SDK 版本 32 .我必须定期从我的应用程序调用 REST 服务。为此,我尝试使用新的 BackgroundFetch API。

这是我的代码:

const BACKGROUND_LOCATION_SENDING_TASK = "BACKGROUND_LOCATION_SENDING_TASK";

async function initBackgroundLocationSending() {
console.log("initBackgroundLocationSending()");

TaskManager.defineTask(BACKGROUND_LOCATION_SENDING_TASK, () => {

// this console.log does never get called ...
console.log("BACKGROUND_LOCATION_SENDING_TASK");

// i will implement real fetch logic here when i found out how to get this function called.
return BackgroundFetch.Result.NewData;
});

console.log("is task registered ... ");
let isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_LOCATION_SENDING_TASK);
console.log("isRegistered: ", isRegistered);

if(isRegistered) {
console.log("unregister task ...");
await BackgroundFetch.unregisterTaskAsync(BACKGROUND_LOCATION_SENDING_TASK);
console.log("Done");
}

console.log("is task registered ... ");
isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_LOCATION_SENDING_TASK);
console.log("isRegistered: ", isRegistered);

console.log("register task ...");
await BackgroundFetch.registerTaskAsync(BACKGROUND_LOCATION_SENDING_TASK);
console.log("OK");


console.log("is task registered ... ");
isRegistered = await TaskManager.isTaskRegisteredAsync(BACKGROUND_LOCATION_SENDING_TASK);
console.log("isRegistered: ", isRegistered);

console.log("set minimum interval ...");
await BackgroundFetch.setMinimumIntervalAsync(60);
console.log("OK");

console.log("get status ... ");
const status = await BackgroundFetch.getStatusAsync();
console.log("status: ", status);
}

中调用函数的控制台输出应用.js :
: initBackgroundLocationSending()
: is task registered ...
: isRegistered: true
: unregister task ...
: OK
: is task registered ...
: isRegistered: false
: register task ...
: OK
: is task registered ...
: isRegistered: true
: set minimum interval ...
: OK
: get status ...
: status: 3

来自 expo's background-fetch documentation我了解 状态:3 表示 BackgroundFetch.Status.Available .

iOS 我的 中的部分app.json :
...
"ios": {
"supportsTablet": true,
"bundleIdentifier": "xxx-yyy-zzz",
"infoPlist": {
"UIBackgroundModes": [
"location",
"fetch"
],
"NSLocationWhenInUseUsageDescription": "... (replaced)",
"NSLocationAlwaysAndWhenInUseUsageDescription": "... (replaced)",
"NSLocationAlwaysUsageDescription": "... (replaced)"
}
},
...

我的测试设备是带有 iOS 12.1.3 的 iPhone 6s。

我错过了什么?为什么即使几分钟后它也根本不运行任务?

顺便说一句:我使用 expo's background location没有任何问题

最佳答案

正如文档所述,我会尝试将任务定义移动到全局范围,因为您当前设置它的方式是在函数范围内定义的

  1. Define the task by providing a name and the function that should be executed Note: This needs to be called in the global scope (e.goutside of your React components)

意思是这个
const BACKGROUND_LOCATION_SENDING_TASK = "BACKGROUND_LOCATION_SENDING_TASK";

async function initBackgroundLocationSending() {
console.log("initBackgroundLocationSending()");

TaskManager.defineTask(BACKGROUND_LOCATION_SENDING_TASK, () => {

// this console.log does never get called ...
console.log("BACKGROUND_LOCATION_SENDING_TASK");

// i will implement real fetch logic here when i found out how to get this function called.
return BackgroundFetch.Result.NewData;
});

....

}
应该改成这个
const BACKGROUND_LOCATION_SENDING_TASK = "BACKGROUND_LOCATION_SENDING_TASK";

TaskManager.defineTask(BACKGROUND_LOCATION_SENDING_TASK, () => {

// this console.log does never get called ...
console.log("BACKGROUND_LOCATION_SENDING_TASK");

// i will implement real fetch logic here when i found out how to get this function called.
return BackgroundFetch.Result.NewData;
});


async function initBackgroundLocationSending() {
console.log("initBackgroundLocationSending()");

...

}
initBackgroundLocationSending 的其余部分处理任务注册的应该在组件挂载时调用

关于ios - expo.io 的 BackgroundFetch 没有运行定义的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54732688/

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