jueves, 9 de septiembre de 2021

Android: Coroutines

Pieces

  • Job
  • Dispatcher
  • Scope

Job

Conceptually, a job is a cancellable thing with a life-cycle that culminates in its completion. Jobs can be arranged into parent-child hierarchies so that cancellation of a parent leads to an immediate cancellation of all its children.

Dispatcher

The dispatcher sends off coroutines to run on various threads (for example, Dispatchers.Main, Dispatchers.IO

Scope

The scope combines information, including a job and dispatcher, to define the context in which the coroutine runs.

Example (on preview)

miércoles, 8 de septiembre de 2021

Android: LiveData and Room: Setup

Setup

Project's gradle 
Module's gradle


Room

Room has three main components of Room DB :

  • Entity
  • Dao
  • Database
  1. Entity - Represents a table within the database. Room creates a table for each class that has @Entity annotation

    1. @PrimaryKey(autoGenerate = true)
    2. @ColumnInfo(name = “column_name”)
    3. @Ignore — field will not be persisted by Room
    4. @Embeded — nested fields can be referenced directly in the SQL queries.

  2. Dao - DAOs are responsible for defining the methods that access the database.

  3. Database - Contains the database holder and serves as the main access point for the underlying connection to your app’s persisted, relational data.

Example

(See on preview for gists visibility)
  1. SleepNight entity

  2. Dao

  3. Database

viernes, 20 de agosto de 2021

Android: Vector drawables

Since vector drawables were added on api 21 if you have vector drawables and have support for api < 21 the system will generate pngs for all systems below 21. To avoid this:

 1. Enable the use of support library for vector drawables in build.gradle file (app level):

vectorDrawables.useSupportLibrary = true

2. Use app:srcCompat in the image tag in the layout file:

app:srcCompat="@drawable/empty_dice"

3. You’ll also need to add the namespace to the root of the layout:

xmlns:app="http://schemas.android.com/apk/res-auto"