Computer Science
Grade 9
20 min
7. Data Storage: Local Storage and AsyncStorage
Explore different data storage options for mobile apps, including local storage and AsyncStorage.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define persistent data storage and explain its importance in mobile apps.
Differentiate between synchronous (Local Storage) and asynchronous (AsyncStorage) operations.
Explain the concept of a key-value pair for storing data.
Write code to save a simple string value using AsyncStorage.
Write code to retrieve a saved string value using AsyncStorage.
Write code to remove a saved value using AsyncStorage.
Ever wonder how a game remembers your high score or a to-do list app remembers your tasks even after you close it? 🤔 Let's learn the magic behind it!
In this lesson, we'll explore how mobile apps save information directly on your device. We will learn about two common methods, Local Storage and AsyncStorage, and discover how to use them to make...
2
Key Concepts & Vocabulary
TermDefinitionExample
Persistent StorageA way for an application to save data that remains even after the app is closed or the device is turned off. This is different from a variable, which is cleared from memory when the app closes.Your phone's photo gallery uses persistent storage to keep your pictures safe.
Key-Value PairA simple way to store data where each piece of information (the 'value') is given a unique name (the 'key') to find it later. Think of it like a dictionary word and its definition.To store a username, you might use the key 'username' and the value 'gamer123'.
Local StorageA type of web storage that allows web apps to store key-value pairs in a web browser. It is synchronous, meaning it blocks other code from running until it...
3
Core Syntax & Patterns
Saving Data with AsyncStorage
await AsyncStorage.setItem('keyName', 'value');
Use this pattern to save a string value. The 'keyName' is the unique identifier you'll use to find the data later, and 'value' is the information you want to store. The `await` keyword is crucial because the operation is asynchronous.
Getting Data with AsyncStorage
const myValue = await AsyncStorage.getItem('keyName');
Use this pattern to retrieve a previously saved value. You provide the 'keyName' you used to save the data, and it returns the corresponding value. If the key doesn't exist, it returns `null`.
Removing Data with AsyncStorage
await AsyncStorage.removeItem('keyName');
Use this pattern to delete a sp...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
A developer needs to store a user's settings object: `{ theme: 'dark', notifications: true, volume: 8 }`. Which code snippet correctly saves this object under the key 'appSettings'?
A.await AsyncStorage.setItem('appSettings', { theme: 'dark', notifications: true, volume: 8 });
B.await AsyncStorage.saveObject('appSettings', { theme: 'dark', notifications: true, volume: 8 });
C.const settings = { theme: 'dark', notifications: true, volume: 8 };
await AsyncStorage.setItem('appSettings', JSON.stringify(settings));
D.const settings = '{ theme: 'dark', notifications: true, volume: 8 }';
await AsyncStorage.setItem('appSettings', settings);
Challenging
A developer saves a score using `await AsyncStorage.setItem('highScore', '950');`. Later, they retrieve it with `const score = await AsyncStorage.getItem('highScore');`. Why would the check `if (score > 900)` potentially behave unexpectedly without an extra step?
A.The `await` keyword works differently for numbers.
B.AsyncStorage saves all values as strings, so '950' is not a number and cannot be compared numerically.
C.The score is too high for AsyncStorage to handle.
D.The key 'highScore' is an invalid key name.
Challenging
An app has a feature to 'Reset All Settings'. A user's theme is stored with the key 'userTheme' and their name with 'playerName'. Which AsyncStorage function would be used as part of implementing this reset for the theme?
A.await AsyncStorage.getItem('userTheme');
B.await AsyncStorage.clear(); // (Note: clear() was not in the tutorial, but removeItem is)
C.await AsyncStorage.removeItem('userTheme');
D.await AsyncStorage.setItem('userTheme', 'default');
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing FreeMore from V. Mobile App Development: Building Applications for Mobile Devices
1. Introduction to Mobile App Development: Platforms, Frameworks, and Tools
2. Mobile UI/UX Design: Principles and Best Practices
5. Handling User Input: Text Fields, Buttons, and Touch Events
6. Navigation: Implementing App Navigation with React Navigation
8. Networking: Making API Calls to Fetch Data