Overview
Threadly uses Room Persistence Library, an abstraction layer over SQLite, to provide robust offline functionality. The database caches critical data including messages, conversation history, and notifications, enabling the app to function seamlessly without network connectivity.Database Configuration
Database Class
The main database class uses the Singleton pattern to ensure a single instance throughout the app lifecycle:RoomDb/DataBase.java:16
Key Configuration Details
- Database Name: “Threadly”
- Version: 1
- Entities: 3 (Messages, History, Notifications)
- Export Schema: Disabled (set to
false)
Database Entities
1. MessageSchema
Stores all chat messages with full metadata:RoomDb/schemas/MessageSchema.java:8
Message Entity Features
Message Types
Supports text, image, video, and post-sharing messages
Delivery Tracking
Tracks message delivery status: pending (0), sent (-1), seen (-2)
Media Upload
Monitors upload progress for media messages
Reply Threading
Supports message replies via replyToMsgId
2. HistorySchema
Maintains conversation history for the messages list:RoomDb/schemas/HistorySchema.java:8
History Entity Purpose
- Displays conversation list in Messages tab
- Shows latest message preview
- Stores user profile information for quick access
- Enables offline browsing of recent conversations
3. NotificationSchema
Caches interaction notifications (likes, comments, follows):RoomDb/schemas/NotificationSchema.java:8
Notification Types
- Likes: Post and comment likes
- Comments: New comments on posts
- Follows: New followers and follow requests
- Mentions: Tagged in posts or comments
Data Access Objects (DAOs)
Message DAO (operator)
Provides comprehensive message operations:RoomDb/Dao/operator.java:14
Key DAO Features
LiveData Queries
Returns LiveData for automatic UI updates when data changes
Batch Operations
Supports inserting multiple messages at once
Soft Delete
Messages are marked as deleted rather than removed
Unread Tracking
Counts unread messages per conversation and globally
Offline Strategy
Message Flow
Delivery Status Codes
| Status | Value | Meaning |
|---|---|---|
| Pending | 0 | Not yet sent to server |
| Delivered | -1 | Sent but not read |
| Seen | -2 | Read by recipient |
Sync Strategy
-
On App Launch
- Fetch recent messages from API
- Merge with local database
- Resolve conflicts (server wins)
-
On Network Restore
- Query pending messages (
deliveryStatus=0) - Attempt to send all pending messages
- Update delivery status on success
- Query pending messages (
-
On Message Receive
- Insert into Room immediately
- Notify UI via LiveData
- Update conversation history
ViewModel Integration
ViewModels provide clean access to database operations:viewmodels/MessagesViewModel.java:14
Database Operations Best Practices
1. Background Thread Execution
All database operations (except LiveData queries) must run on background threads:2. LiveData Observation
Observe LiveData in lifecycle-aware components:3. Soft Deletes
Messages are never physically deleted, only marked:4. Duplicate Prevention
TheGROUP BY messageUid clause prevents duplicate messages:
Performance Optimizations
Indexed Columns
Primary keys and frequently queried columns (conversationId, messageUid) are indexed
Efficient Queries
Queries use specific column selection and WHERE clauses to minimize data transfer
LiveData Caching
LiveData caches results and only emits when data actually changes
Singleton Pattern
Single database instance prevents multiple connections and overhead
Testing Database Operations
Room provides testing utilities for unit tests:Related Documentation
MVVM Pattern
See how ViewModels interact with the database
Project Structure
Locate database files in the project
Architecture Overview
Understand how database fits in overall architecture