最近完成了一个鸿蒙app项目,”弋木单词助手“ 一个帮助中学生学习单词的免费软件。
首页:
用到几大功能:
1: 云函数
2: 云数据库直连
3: 第三方sdk :
"@mcui/mccharts": "^1.0.2", "cjcalendar": "^1.1.6"
开发整个app总的来说还是顺利的,鸿蒙系统的发展我还是很看好的:
ArkTS,声明式UI、状态管理等这些优点用起来很顺滑。简洁。开发效率较高。
不足的是真机调试比较慢,希望在下一个版本可以提高。
主要说说云开发中,遇到的云函数连接问题。经常出现重启app,通过云函数获取数据失败,返回一个错误码17630001,我不知道是什么意思。。通过多次测试, 发现和打包的版本有关, 某次打包是正常的话,每次重启app依然是正常的,读取数据不会有问题。。如果异常,那么安装的这个版本每次重启app都会是通过云函数读取数据失败,但是通过直连云数据读取数据正常。 我的内页面基本都是直连云数据库的。 所以我经常打包若干次直到遇到没有问题的包。 但是这样上架的就比较麻烦了,现在已经上架到华为应用市场了。 目前这个问题依然存在,不知道有没有小伙伴遇到我这样的问题,欢迎指教。
我本来想全部直接连接云数据库来获取数据, 但是没有发现直连的方式可以获取到满足搜索条件的数据条数。
贴上云函数主要代码:
EntryAbility.ets
export default class EntryAbility extends UIAbility {
async onCreate() {
try {
let value = await this.context.resourceManager.getRawFileContent('agconnect-services.json')
let jsonString = util.TextDecoder.create('utf-8', {
ignoreBOM: true
}).decodeWithStream(value, {
stream: false
});
initialize(this.context, JSON.parse(jsonString));
CloudDb.initDB() //直接连接数据
}catch (err){
hilog.info(0x0000,'agconnect初始化失败',JSON.stringify(err))
}
.....
}
index.ets:
import agconnect from '@hw-agconnect/api-ohos';
@Entry
@Component
struct Index {
...
async aboutToAppear(){
//初始化
agconnect.instance().init(getContext(this))
}
}
云函数部分:
const clouddb = require('@hw-agconnect/database-server/dist/index.js');
const agconnect = require('@hw-agconnect/common-server');
const path = require('path');
const credentialPath = "./resources/agc-apiclient-1408623347549382016-7367206308096351252.json";
// 修改为在管理台创建的存储区名称
let zoneName = "learn"
// 修改为需要操作的对象
let objectName = "t_vocabulary";
let logger
let mCloudDBZone
class CloudDBZoneWrapper {
// AGC & 数据库初始化
constructor(log) {
logger = log;
let agcClient;
try {
agcClient = agconnect.AGCClient.getInstance();
} catch (error) {
agconnect.AGCClient.initialize(agconnect.CredentialParser.toCredential(path.join(__dirname, credentialPath)));
agcClient = agconnect.AGCClient.getInstance();
}
clouddb.AGConnectCloudDB.initialize(agcClient);
const cloudDBZoneConfig = new clouddb.CloudDBZoneConfig(zoneName);
const agconnectCloudDB = clouddb.AGConnectCloudDB.getInstance(agcClient);
mCloudDBZone = agconnectCloudDB.openCloudDBZone(cloudDBZoneConfig);
}
async queryCount(data) {
console.log('云函数返回','开始')
if (!mCloudDBZone) {
logger.info("云函数 CloudDBClient is null, try re-initialize it");
console.log("云函数 CloudDBClient is null, try re-initialize it")
return;
}
try {
const cloudDBZoneQuery = clouddb.CloudDBZoneQuery.where(objectName)
if(data.text_book_id!=undefined){
cloudDBZoneQuery.equalTo("text_book_id",data.text_book_id)
}
if(data.grade!=undefined){
cloudDBZoneQuery.contains("grade",data.grade)
}
const resp = await mCloudDBZone.executeCountQuery(cloudDBZoneQuery, 'id');
console.log('云函数返回',resp)
console.log('云函数返回',JSON.stringify(resp))
return resp;
} catch (error) {
logger.info('querySum=>', error);
console.log('云函数返回',JSON.stringify(error))
}
}
。。。。
}