Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to achieve Synchronization in Room (Android persistence library)

In normal sqlite we can achieve Synchronization easily but How to implement it in Room

like image 465
Deepak Avatar asked Jul 03 '17 05:07

Deepak


People also ask

What is Room persistence library?

The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite. Latest Update.

Is Room database persistent?

Room is a persistence library that's part of Android Jetpack. Room is an abstraction layer on top of a SQLite database. SQLite uses a specialized language (SQL) to perform database operations.

Is Android Room an ORM?

An easy way to use a database in an Android app is with a library called Room. Room is what's called an ORM (Object Relational Mapping) library, which as the name implies, maps the tables in a relational database to objects usable in Kotlin code.


1 Answers

Here is the sample which shows how to use Room with Content Provider which then you can link (ContentProvider ) with your SynchronizationAdapter.

Having said that you can modify your Room model as like

@Entity(tableName = Student.TABLE_NAME) 
public class Student { 
    /** The name of the Student table. */ 
    public static final String TABLE_NAME = "student"; 


    /** The name of the ID column. */ 
    public static final String COLUMN_ID = BaseColumns._ID; 


    /** The name of the name column. */ 
    public static final String COLUMN_NAME = "name"; 


    /** The unique ID of the Student*/ 
    @PrimaryKey(autoGenerate = true) 
    @ColumnInfo(index = true, name = COLUMN_ID) 
    public long id; 


    /** The name of the Student*/ 
    @ColumnInfo(name = COLUMN_NAME) 
    public String name; 


    /** 
     * Create a new {@link Studentfrom the specified {@link ContentValues}. 
     * 
     * @param values A {@link ContentValues} that at least contain {@link #COLUMN_NAME}. 
     * @return A newly created {@link Student} instance. 
     */ 
    public static Student fromContentValues(ContentValues values) { 
        final Student student= new Student(); 
        if (values.containsKey(COLUMN_ID)) {
            student.id = values.getAsLong(COLUMN_ID); 
        } 
        if (values.containsKey(COLUMN_NAME)) { 
            student.name = values.getAsString(COLUMN_NAME); 
        }
        return student; 
    }
}
like image 88
Ishwor Khanal Avatar answered Sep 19 '22 02:09

Ishwor Khanal