偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

聊一聊OpenHarmony啟動頁后記

系統(tǒng) OpenHarmony
通過使用云數(shù)據(jù)庫、云存儲相結合的方式使應用的啟動頁能夠動態(tài)化,即可以在不更新應用的情況下更改啟動頁的參數(shù)已達到啟動頁的動態(tài)化。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區(qū)??

??https://ost.51cto.com??

1、回顧

通過DevEco Studio端云協(xié)同開發(fā)OpenHarmony/HarmonyOS應用程序(以下簡稱應用)集成AppGallery Connect(以下簡稱AGC)平臺??云函數(shù)???、??云數(shù)據(jù)庫???、??云存儲??三篇文章筆者從創(chuàng)建端云協(xié)同應用程序開始,逐步對云函數(shù)、云數(shù)據(jù)庫、云存儲簡單的數(shù)據(jù)讀取做了簡單的介紹。通過使用云數(shù)據(jù)庫、云存儲相結合的方式使應用的啟動頁能夠動態(tài)化,即可以在不更新應用的情況下更改啟動頁的參數(shù)已達到啟動頁的動態(tài)化。

2、問題及解決方案

問題: 由于啟動頁參數(shù)來源于云數(shù)據(jù)庫、云存儲,啟動頁數(shù)據(jù)渲染前會受網(wǎng)絡影響出現(xiàn)白屏。

解決方案: 為啟動頁數(shù)據(jù)單獨封裝獲取方法,在啟動頁新增狀態(tài)值,數(shù)據(jù)未加載完成后顯示當前應用的??icon??圖標,數(shù)據(jù)加載完成后渲染實際獲取到的數(shù)據(jù)。

注: 若讀者有其他的處理方法可與筆者共同探討一下。

3、優(yōu)化調用方法

使用async將函數(shù)異步化,使用await獲取Promise的值。

(1)云數(shù)據(jù)庫獲取數(shù)據(jù)方法異步化

每次使用存儲區(qū)都要在使用完成后釋放,新增關閉存儲區(qū)方法。

// service/CloudDBService.ts
// @ts-ignore
import * as schema from './app-schema.json';
import { splash } from './splash';
import {
AGConnectCloudDB,
CloudDBZoneConfig,
CloudDBZone,
CloudDBZoneQuery
} from '@hw-agconnect/database-ohos';

import { AGCRoutePolicy } from '@hw-agconnect/core-ohos';

import { getAGConnect } from './AgcConfig';

export class CloudDBService {

private static readonly ZONE_NAME = "cloudDBZoneSplash";
private static cloudDB: AGConnectCloudDB;
private static cloudDBZone: CloudDBZone;
private static isInit: boolean;

public static async init(context: any): Promise<boolean> {
if (this.isInit) {
return;
}
try {
// 初始化agc
getAGConnect(context);
// 初始化Cloud DB
await AGConnectCloudDB.initialize(context);
// 獲取對應數(shù)據(jù)處理位置的CloudDB實例
this.cloudDB = await AGConnectCloudDB.getInstance(AGCRoutePolicy.CHINA);
// 創(chuàng)建對象類型
this.cloudDB.createObjectType(schema);
// 打開存儲區(qū)
await this.openZone(this.ZONE_NAME);
this.isInit = true;
} catch (err) {
console.error(JSON.stringify(err))
}
return Promise.resolve(this.isInit);
}

// 打開存儲區(qū)
private static async openZone(zoneName: string): Promise<CloudDBZone> {
if (this.cloudDBZone) {
return;
}
try {
const cloudDBZoneConfig = new CloudDBZoneConfig(zoneName);
this.cloudDBZone = await this.cloudDB.openCloudDBZone(cloudDBZoneConfig);
} catch (err) {
console.error(JSON.stringify(err));
}
}

// 關閉存儲區(qū)
public static async closeZone(): Promise<void> {
try {
this.cloudDB.closeCloudDBZone(this.cloudDBZone);
this.cloudDBZone = null;
} catch (err) {
console.error(JSON.stringify(err))
}
}

public static async query(): Promise<splash> {
try {
const query = CloudDBZoneQuery.where(splash).equalTo("status", 1);
const result = await this.cloudDBZone.executeQuery(query);
return result.getSnapshotObjects().length > 0 ? result.getSnapshotObjects()[0] : new splash();
} catch (err) {
console.error(JSON.stringify(err));
}
}
}

(2)云存儲獲取數(shù)據(jù)方法異步化

// services/cloudstorage/CloudStorageService.ts
import agconnect from '@hw-agconnect/api-ohos';
import "@hw-agconnect/cloudstorage-ohos";

import { getAGConnect } from '../AgcConfig';

