2017-07-03 70 views
0

在正常的SQLite,我们可以实现同步很容易,但如何实现它在房间如何实现同步房间(Android的持久性库)

+2

你什么意思通过同步做?请编辑您的问题并澄清问题? – Devrim

+0

我在服务器上有一个后端数据库,在我的设备上有一个数据库我如何同步这些数据库 –

+0

这与Room或SQLite以及与您的服务器有关的一切无关。无论使用SQLite的技术应该能够使用Room来实现。毕竟,你可以用你的'RoomDatabase'运行任意的SQL。如果您有关于如何转换**特定**代码的**特定**问题,则可能需要提出单独的Stack Overflow问题。 – CommonsWare

回答

0

以下是sample,它显示了如何使用Room with Content Provider,然后您可以将(ContentProvider)与您的SynchronizationAdapter链接起来。

说了这么多,你可以修改你的房间模型,像

@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; 
     } 
} 
0

正如你指出,“这是很容易实现的SQLite同步”

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

这就是说,Sqlite类被包装到Room类中,你可能需要编写一些管道。 你使用什么来简化同步?