我需要一个 react native Android/iOS 应用程序的数据库。作为学习 Realm 的第一步,我想用 Realm 替换 AsyncStorage 来读取应用程序设置。
- 问题:通过 nameKey 访问值的语法是什么?
我的 AppSetting 架构将名称设置为主键:
const AppSetting = {
name: 'AppSetting',
primaryKey: 'name',
schemaVersion: 1,
properties: {
name: {type: 'string'},
value: {type: 'string'},
}
};
如果 appSettings 为空,则我编写 AppSettingsDefault:
const AppSettingsDefault = [
{
name: 'version',
value: '0,'
},
{
name: 'gpsEnabled',
value: 'true,'
},
{
name: 'unitType',
value: '2,'
},
];
console.log('appSettings.length:', appSettings.length);
if(0 >= appSettings.length) {
console.log("Init default: AppSetting when app is run first time");
for(let i = 0; i < AppSettingsDefault.length; ++i) {
let name = AppSettingsDefault[i].name;
let value = AppSettingsDefault[i].value;
console.log(name, '=', value);
realm.write(()=>{
realm.create('AppSetting', {
name: name,
value: value,
});
})
}
}
在 react-native 中检索 'gpsEnabled' 值的 Realm 语法是什么?
let realm = new Realm({schema:
[AppSetting], schemaVersion: 1});
let appSettings = realm.objects('AppSetting');
// How to retrieve a value by the primary key?
console.log('gpsEnabled', appSettings['gpsEnabled']; <- This does NOT work
提前感谢任何提示或指导,
最佳答案
试试这个
let result = realm.objects('AppSetting').filtered('name = "gpsEnabled"');
let val = result[0].value;
关于database - 如何在 react-native 中 realm 数据库检索 nameKey-value?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35976303/