@@ -2,7 +2,7 @@ import { exeOnce } from '#common/utils/store/store-realm.js';
2
2
3
3
const oSign = '$object$' ;
4
4
/**
5
- * 转化对象
5
+ * 读取的时候:从对象的字符串转化到对象的对象
6
6
* @param obj
7
7
*/
8
8
function parseQuestion ( obj ) {
@@ -13,7 +13,7 @@ function parseQuestion(obj) {
13
13
} , { } ) ;
14
14
}
15
15
/**
16
- * 转化字符串
16
+ * 存入的时候:从对象的对象属性转化到字符串
17
17
*/
18
18
function stringifyQuestion ( obj ) {
19
19
if ( ! obj ) return null ;
@@ -32,9 +32,8 @@ function stringifyQuestion(obj) {
32
32
*/
33
33
export function getOneQuestion ( id ) {
34
34
return exeOnce ( ( realm ) => {
35
- const all = realm . objects ( 'AllQuestion' ) ;
36
- const question = all . filtered ( 'questionId=$0' , id ) ?. [ 0 ] ;
37
- return question ?. toJSON ( ) ;
35
+ const question = realm . objectForPrimaryKey ( 'AllQuestion' , id ) ;
36
+ return parseQuestion ( question ?. toJSON ( ) ) ;
38
37
} ) ;
39
38
}
40
39
@@ -47,10 +46,9 @@ export function setOneQuestion(question) {
47
46
return exeOnce ( ( realm ) => {
48
47
let newQuestion ;
49
48
realm . write ( ( ) => {
50
- realm . delete ( realm . objects ( 'AllQuestion' ) . filtered ( 'questionId=$0' , question . questionId ) ) ;
51
- newQuestion = realm . create ( 'AllQuestion' , question ) ;
49
+ newQuestion = realm . create ( 'AllQuestion' , stringifyQuestion ( question ) , true ) ;
52
50
} ) ;
53
- return newQuestion . toJSON ( ) ;
51
+ return newQuestion ? .toJSON ( ) ;
54
52
} ) ;
55
53
}
56
54
@@ -77,7 +75,7 @@ export function setAllQuestion(questions) {
77
75
for ( const question of questions ) {
78
76
const data = stringifyQuestion ( question ) ;
79
77
if ( ! data ?. questionId ) continue ;
80
- newQuestions . push ( realm . create ( 'AllQuestion' , data ) ) ;
78
+ newQuestions . push ( realm . create ( 'AllQuestion' , data , true ) ) ;
81
79
}
82
80
} ) ;
83
81
return newQuestions ;
0 commit comments