export class CloudStorageService {

public static async init(context: any, path: string): Promise<string> {
try {
getAGConnect(context);
// 初始化默認實例
const storage = agconnect.cloudStorage();
// 創(chuàng)建需要下載文件的引用
const storageReference = await storage.storageReference();
var reference = await storageReference.child(path);
return reference.getDownloadURL();
} catch (err) {
console.error(JSON.stringify(err));
}
}
}

4、為啟動頁數(shù)據(jù)獲取封裝專用方法

可以將一些處理邏輯放在該方法中處理。

// services/SplashService.ts
import { splash } from './splash';
import { CloudDBService } from '../services/CloudDBService';
import { CloudStorageService } from '../services/cloudstorage/CloudStorageService';

export class SplashService {

public static async querySplash(context: any): Promise<splash> {
try {
await CloudDBService.init(context);
let splash = await CloudDBService.query();
let url = await CloudStorageService.init(context, splash.backgroundImg);
splash.backgroundImg = url;
await CloudDBService.closeZone();
return splash;
} catch (err) {
console.error(JSON.stringify(err));
}
}
}

5、改寫啟動頁

啟動頁新增狀態(tài)碼,用于數(shù)據(jù)未加載完成呈現(xiàn)給用戶的顯示界面,規(guī)避數(shù)據(jù)未獲取導致的白屏現(xiàn)象。

@State isSkip: boolean = false;

在aboutToAppear()方法中執(zhí)行獲取啟動頁數(shù)據(jù)的方法。

aboutToAppear() {
this.isSkip = false;
SplashService.querySplash(getContext(this)).then((ret) => {
this.isSkip = true;
this.result = ret;
})
}

頁面中使用if(){}else{}條件語句判斷渲染的組件,從而規(guī)避數(shù)據(jù)請求時間導致的白屏。

if (this.isSkip) {
SplashPage({ mSplash: {
timer: this.result.timer,
isLogo: this.result.isLogo,
backgroundImg: this.result.backgroundImg,
companyName: this.result.companyName,
mFontColor: this.result.mFontColor
}, skip: this.onSkip })
} else {
Column() {
Image($r('app.media.icon')).objectFit(ImageFit.None)
}
.width('100%').height('100%')
}

通過更改AGC平臺云數(shù)據(jù)庫中啟動頁數(shù)據(jù)狀態(tài),可以實現(xiàn)下次啟動應用程序,啟動頁呈現(xiàn)不同內容。使用場景如新聞類App可以在啟動頁呈現(xiàn)一條配備圖片的熱文;常規(guī)App可以在啟動頁呈現(xiàn)一條經(jīng)典語錄;實現(xiàn)不同節(jié)日在啟動頁呈現(xiàn)問候信息。

6、后記

本文所記為之前文章的總結,針對獲取AGC平臺各項服務的數(shù)據(jù),可直接調用對應的方法即可。若出現(xiàn)復雜的情況,如后面筆者將實現(xiàn)認證服務登錄,并將用戶信息存儲到云數(shù)據(jù)庫中,可以結合云函數(shù),在用戶登錄的時候,直接調用云函數(shù)去保存用戶信息,存儲方法可以通過云函數(shù)的AUTH觸發(fā)器實現(xiàn)數(shù)據(jù)存儲云數(shù)據(jù)庫中;再如用戶上傳圖片,生成縮略圖,也可以利用云函數(shù)將原圖和縮略圖一同保存到云存儲中。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區(qū)??

??https://ost.51cto.com??

責任編輯:jianghua 來源: 51CTO 開源基礎軟件社區(qū)
相關推薦

2023-01-01 13:53:55

跨頁cmp

2023-09-22 17:36:37

2020-05-22 08:16:07

PONGPONXG-PON

2021-01-28 22:31:33

分組密碼算法

2018-06-07 13:17:12

契約測試單元測試API測試

2021-08-04 09:32:05

Typescript 技巧Partial

2020-10-15 06:56:51

MySQL排序

2019-02-13 14:15:59

Linux版本Fedora

2022-08-08 08:25:21

Javajar 文件

2022-11-01 08:46:20

責任鏈模式對象

2023-05-15 08:38:58

模板方法模式

2021-01-29 08:32:21

數(shù)據(jù)結構數(shù)組

2021-02-06 08:34:49

函數(shù)memoize文檔

2023-07-06 13:56:14

微軟Skype

2018-11-29 09:13:47

CPU中斷控制器

2019-12-17 10:06:18

CDMA高通4G

2022-03-08 16:10:38

Redis事務機制

2020-09-08 06:54:29

Java Gradle語言

2022-03-29 09:56:21

游戲版本運營

2021-01-01 09:01:05

前端組件化設計
點贊
收藏

51CTO技術棧公眾號