HarmonyOS 鸿蒙Next 开发中首选项存储和关系型数据库会冲突吗
HarmonyOS 鸿蒙Next 开发中首选项存储和关系型数据库会冲突吗
加入关系型数据库后,首选项存储的读取就出了问题,请问可能是什么原因呢。
import CommonConstants from '../common/constant/CommonConstants';
import StyleConstants from '../common/constant/StyleConstants';
import CommonUtils from '../common/utils/CommonUtils';
import MultipleDevicesUtils from '../common/utils/MultipleDevicesUtils';
import { router } from '@kit.ArkUI';
import PreferencesUtil from '../common/utils/PreferencesUtil'
import FontSave from '../viewmodel/FontSave'
@Entry
@Component
export struct LoginPage {
@StorageProp('currentDeviceSize') currentDeviceSize: string = CommonConstants.SM;
onPageShow() {
MultipleDevicesUtils.register();
}
build() {
GridRow({
columns: {
sm: StyleConstants.COLUMNS_SM,
md: StyleConstants.COLUMNS_MD,
lg: StyleConstants.COLUMNS_LG
},
gutter: {
x: StyleConstants.GRID_GUTTER
}
}) {
GridCol({
span: {
sm: StyleConstants.SPAN_SM,
md: StyleConstants.SPAN_MD,
lg: StyleConstants.SPAN_LG
}, offset: {
md: StyleConstants.OFFSET_MD,
lg: StyleConstants.OFFSET_LG
}
}) {
Column() {
// Title component
LoginTitle()
// Bottom component
LoginBottom()
}
}
}
.backgroundColor($r('app.color.background'))
}
}
@Preview
@Component
struct LoginTitle {
build() {
Column() {
Image($r('app.media.neu'))
.width($r('app.float.logo_image_size'))
.height($r('app.float.logo_image_size'))
.margin({ bottom: $r('app.float.logo_margin_bottom') })
Text('资源复用平台')
.fontSize($r('app.float.common_text_size'))
.fontWeight(FontWeight.Medium)
.fontColor($r('app.color.title_text_color'))
Text('精勤博学,学以致用。厚积薄发,智行天下')
.fontSize($r('app.float.normal_text_size'))
.fontColor($r('app.color.more_text_color'))
.margin({
top: $r('app.float.more_margin_top'),
bottom: $r('app.float.more_margin_bottom')
})
}
.backgroundColor($r('app.color.background'))
.justifyContent(FlexAlign.Center)
.width(StyleConstants.FULL_PARENT)
.height(StyleConstants.LOGIN_LOGO_HEIGHT)
}
}
@Preview
@Component
export struct LoginBottom {
@State account: string = '';
@State password: string = '';
@State FontSaveData: FontSave = new FontSave();
async aboutToAppear() {//首选项读取
await PreferencesUtil.getPreferencesFromStorage();
PreferencesUtil.getFontData().then((resultData: FontSave) => {
if (resultData) {
this.FontSaveData.account = resultData.account;
console.debug('onPageShow: this.FontSaveData.account set to', this.FontSaveData.account);
this.FontSaveData.password = resultData.password;
console.debug('onPageShow: this.FontSaveData.password set to', this.FontSaveData.password);
}
});
}
build() {
Column() {
Column() {
TextInput({ text: "houmingyang1122", placeholder: "用户名" })
.maxLength(StyleConstants.INPUT_ACCOUNT_LENGTH)
.type(InputType.Normal)
.inputStyle()
.onChange((value: string) => {
this.account = value;
})
Line()
.width(StyleConstants.FULL_PARENT)
.height($r('app.float.line_height'))
.margin({
left: $r('app.float.line_margin_left'),
right: $r('app.float.line_margin_right')
})
.backgroundColor($r('app.color.line_color'))
TextInput({ text: "123456", placeholder: "密码" })
.maxLength(StyleConstants.INPUT_PASSWORD_LENGTH)
.type(InputType.Password)
.inputStyle()
.onChange((value: string) => {
this.password = value;
})
}
.backgroundColor(Color.White)
.borderRadius($r('app.float.login_input_radius'))
Row() {
Text('自动登录')
.blueTextStyle()
.onClick(() => {
if(this.FontSaveData.account ===''|| this.FontSaveData.password ===''){
CommonUtils.showToastContent(this.FontSaveData.account);
CommonUtils.showToastContent('账号或密码不能为空');
return;
}
CommonUtils.loginArkTS(this.FontSaveData.account, this.FontSaveData.password)
})
}
.justifyContent(FlexAlign.SpaceBetween)
.width(StyleConstants.FULL_PARENT)
.margin({ top: $r('app.float.forgot_margin_top') })
.padding({
left: $r('app.float.forgot_padding_left'),
right: $r('app.float.forgot_padding_right')
})
Button('登录')
.id(CommonConstants.LOGIN_BUTTON_ID)
.width(StyleConstants.FULL_PARENT)
.height($r('app.float.login_button_height'))
.fontSize($r('app.float.normal_text_size'))
.fontWeight(FontWeight.Medium)
.backgroundColor($r('app.color.login_button_color'))
.margin({
top: $r('app.float.login_button_top'),
bottom: $r('app.float.login_button_bottom')
})
.onClick(() => {
if(this.account ===''|| this.password ===''){
CommonUtils.showToastContent('账号或密码不能为空');
return;
}
CommonUtils.loginArkTS(this.account, this.password)
this.FontSaveData.account=this.account;
this.FontSaveData.password=this.password;
PreferencesUtil.putPreferencesValue(this.FontSaveData)
})
Text('注册')
.fontColor($r('app.color.blue_text_color'))
.fontSize($r('app.float.normal_text_size'))
.fontWeight(FontWeight.Medium)
.onClick(() => {
router.pushUrl({
url: 'pages/RegPage' // 目标url
}, router.RouterMode.Standard, (err) => {
if (err) {
console.error(`Invoke pushUrl failed, code is ${err.code}, message is ${err.message}`);
return;
}
console.info('Invoke pushUrl succeeded.');
});
})
}
.padding({
left: $r('app.float.login_button_left'),
right: $r('app.float.login_button_right'),
})
.backgroundColor($r('app.color.background'))
.height(StyleConstants.LOGIN_BOTTOM_HEIGHT)
}
}
@Extend(TextInput)
function inputStyle() {
.placeholderColor($r('app.color.placeholder_color'))
.height($r('app.float.login_input_height'))
.fontSize($r('app.float.normal_text_size'))
.backgroundColor(Color.White)
.margin({ top: $r('app.float.input_margin_top') })
.padding({ left: StyleConstants.INPUT_PADDING_LEFT })
}
@Extend(Text)
function blueTextStyle() {
.fontColor($r('app.color.blue_text_color'))
.fontSize($r('app.float.small_text_size'))
.fontWeight(FontWeight.Medium)
}
import preferences from '@ohos.data.preferences'
import FontSave from '../../viewmodel/FontSave'
import Logger from './Logger';
import { promptAction } from '@kit.ArkUI';
const PREFERENCES_NAME='myPreferences'
const KEY_APP_Account_Save = 'appdata'
let context = getContext(this) as Context;
let preferenceFont: preferences.Preferences ;
class PreferencesUtil {
private FontSaveData: FontSave = new FontSave();
// 加载Preferences
async getPreferencesFromStorage() {
preferenceFont=await preferences.getPreferences(context,PREFERENCES_NAME)
}
// 放Preferences数据
async putPreferencesValue(FontSave:FontSave) {
if(preferenceFont!==null){
await preferenceFont.put(KEY_APP_Account_Save,JSON.stringify(FontSave));
await preferenceFont.flush();
}
}
async getPreferences() {
let FontSave = '';
if (!preferenceFont) {
await this.getPreferencesFromStorage();
}
if (preferenceFont !== null) {
FontSave = (await preferenceFont.get(KEY_APP_Account_Save,'')).toString();
}
if (FontSave === '') {
this.showToastMessage($r('app.string.data_is_null_msg'))
return;
}
this.showToastMessage($r('app.string.read_success_msg'));
return JSON.parse(FontSave);
}
async deletePreferences() {
await preferences.deletePreferences(context,PREFERENCES_NAME)
}
async getFontData() {
this.FontSaveData = await this.getPreferences();
return this.FontSaveData;
}
checkFontData(FontSave: FontSave) {
if (FontSave.FontSize === 0 || FontSave.FontRatio === 0) {
this.showToastMessage($r('app.string.fruit_input_null_msg'));
return true;
}
return false;
}
writeData(FontSave: FontSave) {
// Check whether the data is null.
let isDataNull = this.checkFontData(FontSave);
if (isDataNull) {
return;
}
// The data is inserted into the preferences database if it is not empty.
this.putPreferencesValue(FontSave);
this.showToastMessage($r('app.string.write_success_msg'));
}
showToastMessage(message: Resource) {
promptAction.showToast({
message: message,
duration: 3000
});
};
}
export default new PreferencesUtil();
4 回复
aboutToAppear不能加async和await
我是应用开发初学者,这里不太了解,请问大佬如果不使用async和await大概要怎么写可以页面出现就读取首选项存储呀
你这个都不用在aboutToAppear里加async和await,直接调用getPreferencesFromStorage就行了,因为你在getPreferencesFromStorage里已经await了
HarmonyOS 鸿蒙Next 开发中首选项存储和关系型数据库不会冲突。
在HarmonyOS鸿蒙Next开发中,首选项存储和关系型数据库是两种不同的数据存储解决方案,它们各自具有独特的应用场景和优势,可以共存并不会相互冲突。
首选项存储通常用于保存应用的配置信息,它以键值对的形式存储数据,具有访问速度快、效率高的特点,但不适合存储大量数据。而关系型数据库则适用于存储大量数据,并且需要进行复杂查询或多表关联的场景。它提供了事务、索引、约束等功能,用于保证数据的完整性和一致性。
在开发过程中,开发者可以根据应用的具体需求和场景选择合适的数据存储方案。如果应用需要存储少量简单数据,可以选择使用首选项存储;如果需要存储大量数据并进行复杂查询和关联操作,则应选择关系型数据库。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html
回到顶部