CRUD — 更新 — React Native SDK
本页中的示例使用以下模式:
class Task extends Realm.Object { static schema = { name: 'Task', properties: { _id: 'int', name: 'string', priority: 'int?', progressMinutes: 'int?', assignee: 'Person?', }, primaryKey: '_id', }; }
class Task extends Realm.Object<Task> { _id!: number; name!: string; priority?: number; progressMinutes?: number; assignee?: Person; age?: number; static schema: ObjectSchema = { name: 'Task', properties: { _id: 'int', name: 'string', priority: 'int?', progressMinutes: 'int', assignee: 'Person?', }, primaryKey: '_id', }; }
更新对象
您可以像更新任何其他 JavaScript 对象那样添加、修改或删除 Realm 对象的属性。但是,您必须在写事务内执行这些操作。
在以下 TaskItem
组件示例中,我们:
通过调用该组件中的
useRealm()
挂钩来访问打开的 Realm 实例。使用“Task”(任务)以及该组件的
_id
参数来调用useObject()
,从而检索任务。创建组件方法
incrementTaskProgress()
,用于执行写事务并递增任务的progressMinutes
。在用户界面中呈现任务的
name
和progressMinutes
。添加 onPress 调用
incrementTaskProgress()
的“增量”按钮上的事件。
const TaskItem = ({_id}) => { const realm = useRealm(); const myTask = useObject(Task, _id); const incrementTaskProgress = () => { if (myTask) { realm.write(() => { myTask.progressMinutes += 1; }); } }; if (myTask) { return ( <> <Text>Task: {myTask.name}</Text> <Text>Progress made (in minutes):</Text> <Text>{myTask.progressMinutes}</Text> <Button onPress={() => incrementTaskProgress()} title='Increment Task Progress' /> </> ); } else { return <></>; } };
const TaskItem = ({_id}: {_id: number}) => { const realm = useRealm(); const myTask = useObject(Task, _id); const incrementTaskProgress = () => { if (myTask) { realm.write(() => { myTask.progressMinutes! += 1; }); } }; if (myTask) { return ( <> <Text>Task: {myTask.name}</Text> <Text>Progress made (in minutes):</Text> <Text>{myTask.progressMinutes}</Text> <Button onPress={() => incrementTaskProgress()} title='Increment Task Progress' /> </> ); } else { return <></>; } };
更新或插入(upsert)对象
要在写事务中更新或插入对象,请调用Realm.create()并将更新模式设置为modified
。 该操作要么插入具有给定主键的新对象,要么更新已具有该主键的现有对象。
在以下 CreateTaskItem
组件示例中,我们:
通过调用该组件中的
useRealm()
挂钩来访问打开的 Realm 实例。执行写事务并创建一个
Task
对象,且该对象的_id
值为1234
。在此写事务中调用
Realm.create()
,从而通过指定相同的_id
和不同的“progressMinutes”并将更新模式设为“已修改”来更新或插入Task
对象。在用户界面中呈现任务的
name
和progressMinutes
,显示修改的进度。
const CreateTaskItem = () => { const realm = useRealm(); let myTask; realm.write(() => { // Add a new Task to the realm. Since no task with ID 1234 // has been added yet, this adds the instance to the realm. myTask = realm.create( 'Task', {_id: 1234, name: 'Wash the car', progressMinutes: 0}, 'modified', ); // If an object exists, setting the third parameter (`updateMode`) to // "modified" only updates properties that have changed, resulting in // faster operations. myTask = realm.create( 'Task', {_id: 1234, name: 'Wash the car', progressMinutes: 5}, 'modified', ); }); return ( <> <Text>{myTask.name}</Text> <Text>Progress made (in minutes):</Text> <Text>{myTask.progressMinutes}</Text> </> ); };
const CreateTaskItem = () => { const realm = useRealm(); const myTask: Realm.Object = realm.write(() => { // Add a new Task to the realm. Since no Task with ID 1234 // has been added yet, this adds the instance to the realm. realm.create( 'Task', {_id: 1234, name: 'Wash the car', progressMinutes: 0}, 'modified', ); // If an object exists, setting the third parameter (`updateMode`) to // "modified" only updates properties that have changed, resulting in // faster operations. return realm.create( 'Task', {_id: 1234, name: 'Wash the car', progressMinutes: 5}, 'modified', ); }); return ( <> <Text>{myTask.name}</Text> <Text>Progress made (in minutes):</Text> <Text>{myTask.progressMinutes}</Text> </> ); };
批量更新集合
如需对对象集合应用更新,需要遍历该集合(例如使用 for...of)。在循环中逐个更新每个对象。
在以下 TaskDashboard
组件示例中,我们:
通过调用该组件中的
useRealm()
挂钩来访问打开的 Realm 实例。通过将
Task
传递给useQuery()
钩子来检索 Realm 实例中的所有任务。创建用于执行写事务的组件方法
resetProgressOnAllTasks()
。在该写事务中,通过使用for...of
来循环遍历所有任务并将这些任务的progressMinutes
设为 0,从而批量更新所有任务。
const TaskDashboard = () => { const realm = useRealm(); const tasks = useQuery(Task); const resetProgressOnAllTasks = () => { realm.write(() => { for (const task of tasks) { task.progressMinutes = 0; } }); }; return ( <> {tasks.map(task => { <Text> {task.name} has {task.progressMinutes} minutes progressed </Text>; })} <Button onPress={resetProgressOnAllTasks} title='Reset Progress' /> </> ); };