Realm expects property types as lowercase strings like 'string' and 'bool'.
Step 2: Compare options
const TaskSchema = {name: 'Task', properties: {name: 'string', completed: 'bool'}}; uses correct lowercase strings. Options A, C, and D use incorrect types or JavaScript types instead of strings.
A. The second create will throw an error because a Task with the same primary key already exists
B. You cannot call realm.write() more than once
C. The second create will update the existing object automatically
D. There is no error; this code works fine
Solution
Step 1: Check schema requirements
With name as primary key, creating an object with the same primary key without update flag causes error.
Step 2: Analyze code behavior
The first create succeeds. The second create uses the same name (primary key) without update flag, so it throws an error.
Final Answer:
The second create will throw an error because a Task with the same primary key already exists -> Option A
Quick Check:
Duplicate primary key without update flag causes error [OK]
Hint: Use primary key and update flag to avoid create errors [OK]
Common Mistakes:
Assuming multiple writes are forbidden
Thinking create auto-updates without flags
Ignoring primary key requirements
5. You want to update the completed status of a Task object named 'Laundry' to true. (Assume the Task schema defines name as the primary key) Which code snippet correctly performs this update in Realm?
hard
A. realm.write(() => { realm.objects('Task').filtered('name == "Laundry"').completed = true; });
B. realm.write(() => { realm.create('Task', {name: 'Laundry', completed: true}); });
C. realm.write(() => { realm.update('Task', {name: 'Laundry', completed: true}); });