Skip to content

Commit 5180e5a

Browse files
committed
perf(allquestion): modify the controller to get/set preferly
fix #63
1 parent 093201c commit 5180e5a

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

common/utils/store/controller/allQuestion.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { exeOnce } from '#common/utils/store/store-realm.js';
22

33
const oSign = '$object$';
44
/**
5-
* 转化对象
5+
* 读取的时候:从对象的字符串转化到对象的对象
66
* @param obj
77
*/
88
function parseQuestion(obj) {
@@ -13,7 +13,7 @@ function parseQuestion(obj) {
1313
}, {});
1414
}
1515
/**
16-
* 转化字符串
16+
* 存入的时候:从对象的对象属性转化到字符串
1717
*/
1818
function stringifyQuestion(obj) {
1919
if (!obj) return null;
@@ -32,9 +32,8 @@ function stringifyQuestion(obj) {
3232
*/
3333
export function getOneQuestion(id) {
3434
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());
3837
});
3938
}
4039

@@ -47,10 +46,9 @@ export function setOneQuestion(question) {
4746
return exeOnce((realm) => {
4847
let newQuestion;
4948
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);
5250
});
53-
return newQuestion.toJSON();
51+
return newQuestion?.toJSON();
5452
});
5553
}
5654

@@ -77,7 +75,7 @@ export function setAllQuestion(questions) {
7775
for (const question of questions) {
7876
const data = stringifyQuestion(question);
7977
if (!data?.questionId) continue;
80-
newQuestions.push(realm.create('AllQuestion', data));
78+
newQuestions.push(realm.create('AllQuestion', data, true));
8179
}
8280
});
8381
return newQuestions;

0 commit comments

Comments
 (0)