| name | android-data-layer |
| description | Guidance on implementing the Data Layer using Repository pattern, Room (Local), and Retrofit (Remote) with offline-first synchronization. |
Android Data Layer & Offline-First
Instructions
The Data Layer coordinates data from multiple sources.
1. Repository Pattern
2. Local Persistence (Room)
- Usage: Primary cache and offline storage.
- Entities: Define
@Entity data classes.
- DAOs: Return
Flow<T> for observable data.
3. Remote Data (Retrofit)
- Usage: Fetching data from backend.
- Response: Use
suspend functions in interfaces.
- Error Handling: Wrap network calls in
try-catch blocks or a Result wrapper to handle exceptions (NoInternet, 404, etc.) gracefully.
4. Synchronization
- Read: "Stale-While-Revalidate". Show local data immediately, trigger a background refresh.
- Write: "Outbox Pattern" (Advanced). Save local change immediately, mark as "unsynced", use
WorkManager to push changes to server.
5. Dependency Injection