Multi operations allow you to read or write multiple storage items in a single operation, improving performance through parallel execution.
multiGet()
Retrieve multiple items from storage at once.
async multiGet(keys: string[]): Promise<Record<string, string | null>>
Parameters
An array of storage keys to retrieve
Returns
Promise<Record<string, string | null>>
Promise<Record<string, string | null>>
A promise that resolves to an object mapping each key to its stored value. Keys that don’t exist will have a value of null.
Example
import Storage from 'expo-native-storage';
// Get multiple values at once
const result = await Storage.multiGet(['username', 'email', 'theme']);
console.log(result);
// {
// username: 'john_doe',
// email: '[email protected]',
// theme: 'dark'
// }
// Check for missing keys
if (result.theme === null) {
console.log('Theme not set, using default');
}
Advanced Example
// Load user session data
const sessionKeys = ['authToken', 'userId', 'userEmail', 'lastLogin'];
const session = await Storage.multiGet(sessionKeys);
if (session.authToken && session.userId) {
// Valid session
initializeUserSession(session);
} else {
// Invalid or missing session
redirectToLogin();
}
All get operations are executed in parallel using Promise.all(), making this significantly faster than calling getItem() multiple times:
// Slow - sequential operations
const username = await Storage.getItem('username');
const email = await Storage.getItem('email');
const theme = await Storage.getItem('theme');
// Fast - parallel operations
const result = await Storage.multiGet(['username', 'email', 'theme']);
multiSet()
Store multiple items in storage at once.
async multiSet(items: Record<string, string>): Promise<void>
Parameters
items
Record<string, string>
required
An object where keys are storage keys and values are the strings to store
Returns
A promise that resolves when all items are stored
Example
import Storage from 'expo-native-storage';
// Store multiple values at once
await Storage.multiSet({
username: 'john_doe',
email: '[email protected]',
theme: 'dark'
});
Advanced Example
// Save user session
await Storage.multiSet({
authToken: 'eyJhbGciOiJIUzI1NiIs...',
userId: '12345',
userEmail: '[email protected]',
lastLogin: new Date().toISOString()
});
// Batch update settings
const newSettings = {
theme: 'dark',
language: 'en',
notifications: 'enabled',
fontSize: '16'
};
await Storage.multiSet(newSettings);
All set operations are executed in parallel using Promise.all(), making this significantly faster than calling setItem() multiple times:
// Slow - sequential operations
await Storage.setItem('username', 'john_doe');
await Storage.setItem('email', '[email protected]');
await Storage.setItem('theme', 'dark');
// Fast - parallel operations
await Storage.multiSet({
username: 'john_doe',
email: '[email protected]',
theme: 'dark'
});
Implementation Details
Both multiGet() and multiSet() use Promise.all() internally to execute operations in parallel:
// multiGet implementation
async multiGet(keys: string[]): Promise<Record<string, string | null>> {
const result: Record<string, string | null> = {};
await Promise.all(
keys.map(async (key) => {
result[key] = await this.getItem(key);
})
);
return result;
}
// multiSet implementation
async multiSet(items: Record<string, string>): Promise<void> {
await Promise.all(
Object.entries(items).map(([key, value]) => this.setItem(key, value))
);
}
This parallel execution provides significant performance improvements, especially when:
- Working with multiple storage keys
- Initializing or saving app state
- Loading user preferences
- Managing session data
Use Cases
Loading App Configuration
async function loadAppConfig() {
const config = await Storage.multiGet([
'theme',
'language',
'notifications',
'fontSize',
'autoSave'
]);
return {
theme: config.theme || 'light',
language: config.language || 'en',
notifications: config.notifications === 'true',
fontSize: parseInt(config.fontSize || '14'),
autoSave: config.autoSave === 'true'
};
}
Saving User Session
async function saveUserSession(user: User, token: string) {
await Storage.multiSet({
authToken: token,
userId: user.id.toString(),
userEmail: user.email,
userName: user.name,
lastLogin: new Date().toISOString()
});
}
Batch Settings Update
async function updateUserPreferences(prefs: UserPreferences) {
await Storage.multiSet({
'pref_theme': prefs.theme,
'pref_language': prefs.language,
'pref_notifications': prefs.notifications.toString(),
'pref_emailAlerts': prefs.emailAlerts.toString(),
'pref_pushAlerts': prefs.pushAlerts.toString()
});
}