0% found this document useful (0 votes)
5 views40 pages

08 App Mobile Teo Local Persistence(1)

The document outlines a course on mobile applications, focusing on local persistence using SQLite and Room. It covers key concepts such as the repository pattern, local vs remote persistence, and practical implementation details. Additionally, it includes feedback from Sprint 01 and tasks for Sprint 03 related to database integration and testing.

Uploaded by

jixiaolong2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views40 pages

08 App Mobile Teo Local Persistence(1)

The document outlines a course on mobile applications, focusing on local persistence using SQLite and Room. It covers key concepts such as the repository pattern, local vs remote persistence, and practical implementation details. Additionally, it includes feedback from Sprint 01 and tasks for Sprint 03 related to database integration and testing.

Uploaded by

jixiaolong2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Applications for Mobile Devices

Local Persistence

Dr. Vítor Luiz da Silva Verbel


[email protected]

Course: 105025-2425
Contents
Sprint 01 - Feedback

Recap: Repository Pattern

Persistence

SqlLite

Room

Hands On

Sprint 03

Applications for Mobile Devices


Sprint 01 - Feedback

Applications for Mobile Devices


Sprint 01 - Feedback

Puntos Importantes
• Uso de la IA
• Copyright (c) 2025 [Your Name or Organization]
• Organización del Proyecto (Nombres x Documentación)
• Release v.0.1.0 vs v1.0.0 (semver.org)
• No creación de las clases de dominio (domain/)
• Bugs al compilar

Applications for Mobile Devices


Recap: Repository Pattern

Applications for Mobile Devices


Repository in MVVM

Schema of the MVVM architecture

Applications for Mobile Devices


Repository Pattern

What
• The repository pattern is a structural design pattern. It’s instrumental for organizing how you access data.
• A repository defines data operations. The most common operations are creating, reading, updating, and deleting
data, (also known as CRUD).
• The repository pattern was first introduced in 2004 by Eric Evans in his book, Domain-Driven Design: Tackling
Complexity in the Heart of Software.

Why
• Serves as an intermediary layer between the data source and business logic.
• Simplifies data access and management without binding the app to a specific technology.

Key Benefits
• Promotes separation of concerns, enhancing testability and maintenance.
• Allows changing the data access implementation without altering the business logic.

Applications for Mobile Devices


Repository Pattern

Applications for Mobile Devices


Repository Example
// Data class representing an itinerary
data class Itinerary(val id: Int, val description: String)

// Class Trip that manages its own list of itineraries


class Trip(val id: Int, val destination: String) {
private val itineraries = mutableListOf<Itinerary>()

// Function to add an itinerary to the trip


fun addItinerary(itinerary: Itinerary) {
itineraries.add(itinerary)
}

// Function to retrieve the list of itineraries


fun getItineraries(): List<Itinerary> = itineraries
}

// Example usage
fun main() {
val trip = Trip(id = 1, destination = "Barcelona")

// Add itineraries to the trip


trip.addItinerary(Itinerary(1, "Stroll along Las Ramblas"))
trip.addItinerary(Itinerary(2, "Visit Sagrada Familia"))
}

non-repository repository

Applications for Mobile Devices


Repository Example

Repository Data Models (DTO - Data Transport Objects)

Repository Implementation (In Memory)

Repository Interface

Applications for Mobile Devices


Repository Example

non-repository

repository

Applications for Mobile Devices


Local Persistence

Applications for Mobile Devices


Persistence in Android
Local Persistence:
• Refers to storing data directly on the device.
• Provides fast, offline access to data.
• Data remains on the device, even when not connected to the Internet.

Remote Persistence:
• Supports cross-device data sync and real-time updates
• Involves storing data on remote servers or cloud services (e.g., Firebase, AWS,
Google Cloud).
• Enables data to be synced and accessed across multiple devices.
• Supports real-time updates and collaboration.
• Often uses REST APIs or cloud databases for data management.
Example of repository connecting to local and remote data source
https://developer.android.com/training/testing/ui-tests

Applications for Mobile Devices


Persistence in Android

Local Persistence:

• SharedPreferences:
• Lightweight key-value storage, ideal for simple data.
• Stored as XML files in the app’s internal storage (similar to cache storage).
• SQLite/Room:
• Relational database for structured data, supporting complex queries.
• File Storage:
• Storing files (images, documents, etc.) in internal or external storage.

