0
0
React Nativemobile~20 mins

Realm database in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Realm Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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);
A3
B0
C1
D2
Attempts:
2 left
💡 Hint
Count how many tasks have completed set to true.
lifecycle
intermediate
2: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;
}
AMemory leak and possible app crash due to open Realm instances.
BRealm data will be lost immediately.
CApp will throw a syntax error at runtime.
DRealm automatically closes when component unmounts, no issues.
Attempts:
2 left
💡 Hint
Think about resource management and cleanup in apps.
📝 Syntax
advanced
2: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?
Aconst UserSchema = {name: 'User', primaryKey: 'id', properties: {id: 'int', name: 'string'}};
Bconst UserSchema = {name: 'User', properties: {id: 'int', name: 'string', primaryKey: 'id'}};
Cconst UserSchema = {name: 'User', properties: {id: 'int', name: 'string'}, primaryKey: 'id'};
Dconst UserSchema = {name: 'User', properties: {id: 'int', name: 'string'}, primary_key: 'id'};
Attempts:
2 left
💡 Hint
Check the correct placement and spelling of primaryKey in schema object.
navigation
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);
Afalse
Btrue
Cundefined
DThrows an error
Attempts:
2 left
💡 Hint
Think about how Realm transactions update objects.
🔧 Debug
expert
2: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"');
AFiltering with a string "25" instead of a number causes a type error.
BThe schema is missing a primary key, causing query failure.
CThe write transaction is missing a commit call.
DRealm does not support comparison operators in filtered queries.
Attempts:
2 left
💡 Hint
Check the data types used in the filter expression.