Challenge - 5 Problems
Realm Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this Realm query code?
Given a Realm database with a schema for Task objects having a
completed boolean field, what will be the length of completedTasks after running this code?React Native
const realm = await Realm.open({schema: [{name: 'Task', properties: {name: 'string', completed: 'bool'}}]}); realm.write(() => { realm.create('Task', {name: 'Buy milk', completed: true}); realm.create('Task', {name: 'Walk dog', completed: false}); realm.create('Task', {name: 'Read book', completed: true}); }); const completedTasks = realm.objects('Task').filtered('completed == true'); console.log(completedTasks.length);
Attempts:
2 left
💡 Hint
Count how many tasks have completed set to true.
✗ Incorrect
Only two tasks have completed set to true: 'Buy milk' and 'Read book'. The filtered query returns those two.
❓ lifecycle
intermediate2:00remaining
What happens if you forget to close a Realm instance in React Native?
In React Native, if you open a Realm instance but do not call
realm.close() when the component unmounts, what is the likely outcome?React Native
import React, {useEffect} from 'react'; import Realm from 'realm'; export default function MyComponent() { useEffect(() => { Realm.open({schema: [{name: 'Note', properties: {text: 'string'}}]}).then(realm => { // Use realm }); // No realm.close() called }, []); return null; }
Attempts:
2 left
💡 Hint
Think about resource management and cleanup in apps.
✗ Incorrect
Not closing Realm instances keeps resources open, causing memory leaks and potential crashes.
📝 Syntax
advanced2:00remaining
Which option correctly defines a Realm schema with a primary key?
You want to define a Realm schema for a
User model with id as primary key and name as string. Which option is correct?Attempts:
2 left
💡 Hint
Check the correct placement and spelling of primaryKey in schema object.
✗ Incorrect
The primaryKey property must be at the root level of the schema object, not inside properties, and spelled exactly 'primaryKey'.
advanced
2:00remaining
What is the effect of this Realm write transaction in React Native?
Consider this code snippet inside a React Native app using Realm. What will be the value of
task.completed after this write?React Native
const realm = await Realm.open({schema: [{name: 'Task', properties: {name: 'string', completed: 'bool'}}]}); realm.write(() => { let task = realm.create('Task', {name: 'Clean house', completed: false}); task.completed = true; }); const task = realm.objects('Task').filtered('name == "Clean house"')[0]; console.log(task.completed);
Attempts:
2 left
💡 Hint
Think about how Realm transactions update objects.
✗ Incorrect
Inside a write transaction, updating the property sets it in the database. The final value is true.
🔧 Debug
expert2:00remaining
Why does this Realm query throw an error?
This code throws an error when running the filtered query. What is the cause?
React Native
const realm = await Realm.open({schema: [{name: 'Person', properties: {name: 'string', age: 'int'}}]}); realm.write(() => { realm.create('Person', {name: 'Alice', age: 30}); }); const results = realm.objects('Person').filtered('age > "25"');
Attempts:
2 left
💡 Hint
Check the data types used in the filter expression.
✗ Incorrect
The filter compares an integer field to a string literal, causing a type mismatch error.