Remote Persistence:
• Involves cloud storage solutions (e.g., Firebase, AWS)
• Supports cross-device data sync and real-time updates

Applications for Mobile Devices


Local vs Remote Persistence
Local Persistence Remote Persistence
Advantages: Advantages:
• Fast access and low latency. • Data is accessible from any device with Internet access.
• Works offline, ensuring data is available without Internet • Scalable storage and easier data backup.
connectivity. • Enables real-time data synchronization and collaboration.
• Simpler implementation for small or isolated data sets.

Disadvantages: Disadvantages:
• Limited storage capacity on the device. • Requires stable network connectivity.
• Data is confined to a single device; no cross-device • Potential latency and data synchronization challenges.
synchronization. • More complex setup, including security, authentication, and
• Potential security issues if the device is compromised. handling network failures.

Applications for Mobile Devices


SQLite

Applications for Mobile Devices


SQLite

https://www.sqlite.org/about.html

• One single file


• Built in (no installation required)
• ACID Compliant
• Small size (usefull for mobile devices)
• ´CUD´ operations lock DB (remember to close connection!)
• ´R´ opeartions allow simultaneus connections
• Tables are needed to be created / updated by the app
• Data Types are a subset of SQL Server types.

Applications for Mobile Devices


SQLite - Datatypes

https://www.sqlite.org/datatype3.html

Applications for Mobile Devices


Unix time stamp

https://www.unixtimestamp.com/

Applications for Mobile Devices


SQLite Hands On
• Download /source examples/SQLiteExample.zip, don’t run it

Applications for Mobile Devices


Create a Database

Applications for Mobile Devices


Creating Tables

Applications for Mobile Devices


Query Example (Insert)

Applications for Mobile Devices


Query Example (Select using cursor)

Applications for Mobile Devices


Expected Result
• App -> Run

Applications for Mobile Devices


DB Inspection
• Open View-> Tools -> App Inspection

Applications for Mobile Devices


Query Inspector
• Databases -> Users -> Double Click
• ISO Text (TEXT): ~20 bytes for the
base string alone; with length and
terminator, it expands to ~22 bytes, and
potentially more if milliseconds or a
detailed timezone are included.

• Unix Timestamp (INTEGER): ~4 bytes


(a typical case for a timestamp in the
int32 range), up to 8 bytes if working
with 64-bit ranges.

Note the 2 dt_creation columns


each using one different format
(ISO and Unix Time Stamp) as an
example

Applications for Mobile Devices


Query Inspector
• Databases -> Users -> Open New Query Tab

Applications for Mobile Devices


Device Explorer
• Open View-> Tools -> Device Explorer

Applications for Mobile Devices


Device Explorer (2)
• Open View-> Tools -> Device Explorer

Applications for Mobile Devices


Testing DB Inspection
• Uncomment all db.close() and run again. See what happens.

Applications for Mobile Devices


SQLite + ROOM

Applications for Mobile Devices


Code Review
• Download /source examples/LocalPersistence.zip

Applications for Mobile Devices


Code Review - Structure
• Refactoring:
• DI folder
• Data/repository

Applications for Mobile Devices


Code Review - DI

Applications for Mobile Devices


Code Review - Repository

Applications for Mobile Devices


Sprint 03

Applications for Mobile Devices


Sprint 03 – Persistence

T1. Implement SQLite Persistence (5 Points)


T2. Integrate Room Database into the Application (3 Points)
• T1.1 Create Room Database class.
• T1.2 Define Entities for Trip and ItineraryItem. Must contain at • T2.1 Modify ViewModels to use Room Database instead of in-memory
least one datetime field, text and integer. storage.
• T1.3 Create Data Access Objects (DAOs) for database operations. • T2.2 Ensure UI updates when database changes.
• T1.4 Implement CRUD operations using DAO for trips and
itineraries.
T3. Testing and Debugging (2 Points)
• T3.1 Write unit tests for DAOs and database interactions.
• T3.2 Implement data validation (e.g., prevent duplicate trip names, check
valid dates).
• T3.3 Use Logcat to track database operations and errors.
• T3.4 Update documentation with database schema and usage at design.md.

Applications for Mobile Devices 3


Project – Travel Planner

*Delays will incur a 30% penalty. The last delivery cannot be delayed.

Applications for Mobile Devices 3


Applications for Mobile Devices

That’s all

Full Name 2022 March 27


Week IV
Course: 105025-2425

You might also like