6

在普通 sqlite 中,我们可以轻松实现同步,但是如何在 Room 中实现它

4

3 回答 3

5

以下示例展示了如何将 Room 与 Content Provider 一起使用,然后您可以将 (ContentProvider ) 与 SynchronizationAdapter 链接。

话虽如此,您可以像这样修改 Room 模型

@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; 
    }
}
于 2018-02-16T08:51:45.577 回答
0
@Database(entities = [Student::class,Teacher::class], version = 1, exportSchema = false)
@TypeConverters(DataTypeConverter::class)
abstract class ClassDatabase : RoomDatabase() {

 // initiate database

 abstract fun studentDao()
 abstract fun teacherDao()
}

val database = ClassDatabase(context)
database.beginTransaction()
try{
   //write your operations
    database.setTransactionSuccessful()
   }
finally{
    database.endTransaction()
     }
于 2019-09-10T07:21:25.623 回答
0

正如您所说“这很容易实现与 SQLITE 的同步”

Room 在内部使用 SQLite 数据库。我相信在 Room 中可以使用 SQLite 的方法。因此,空间也应该很容易。

这表示 Sqlite 类被包装到 Room 类中,您可能需要编写一些管道。您使用什么来轻松同步?

于 2017-07-25T11:03:40.430 回答