Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Docs
提交
51ff2a3b
D
Docs
项目概览
OpenHarmony
/
Docs
1 年多 前同步成功
通知
159
Star
292
Fork
28
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Docs
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
51ff2a3b
编写于
8月 31, 2023
作者:
O
openharmony_ci
提交者:
Gitee
8月 31, 2023
浏览文件
操作
浏览文件
下载
差异文件
!23577 元能力ArlTS整改补充
Merge pull request !23577 from liuliu/dfx
上级
54c73c25
a2dc3eea
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
108 addition
and
59 deletion
+108
-59
zh-cn/application-dev/application-models/create-dataability.md
.../application-dev/application-models/create-dataability.md
+14
-14
zh-cn/application-dev/dfx/apprecovery-guidelines.md
zh-cn/application-dev/dfx/apprecovery-guidelines.md
+84
-39
zh-cn/application-dev/dfx/errormanager-guidelines.md
zh-cn/application-dev/dfx/errormanager-guidelines.md
+10
-6
未找到文件。
zh-cn/application-dev/application-models/create-dataability.md
浏览文件 @
51ff2a3b
...
...
@@ -9,32 +9,32 @@
```
ts
import
featureAbility
from
'
@ohos.ability.featureAbility
'
;
import
dataAbility
from
'
@ohos.data.dataAbility
'
;
import
relationalStore
from
'
@ohos.data.relationalStore
'
;
import
Want
from
'
@ohos.app.ability.Want
'
;
import
common
from
'
@ohos.app.ability.common
'
;
import
{
AsyncCallback
,
BusinessError
}
from
'
@ohos.base
'
;
import
rdb
from
'
@ohos.data.rdb
'
;
const
TABLE_NAME
=
'
book
'
const
STORE_CONFIG
:
r
elationalStore
.
StoreConfig
=
{
name
:
'
book.db
'
,
securityLevel
:
1
}
const
STORE_CONFIG
:
r
db
.
StoreConfig
=
{
name
:
'
book.db
'
}
const
SQL_CREATE_TABLE
=
'
CREATE TABLE IF NOT EXISTS book(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, introduction TEXT NOT NULL)
'
let
rdbStore
:
r
elationalStore
.
RdbStore
|
undefined
=
undefined
let
rdbStore
:
r
db
.
RdbStore
|
undefined
=
undefined
export
default
class
DataAbility
{
onInitialized
(
want
:
Want
)
{
console
.
info
(
'
DataAbility onInitialized, abilityInfo:
'
+
want
.
bundleName
)
let
context
=
featureAbility
.
getContext
()
r
elationalStore
.
getRdbStore
(
context
,
STORE_CONFIG
,
(
err
,
store
)
=>
{
let
context
:
common
.
BaseContext
=
{
stageMode
:
featureAbility
.
getContext
().
stageMode
}
r
db
.
getRdbStore
(
context
,
STORE_CONFIG
,
1
,
(
err
,
store
)
=>
{
console
.
info
(
'
DataAbility getRdbStore callback
'
)
store
.
executeSql
(
SQL_CREATE_TABLE
,
[])
rdbStore
=
store
});
}
;
}
insert
(
uri
:
string
,
valueBucket
:
rdb
.
ValuesBucket
,
callback
:
AsyncCallback
<
number
>
)
{
console
.
info
(
'
DataAbility insert start
'
)
if
(
rdbStore
)
{
rdbStore
.
insert
(
TABLE_NAME
,
valueBucket
,
callback
)
}
}
;
}
batchInsert
(
uri
:
string
,
valueBuckets
:
Array
<
rdb
.
ValuesBucket
>
,
callback
:
AsyncCallback
<
number
>
)
{
console
.
info
(
'
DataAbility batch insert start
'
)
if
(
rdbStore
)
{
...
...
@@ -49,29 +49,29 @@ export default class DataAbility {
}
}
}
}
;
}
query
(
uri
:
string
,
columns
:
Array
<
string
>
,
predicates
:
dataAbility
.
DataAbilityPredicates
,
callback
:
AsyncCallback
<
relationalStore
.
ResultSet
>
)
{
callback
:
AsyncCallback
<
rdb
.
ResultSet
>
)
{
console
.
info
(
'
DataAbility query start
'
)
let
rdbPredicates
=
dataAbility
.
createRdbPredicates
(
TABLE_NAME
,
predicates
)
if
(
rdbStore
)
{
rdbStore
.
query
(
rdbPredicates
,
columns
,
callback
)
}
}
;
}
update
(
uri
:
string
,
valueBucket
:
rdb
.
ValuesBucket
,
predicates
:
dataAbility
.
DataAbilityPredicates
,
callback
:
AsyncCallback
<
number
>
)
{
callback
:
AsyncCallback
<
number
>
)
{
console
.
info
(
'
DataAbilityupdate start
'
)
let
rdbPredicates
=
dataAbility
.
createRdbPredicates
(
TABLE_NAME
,
predicates
)
if
(
rdbStore
)
{
rdbStore
.
update
(
valueBucket
,
rdbPredicates
,
callback
)
}
}
;
}
delete
(
uri
:
string
,
predicates
:
dataAbility
.
DataAbilityPredicates
,
callback
:
AsyncCallback
<
number
>
)
{
console
.
info
(
'
DataAbilitydelete start
'
)
let
rdbPredicates
=
dataAbility
.
createRdbPredicates
(
TABLE_NAME
,
predicates
)
if
(
rdbStore
)
{
rdbStore
.
delete
(
rdbPredicates
,
callback
)
}
}
;
}
;
}
}
```
zh-cn/application-dev/dfx/apprecovery-guidelines.md
浏览文件 @
51ff2a3b
...
...
@@ -128,8 +128,13 @@ import AbilityConstant from '@ohos.app.ability.AbilityConstant';
-
定义和注册
[
ErrorObserver
](
../reference/apis/js-apis-inner-application-errorObserver.md
)
callback,具体可参考
[
errorManager
](
../reference/apis/js-apis-app-ability-errorManager.md
)
里的使用方法。
```
ts
var
registerId
=
-
1
;
var
callback
=
{
import
appRecovery
from
'
@ohos.app.ability.appRecovery
'
;
import
errorManager
from
'
@ohos.app.ability.errorManager
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
window
from
'
@ohos.window
'
;
let
registerId
=
-
1
;
let
callback
:
errorManager
.
ErrorObserver
=
{
onUnhandledException
(
errMsg
)
{
console
.
log
(
errMsg
);
appRecovery
.
saveAppState
();
...
...
@@ -137,15 +142,20 @@ import AbilityConstant from '@ohos.app.ability.AbilityConstant';
}
}
onWindowStageCreate
(
windowStage
)
{
// Main window is created, set main page for this ability
console
.
log
(
"
[Demo] EntryAbility onWindowStageCreate
"
)
globalThis
.
registerObserver
=
(()
=>
{
export
default
class
EntryAbility
extends
UIAbility
{
onWindowStageCreate
(
windowStage
:
window
.
WindowStage
)
{
// Main window is created, set main page for this ability
console
.
log
(
"
[Demo] EntryAbility onWindowStageCreate
"
)
registerId
=
errorManager
.
on
(
'
error
'
,
callback
);
})
windowStage
.
loadContent
(
"
pages/index
"
,
null
);
windowStage
.
loadContent
(
"
pages/index
"
,
(
err
,
data
)
=>
{
if
(
err
.
code
)
{
console
.
error
(
'
Failed to load the content. Cause:
'
+
JSON
.
stringify
(
err
));
return
;
}
console
.
info
(
'
Succeeded in loading the content. Data:
'
+
JSON
.
stringify
(
data
))
})
}
}
```
...
...
@@ -154,12 +164,17 @@ import AbilityConstant from '@ohos.app.ability.AbilityConstant';
callback触发appRecovery.saveAppState()调用后,会触发EntryAbility的onSaveState(state, wantParams)函数回调。
```
ts
onSaveState
(
state
,
wantParams
)
{
// Ability has called to save app data
console
.
log
(
"
[Demo] EntryAbility onSaveState
"
)
wantParams
[
"
myData
"
]
=
"
my1234567
"
;
return
AbilityConstant
.
OnSaveResult
.
ALL_AGREE
;
}
import
AbilityConstant
from
'
@ohos.app.ability.AbilityConstant
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
export
default
class
EntryAbility
extends
UIAbility
{
onSaveState
(
state
:
AbilityConstant
.
StateType
,
wantParams
:
Record
<
string
,
Object
>
)
{
// Ability has called to save app data
console
.
log
(
"
[Demo] EntryAbility onSaveState
"
)
wantParams
[
"
myData
"
]
=
"
my1234567
"
;
return
AbilityConstant
.
OnSaveResult
.
ALL_AGREE
;
}
}
```
-
数据恢复
...
...
@@ -167,15 +182,26 @@ callback触发appRecovery.saveAppState()调用后,会触发EntryAbility的onSa
callback触发后appRecovery.restartApp()调用后,应用会重启,重启后会走到EntryAbility的onCreate(want, launchParam)函数,保存的数据会在want参数的parameters里。
```
ts
storage
:
LocalStorage
onCreate
(
want
,
launchParam
)
{
console
.
log
(
"
[Demo] EntryAbility onCreate
"
)
globalThis
.
abilityWant
=
want
;
if
(
launchParam
.
launchReason
==
AbilityConstant
.
LaunchReason
.
APP_RECOVERY
)
{
this
.
storage
=
new
LocalStorage
();
let
recoveryData
=
want
.
parameters
[
"
myData
"
];
this
.
storage
.
setOrCreate
(
"
myData
"
,
recoveryData
);
this
.
context
.
restoreWindowStage
(
this
.
storage
);
import
AbilityConstant
from
'
@ohos.app.ability.AbilityConstant
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
Want
from
'
@ohos.app.ability.Want
'
;
let
abilityWant
:
Want
;
export
default
class
EntryAbility
extends
UIAbility
{
storage
:
LocalStorage
|
undefined
=
undefined
;
onCreate
(
want
:
Want
,
launchParam
:
AbilityConstant
.
LaunchParam
)
{
console
.
log
(
"
[Demo] EntryAbility onCreate
"
)
abilityWant
=
want
;
if
(
launchParam
.
launchReason
==
AbilityConstant
.
LaunchReason
.
APP_RECOVERY
)
{
this
.
storage
=
new
LocalStorage
();
if
(
want
.
parameters
)
{
let
recoveryData
=
want
.
parameters
[
"
myData
"
];
this
.
storage
.
setOrCreate
(
"
myData
"
,
recoveryData
);
this
.
context
.
restoreWindowStage
(
this
.
storage
);
}
}
}
}
```
...
...
@@ -183,15 +209,20 @@ onCreate(want, launchParam) {
-
取消注册ErrorObserver callback
```
ts
onWindowStageDestroy
()
{
// Main window is destroyed, release UI related resources
console
.
log
(
"
[Demo] EntryAbility onWindowStageDestroy
"
)
import
errorManager
from
'
@ohos.app.ability.errorManager
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
let
registerId
=
-
1
;
export
default
class
EntryAbility
extends
UIAbility
{
onWindowStageDestroy
()
{
// Main window is destroyed, release UI related resources
console
.
log
(
"
[Demo] EntryAbility onWindowStageDestroy
"
)
globalThis
.
unRegisterObserver
=
(()
=>
{
errorManager
.
off
(
'
error
'
,
registerId
,
(
err
)
=>
{
console
.
error
(
"
[Demo] err:
"
,
err
);
});
}
)
}
}
```
...
...
@@ -200,20 +231,28 @@ onWindowStageDestroy() {
被动保存和恢复依赖恢复框架底层触发,无需注册监听ErrorObserver callback,只需实现Ability的onSaveState接口数据保存和onCreate接口数据恢复流程即可。
```
ts
export
default
class
EntryAbility
extends
Ability
{
storage
:
LocalStorage
onCreate
(
want
,
launchParam
)
{
console
.
log
(
"
[Demo] EntryAbility onCreate
"
)
globalThis
.
abilityWant
=
want
;
import
AbilityConstant
from
'
@ohos.app.ability.AbilityConstant
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
Want
from
'
@ohos.app.ability.Want
'
;
let
abilityWant
:
Want
;
export
default
class
EntryAbility
extends
UIAbility
{
storage
:
LocalStorage
|
undefined
=
undefined
onCreate
(
want
:
Want
,
launchParam
:
AbilityConstant
.
LaunchParam
)
{
console
.
log
(
"
[Demo] EntryAbility onCreate
"
)
abilityWant
=
want
;
if
(
launchParam
.
launchReason
==
AbilityConstant
.
LaunchReason
.
APP_RECOVERY
)
{
this
.
storage
=
new
LocalStorage
();
let
recoveryData
=
want
.
parameters
[
"
myData
"
];
this
.
storage
.
setOrCreate
(
"
myData
"
,
recoveryData
);
this
.
context
.
restoreWindowStage
(
this
.
storage
);
if
(
want
.
parameters
)
{
let
recoveryData
=
want
.
parameters
[
"
myData
"
];
this
.
storage
.
setOrCreate
(
"
myData
"
,
recoveryData
);
this
.
context
.
restoreWindowStage
(
this
.
storage
);
}
}
}
onSaveState
(
state
,
wantParams
)
{
onSaveState
(
state
:
AbilityConstant
.
StateType
,
wantParams
:
Record
<
string
,
Object
>
)
{
// Ability has called to save app data
console
.
log
(
"
[Demo] EntryAbility onSaveState
"
)
wantParams
[
"
myData
"
]
=
"
my1234567
"
;
...
...
@@ -227,10 +266,16 @@ export default class EntryAbility extends Ability {
发生故障的Ability再次重新启动时,在调度onCreate生命周期里,参数want的parameters成员会有
[
ABILITY_RECOVERY_RESTART
](
../reference/apis/js-apis-app-ability-wantConstant.md#wantconstantparams
)
标记数据,并且值为true。
```
ts
import
AbilityConstant
from
'
@ohos.app.ability.AbilityConstant
'
;
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
Want
from
'
@ohos.app.ability.Want
'
;
import
wantConstant
from
'
@ohos.app.ability.wantConstant
'
;
export
default
class
EntryAbility
extends
UIAbility
{
onCreate
(
want
,
launchParam
)
{
onCreate
(
want
:
Want
,
launchParam
:
AbilityConstant
.
LaunchParam
)
{
if
(
want
.
parameters
===
undefined
)
{
return
;
}
if
(
want
.
parameters
[
wantConstant
.
Params
.
ABILITY_RECOVERY_RESTART
]
!=
undefined
&&
want
.
parameters
[
wantConstant
.
Params
.
ABILITY_RECOVERY_RESTART
]
==
true
)
{
console
.
log
(
"
This ability need to recovery
"
);
...
...
zh-cn/application-dev/dfx/errormanager-guidelines.md
浏览文件 @
51ff2a3b
...
...
@@ -38,14 +38,17 @@
## 开发示例
```
ts
import
UIAbility
from
'
@ohos.app.ability.UIAbility
'
;
import
AbilityConstant
from
'
@ohos.app.ability.AbilityConstant
'
;
import
errorManager
from
'
@ohos.app.ability.errorManager
'
;
import
Want
from
'
@ohos.app.ability.Want
'
;
import
window
from
'
@ohos.window
'
;
let
registerId
=
-
1
;
let
callback
=
{
onUnhandledException
:
function
(
errMsg
)
{
let
callback
:
errorManager
.
ErrorObserver
=
{
onUnhandledException
:
(
errMsg
)
=>
{
console
.
log
(
errMsg
);
},
onException
:
function
(
errorObj
)
{
onException
:
(
errorObj
)
=>
{
console
.
log
(
'
onException, name:
'
,
errorObj
.
name
);
console
.
log
(
'
onException, message:
'
,
errorObj
.
message
);
if
(
typeof
(
errorObj
.
stack
)
===
'
string
'
)
{
...
...
@@ -53,12 +56,13 @@ let callback = {
}
}
}
let
abilityWant
:
Want
;
export
default
class
EntryAbility
extends
UIAbility
{
onCreate
(
want
,
l
aunchParam
)
{
onCreate
(
want
:
Want
,
launchParam
:
AbilityConstant
.
L
aunchParam
)
{
console
.
log
(
"
[Demo] EntryAbility onCreate
"
)
registerId
=
errorManager
.
on
(
"
error
"
,
callback
);
globalThis
.
abilityWant
=
want
;
abilityWant
=
want
;
}
onDestroy
()
{
...
...
@@ -68,7 +72,7 @@ export default class EntryAbility extends UIAbility {
});
}
onWindowStageCreate
(
windowStage
)
{
onWindowStageCreate
(
windowStage
:
window
.
WindowStage
)
{
// Main window is created, set main page for this ability
console
.
log
(
"
[Demo] EntryAbility onWindowStageCreate
"
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录