一.预加载的作用是在应用安装的状态下提前加载应用的数据到本地缓存,有效的提升应用的首次打开速度,另外预加载服务仅以原始的二进制数据进行缓存,应用使用预加载时不需要修改原有的数据格式,获取缓存后即可进行解析,并且可以对隐私,敏感数据进行加密,预加载是在AppGalleryConnent(简称ACG)平台上完成开通。
二.首先在AGC平台上创建项目并且打开云函数和预加载
三.创建云函数,在函数配置中填写函数名,其余几步选择均为默认,第三步编写发送请求的函数代码,可以让AI帮你写
创建完成之后点击测试
拿到返回结果则是云函数编写完成(我使用的是默认代码,你的返回值结果由你的接口返回数据为准)
在预加载的页面选择你创建的函数并点击保存,至此ACG端配置完成,注意项目包名需要和AGC平台保持一致
四.在项目的EntryAbility中写法如下,注意云函数名称必须和AGC平台上预加载绑定云函数保持一致,在functionPreload函数中的console.log得到接口返回的结果视为成功,注意预加载功能每天只能成功一次,因为有华为白名单,调试的话可以把手机日期往后延一天以上,并且预加载只能在真机上测试,签名必须手动配置,否则预加载会失败
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
import { cloudFunction } from '@kit.CloudFoundationKit';
export default class EntryAbility extends UIAbility {
functionPreload() {
let promise = cloudFunction.call({
name: "prestrain", // 预加载缓存数据的云函数名称
timeout: 3 * 1000, // 获取缓存数据的超时时间
loadMode: cloudFunction.LoadMode.PRELOAD // 获取缓存数据必须设置为PRELOAD
});
promise.then((data: cloudFunction.FunctionResult) => { // 接口调用成功处理缓存的应用数据
hilog.info(0x0000, 'testTag', 'get preload cache successfully');
let getStr = data.result; // data.result即是缓存的应用数据
console.log("结果输出11111"+JSON.stringify(getStr))
}).catch((err: Error) => {
hilog.error(0x0000, 'testTag', 'fail to get preload cache: %{public}s', err.message);
this.functionNormal(); // 使用普通方式获取应用数据
})
}
functionNormal() {
hilog.info(0x0000, 'testTag', 'promise start');
let promise = cloudFunction.call({
name: "function_name",
timeout: 5 * 1000,
loadMode: cloudFunction.LoadMode.NORMAL // 默认为NORMAL, 接口会调用云函数从云服务器获取应用数据
});
promise.then((data: cloudFunction.FunctionResult) => {
hilog.info(0x0000, 'testTag', 'call function successfully');
let getStr = data.result; // data.result即是缓存的应用数据
// todo 处理getStr
}).catch((err: Error) => {
hilog.info(0x0000, 'testTag', 'fail to call function: %{public}s', err.message);
})
}
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
console.log("结果输出")
this.functionPreload(); // 在应用启动时调用函数获取应用数据
}
onDestroy(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
}
}