[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-09 21:08:02 +09:00
parent f0befc887a
commit 93ec7e9056
363 changed files with 68333 additions and 64 deletions
@@ -0,0 +1,114 @@
---
id: rn-asyncstorage-mmkv
title: RN 로컬 저장 — AsyncStorage vs MMKV
category: Coding
status: draft
source_trust_level: B
verification_status: conceptual
created_at: 2026-05-09
updated_at: 2026-05-09
tags: [react-native, storage, mmkv, vibe-coding]
tech_stack: { language: "TypeScript / react-native-mmkv", applicable_to: ["React Native"] }
applied_in: []
aliases: [AsyncStorage, MMKV, encrypted storage, Keychain RN]
---
# RN 로컬 저장
> AsyncStorage 는 async + 느림 (특히 Android). **MMKV (mmap key-value)** 가 동기 + 30배 빠름. 비밀은 **react-native-keychain** (iOS Keychain / Android Keystore).
## 📖 핵심 개념
- AsyncStorage: 호환성 좋음. async. 큰 값 / 자주 read 시 느림.
- MMKV (Tencent): mmap. 동기 read. iOS/Android 모두.
- Keychain: 암호화 비밀.
## 💻 코드 패턴
### MMKV
```ts
import { MMKV } from 'react-native-mmkv';
export const storage = new MMKV({
id: 'user-storage',
encryptionKey: getEncryptionKey(), // optional, 자동 암호화
});
storage.set('theme', 'dark'); // 동기
const theme = storage.getString('theme'); // 동기
storage.set('count', 42);
storage.set('flag', true);
// 객체
storage.set('user', JSON.stringify(user));
const u = JSON.parse(storage.getString('user') ?? 'null');
// 변경 감지
const sub = storage.addOnValueChangedListener(key => {
if (key === 'theme') refresh();
});
```
### React hook
```ts
import { useMMKVString, useMMKVNumber } from 'react-native-mmkv';
function App() {
const [theme, setTheme] = useMMKVString('theme', storage);
return <Button onPress={() => setTheme('dark')}>dark</Button>;
}
```
### Keychain — 비밀
```ts
import * as Keychain from 'react-native-keychain';
await Keychain.setGenericPassword('userToken', token, {
accessible: Keychain.ACCESSIBLE.AFTER_FIRST_UNLOCK,
authenticationType: Keychain.AUTHENTICATION_TYPE.BIOMETRICS,
});
const creds = await Keychain.getGenericPassword();
if (creds) console.log(creds.password);
```
### Migration AsyncStorage → MMKV
```ts
import AsyncStorage from '@react-native-async-storage/async-storage';
async function migrate() {
if (storage.contains('migrated')) return;
const keys = await AsyncStorage.getAllKeys();
for (const k of keys) {
const v = await AsyncStorage.getItem(k);
if (v) storage.set(k, v);
}
storage.set('migrated', true);
await AsyncStorage.clear();
}
```
## 🤔 의사결정 기준
| 데이터 | 저장 |
|---|---|
| 단순 설정 (theme, locale) | MMKV |
| 큰 list / 객체 (1MB+) | SQLite (RN-WatermelonDB / op-sqlite) |
| 비밀 (token, password) | Keychain |
| 영구 캐시 (이미지) | FastImage / Expo FileSystem |
| 임시 in-memory | useState / context |
| 새 RN 프로젝트 | MMKV 디폴트 |
## ❌ 안티패턴
- **AsyncStorage 에 token**: 암호화 없음. iOS 백업 노출.
- **MMKV 에 거대 JSON (수 MB)**: read/write 느림. SQLite.
- **MMKV instance 매번 new**: instance 한 번만 생성, export.
- **encryptionKey 하드코딩**: 부팅 시 Keychain 에서 받아오기.
- **AsyncStorage 와 MMKV 혼용 + 동기화 X**: stale.
- **JSON.parse 후 catch 없음**: corrupt 시 crash.
## 🤖 LLM 활용 힌트
- 신규 RN = MMKV. 비밀은 Keychain.
- 동기 read 가 hook 친화 — useMMKVString.
## 🔗 관련 문서
- [[iOS_Keychain_Storage]]
- [[Android_DataStore_Patterns]]