字典 - React Native SDK
realm@10.5.0
版本中的新增功能。
您可以使用 Realm.Dictionary 数据类型来管理由唯一字符串键值对组成的集合。dictionary
数据映射到 Javascript 对象 类型。
例如,要创建 home
属性定义为 dictionary
类型的 HomeOwner
Realm 对象,步骤可能如下所示:
realm.create('HomeOwner', { name: 'Anna Smith', home: {address: '2 jefferson lane', yearRenovated: 1994, color: 'blue'}, });
Realm 对象模型
您可以通过以下三种方式为 Realm 对象模型定义混合值字典:
将字段的数据类型设置为空对象
"{}"
。在括号前添加数据类型,创建包含特定类型值的字典。例如,使用
"int{}"
指定字典值必须是整数,或使用"string{}"
指定字典值必须是字符串。显式定义对象类型。这是在 Realm 中使用对象类型作为字典值的必要条件。
class HomeOwner extends Realm.Object { static schema = { name: 'HomeOwner', properties: { name: 'string', home: '{}', pets: { type: 'dictionary', objectType: 'Pet', optional: true, }, }, }; }
使用Realm.Dictionary
接口 扩展 类型以定义字典对象的语法。此类的所有 Realm 对象都必须遵循接口中指定的语法。
interface Home extends Realm.Dictionary { address?: string; color?: string; price?: number; yearRenovated?: number; } class HomeOwner extends Realm.Object<HomeOwner> { name!: string; home!: Home; pets?: Pet[]; static schema: ObjectSchema = { name: 'HomeOwner', properties: { name: 'string', home: 'mixed{}', pets: { type: 'dictionary', objectType: 'Pet', optional: true, }, }, }; }
Realm 不允许在映射键中使用 .
或 $
字符。您可以使用百分比编码和解码来存储包含这些不允许的任一字符的映射键。
// Percent encode . or $ characters to use them in map keys const mapKey = "kitchen.windows"; const encodedMapKey = mapKey.replace(".", "%2E");
创建附带字典值的对象
在下面的 CreateHomeOwner
示例中,我们创建了具有字典属性的新对象。
CreateHomeOwner
组件将执行以下操作:
创建 React 状态 分别代表房主的姓名和地址。
通过调用该组件中的
useRealm()
挂钩来访问打开的 Realm 实例。创建一个组件方法
SubmitHomeOwner()
,用于执行写事务(write transaction)并根据房主姓名和地址的HomeOwner
TextInput
值分别创建新的对象(object)对象。添加 onPress 调用 的提交按钮上的事件
SubmitHomeOwner()
1 const CreateHomeOwner = () => { 2 const [homeOwnerName, setHomeOwnerName] = useState('John Smith'); 3 const [address, setAddress] = useState('1 Home Street'); 4 const realm = useRealm(); 5 6 const submitHomeOwner = () => { 7 // Create a HomeOwner realm object within a Write Transaction 8 realm.write(() => { 9 realm.create('HomeOwner', { 10 name: homeOwnerName, 11 // For the dictionary field, 'home', set the value 12 // to a regular JavaScript object 13 home: { 14 address, 15 }, 16 }); 17 }); 18 }; 19 return ( 20 <View> 21 <TextInput 22 value={homeOwnerName} 23 onChangeText={text => setHomeOwnerName(text)} 24 /> 25 <TextInput value={address} onChangeText={text => setAddress(text)} /> 26 <Button 27 title='Submit Home Owner' 28 onPress={submitHomeOwner} 29 /> 30 </View> 31 ); 32 };
查询附带字典属性的对象
要过滤查询,请运行集合()以根据一个或多个对象属性的值指定结果子集。 您可以使用方 括号表示法,根据字典属性的值指定结果。
您还可以使用 <dictionary>.@keys
或 <dictionary>.@values
来确定结果集合是否包含某个键或值。例如,如果您有一个附带嵌套 home
字典的 HomeOwner
集合,则可通过运行查询 home.@keys = "price"
来返回附带具有 "price"
属性的 home
的所有 HomeOwner
对象。
例子
在下面的 HomeList
示例中,我们将查询具有字典属性的对象。
HomeList
组件将执行以下操作:
通过将
HomeOwner
类传递给useQuery
钩子,查询所有房主。通过向
collection.filtered()
传递查询home.@keys = "price"
,查询具有标价的房主。通过运行
collection.filtered()
查询位于 Summerhill Hill 的房屋,使用方括号表示法查找第一个地址为“Summerhill St”的房主,并使用点语法获取其房屋。通过向
collection.filtered()
传递查询'home.@values = "red"'
,查询任意字段值为 red 的所有房主。然后获取第一个房主的房屋。通过呈现房屋的相关信息,在用户界面中显示查询结果
1 const HomeList = () => { 2 // query for all HomeOwner objects 3 const homeOwners = useQuery(HomeOwner); 4 5 // run the `.filtered()` method on all the returned homeOwners to 6 // find all homeOwners that have a house with a listed price 7 const listedPriceHomes = useQuer(HomeOwner, homeOwners => { 8 return homeOwners.filtered('home.@keys = "price"'); 9 }); 10 11 // run the `.filtered()` method on all the returned homeOwners to 12 // find the house with the address "Summerhill St." 13 const summerHillHouse = useQuery(HomeOwner, homeOwners => { 14 return homeOwners.filtered('home["address"] = "Summerhill St."'); 15 })[0].home; 16 17 // run the `.filtered()` method on all the returned homeOwners to 18 // find the first house that has any field with a value of 'red' 19 const redHouse = useQuery(HomeOwner, homeOwners => { 20 return homeOwners.filtered('home.@values = "red"'); 21 })[0].home; 22 23 return ( 24 <View> 25 <Text>All homes:</Text> 26 {homeOwners.map(homeOwner => ( 27 <View> 28 <Text>{homeOwner.home.address}</Text> 29 </View> 30 ))} 31 32 <Text>All homes with a price:</Text> 33 {listedPriceHomes.map(homeOwner => ( 34 <View> 35 <Text>{homeOwner.home.address}</Text> 36 <Text>{homeOwner.home.price}</Text> 37 </View> 38 ))} 39 40 <Text>Summer Hill House:</Text> 41 <Text>{summerHillHouse.address}</Text> 42 <Text>{summerHillHouse.color}</Text> 43 44 <Text>Red House:</Text> 45 <Text>{redHouse.address}</Text> 46 </View> 47 ); 48 };
1 const HomeList = () => { 2 // query for all HomeOwner objects 3 const homeOwners = useQuery(HomeOwner); 4 5 // run the `.filtered()` method on all the returned homeOwners to 6 // find all homeOwners that have a house with a listed price 7 const listedPriceHomes = useQuery(HomeOwner, homeOwners => { 8 return homeOwners.filtered('home.@keys = "price"'); 9 }); 10 11 // run the `.filtered()` method on all the returned homeOwners to 12 // find the house with the address "Summerhill St." 13 const summerHillHouse = useQuery(HomeOwner, homeOwners => { 14 return homeOwners.filtered('home["address"] = "Summerhill St."'); 15 })[0].home; 16 17 // run the `.filtered()` method on all the returned homeOwners to 18 // find the first house that has any field with a value of 'red' 19 const redHouse = useQuery(HomeOwner, homeOwners => { 20 return homeOwners.filtered('home.@values = "red"'); 21 })[0].home; 22 23 return ( 24 <View> 25 <Text>All homes:</Text> 26 {homeOwners.map(homeOwner => ( 27 <View> 28 <Text>{homeOwner.home.address}</Text> 29 </View> 30 ))} 31 32 <Text>All homes with a price:</Text> 33 {listedPriceHomes.map(homeOwner => ( 34 <View> 35 <Text>{homeOwner.home.address}</Text> 36 <Text>{homeOwner.home.price}</Text> 37 </View> 38 ))} 39 40 <Text>Summer Hill House:</Text> 41 <Text>{summerHillHouse.address}</Text> 42 <Text>{summerHillHouse.color}</Text> 43 44 <Text>Red House:</Text> 45 <Text>{redHouse.address}</Text> 46 </View> 47 ); 48 };
更新字典
使用 dictionary.set() 方法或点表示法将字典属性设置为新的值以更新该属性。
例子
在下面的 UpdateHome
示例中,我们将更新字典的属性。
UpdateHome
组件将执行以下操作:
- 创建 React 状态
- 表示家庭地址的变量。
- 调用
useRealm()
挂钩以访问打开的 Realm 实例。 - 。
- 调用
- 创建组件方法
updateAddress()
,用于执行写 - 事务,并使用
dictionary.set()
将家庭住址设置为address
状态变量值。它还使用点语法将yearRenovated
设置为2004
。
- 创建组件方法
呈现一个显示并更改
address
状态变量的TextInput
。- 在以下位置中添加 onPress 事件
- 在“Update Address”按钮上以调用
updateAddress()
1 const UpdateHome = ({homeOwnerName}) => { 2 const [address, setAddress] = useState(''); 3 const realm = useRealm(); 4 const homeOwner = useQuery( 5 HomeOwner, 6 homeOwners => { 7 return homeOwners.filtered(`name == '${homeOwnerName}'`); 8 }, 9 [homeOwnerName], 10 )[0]; 11 12 const updateAddress = () => { 13 // Update the home object with the new address 14 realm.write(() => { 15 // use the `set()` method to update a field of a dictionary 16 homeOwner.home.set({address}); 17 // alternatively, update a field of a dictionary through dot notation 18 homeOwner.home.yearRenovated = 2004; 19 }); 20 }; 21 22 return ( 23 <View> 24 <Text>{homeOwner.name}</Text> 25 <TextInput 26 value={address} 27 onChangeText={setAddress} 28 placeholder='Enter new address' 29 /> 30 <Button 31 onPress={updateAddress} 32 title='Update Address' 33 34 /> 35 </View> 36 ); 37 };
1 const UpdateHome = ({homeOwnerName}: {homeOwnerName: string}) => { 2 const [address, setAddress] = useState(''); 3 const realm = useRealm(); 4 const homeOwner = useQuery( 5 HomeOwner, 6 homeOwners => { 7 return homeOwners.filtered(`name == '${homeOwnerName}'`); 8 }, 9 [homeOwnerName], 10 )[0]; 11 12 const updateAddress = () => { 13 // Update the home object with the new address 14 realm.write(() => { 15 // use the `set()` method to update a field of a dictionary 16 homeOwner.home.set({address}); 17 // alternatively, update a field of a dictionary through dot notation 18 homeOwner.home.yearRenovated = 2004; 19 }); 20 }; 21 22 return ( 23 <View> 24 <Text>{homeOwner.name}</Text> 25 <TextInput 26 value={address} 27 onChangeText={setAddress} 28 placeholder='Enter new address' 29 /> 30 <Button 31 onPress={updateAddress} 32 title='Update Address' 33 /> 34 </View> 35 ); 36 };
删除字典成员
要删除字典的成员,请使用 dictionary.remove() 方法以及要从字典删除的属性数组。
例子
在下面的 HomeInfo
示例中,我们将删除字典的成员。
HomeInfo
组件将执行以下操作:
通过调用该组件中的
useRealm()
挂钩来访问打开的 Realm 实例。检索第一个与作为属性传递给组件的姓名相匹配的房主。方法为获取从查询返回的第一个值:
useQuery(HomeOwner).filtered(`name == '${homeOwnerName}'`)
。创建用于执行写事务并调用
dictionary.remove()
的组件方法deleteExtraHomeInfo()
,以便删除yearRenovated
和color
属性。在用户界面中呈现房主的姓名和房屋地址。
添加 onPress 调用
deleteExtraHomeInfo()
的“删除额外的家庭信息”按钮上的事件。
1 const HomeInfo = ({homeOwnerName}) => { 2 const realm = useRealm(); 3 const homeOwner = useQuery( 4 HomeOwner, 5 homeOwners => { 6 return homeOwners.filtered(`name == '${homeOwnerName}'`); 7 }, 8 [homeOwnerName], 9 )[0]; 10 11 const deleteExtraHomeInfo = () => { 12 realm.write(() => { 13 // remove the 'yearRenovated' and 'color' field of the house 14 homeOwner.home.remove(['yearRenovated', 'color']); 15 }); 16 }; 17 18 return ( 19 <View> 20 <Text>{homeOwner.name}</Text> 21 <Text>{homeOwner.home.address}</Text> 22 <Button 23 onPress={deleteExtraHomeInfo} 24 title='Delete extra home info' 25 26 /> 27 </View> 28 ); 29 };
1 const HomeInfo = ({homeOwnerName}: {homeOwnerName: string}) => { 2 const realm = useRealm(); 3 const homeOwner = useQuery( 4 HomeOwner, 5 homeOwners => { 6 return homeOwners.filtered(`name == '${homeOwnerName}'`); 7 }, 8 [homeOwnerName], 9 )[0]; 10 11 const deleteExtraHomeInfo = () => { 12 realm.write(() => { 13 // remove the 'yearRenovated' and 'color' field of the house 14 homeOwner.home.remove(['yearRenovated', 'color']); 15 }); 16 }; 17 18 return ( 19 <View> 20 <Text>{homeOwner.name}</Text> 21 <Text>{homeOwner.home.address}</Text> 22 <Button 23 onPress={deleteExtraHomeInfo} 24 title='Delete extra home info' 25 /> 26 </View> 27 ); 28 };