FileSystem
文章目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
FileSystem.getInfoAsync(`${FileSystem.documentDirectory}setting.json`)
.then(res => {
console.log('Setting.read.fileStatus', res);
if (res.exists) {

/* if exists then read and push to state */
FileSystem.readAsStringAsync(
`${FileSystem.documentDirectory}setting.json`
)
.then(res => JSON.parse(res))
.then(res => {
dispatch({
type: 'SETTING_READ_FINISHED',
settings: res,
});
console.log('Setting.read.Finished', res);
});

} else {

/* if not exists then create new setting file and push default value to state */
FileSystem.writeAsStringAsync(
`${FileSystem.documentDirectory}setting.json`,
JSON.stringify(getState().app.settings)
).then(res => {
dispatch({
type: 'SETTING_READ_FINISHED',
});
});
/* create a new sub folder */
FileSystem.makeDirectoryAsync(
`${FileSystem.documentDirectory}savedScore/`
);
}

})
.catch(err => {

dispatch({
type: 'SETTING_READ_FAILED',
});

});