Tag: android room

房间:Dao的LiveData将在每次更新时触发Observer.onChanged,即使LiveData值没有变化

我发现Dao返回的LiveData将在DB更新行时调用其观察者,即使LiveData值显然没有改变。 考虑下面的例子: 示例实体 @Entity public class User { public long id; public String name; // example for other variables public Date lastActiveDateTime; } 例如道 @Dao public interface UserDao { // I am only interested in the user name @Query(“SELECT name From User”) LiveData<List> getAllNamesOfUser(); @Update(onConflict = OnConflictStrategy.REPLACE) void updateUser(User user); } 在后台线程的某处 UserDao userDao = […]

Android室错误:Dao类必须用@Dao标注

我正在使用我的Android应用程序的空间。 我现在试图设置我的数据库,但是有一个错误消息,说,Dao类必须注释@Dao。 但是正如你在编码片段中看到的那样,Dao类用@Dao标注。 有谁知道问题或我的错误可能是哪里? 这两个文件都不在同一个文件夹中(DAO位于服务文件夹中,而另一个类位于模型文件夹中) Device.java @Entity(tableName = “device”) public class Device { @PrimaryKey(autoGenerate = true) public int device_id; @ColumnInfo(name = “identifier”) public String identifier; @ColumnInfo(name = “language”) public int language; @ColumnInfo(name = “searchFilter”) public int searchFilter; public Device(String identifier, int language, int searchFilter){ this.identifier = identifier; this.language = language; this.searchFilter = searchFilter; } } […]

Android室错误:types转换器无法识别列表的枚举

房间库不识别我为枚举List创建的TypeConverter 。 但是,当我将其更改为枚举的ArrayList它工作正常。 任何人有任何想法,为什么和我能做些什么来使这个工作与List ? (在Kotlin中使用列表更容易,我真的不希望只是因为这个原因转换回ArrayList )。 这是我的代码: 我的模特: @Entity data class Example(@PrimaryKey val id: String?, val name: String, var days: List?) DayOfWeek是一个枚举: enum class DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; val value: Int get() = ordinal + 1 companion object { private val ENUMS = DayOfWeek.values() fun of(dayOfWeek: Int): DayOfWeek { […]

房间找不到实施

我在测试一个Room数据库时遇到了问题:当我运行测试时,出现以下exception: java.lang.RuntimeException: cannot find implementation for database.TicketDatabase. TicketDatabase_Impl does not exist at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92) at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:454) at com.sw.ing.gestionescontrini.DatabaseTest.setDatabase(DatabaseTest.java:36) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at […]

如何在RoomDatabase中存储Enum的ArrayList

我的问题是如何存储 List 在RoomDatabase中,到目前为止我找不到答案。

链接多个RxJava,Room和Retrofit

我正在使用RxJava / Kotlin和Room和Retrofit。 我确信我没有写任何东西,因为我刚开始学习RxJava。 这种情况是我打电话来检查数据库中是否有最喜欢的记录,并将它们放入列表中,从API获取数据并将其插入到数据库中,使用之前的collections夹列表更新数据库,并获取所有记录更新,列表。 我得到了我的片段的结果,但每次我得到它,就好像我得到1不喜欢的项目,直到我没有collections的项目。 Repository fun getKafaniFromApi(): Observable<List> { return apiService.getKafani().toObservable().doOnNext { insertKafaniInDb(it) } } fun getKafaniFromDb(): Observable<List> { return kafanaDao.getKafani().toObservable() } fun insertKafaniInDb(kafani: List) { Observable.fromCallable { kafanaDao.insertAll(kafani) } .subscribeOn(Schedulers.io()) .subscribe { Timber.d(“Inserted ${kafani.size} kafani from API in DB…”) } } fun getFavoriteKafani(): Single<List> { return kafanaDao.getFavoriteKafani() } fun setKafanaFavorite(kafana: Kafana, isFavorite: […]

SyncAdapter中的Singleton Room数据库触发LiveData

我努力在我的SyncAdapter使用Room作为单例。 我使用Kotlin。 我的房间课 @Database(entities = [(Product::class)], version = 1, exportSchema = false) abstract class AppDatabase : RoomDatabase() { abstract fun productDao(): ProductDao companion object { @Volatile private var INSTANCE: AppDatabase? = null fun getInstance(context: Context) : AppDatabase = INSTANCE ?: synchronized(this) { INSTANCE ?: buildDatabase(context.applicationContext) .also {INSTANCE = it} } private fun buildDatabase(context: Context) = […]

运行时崩溃与Proguard启用房间持久性库

当启用了proguard时,我一直面对房间稳定版本的崩溃 FATAL EXCEPTION: main Process: .debug, PID: 27841 java.lang.RuntimeException: Unable to create application .DebugBaseApplication: java.lang.RuntimeException: cannot find implementation for .database.c. c_Impl does not exist at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5015) at android.app.ActivityThread.-wrap1(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1573) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5811) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681) Caused by: java.lang.RuntimeException: cannot find implementation for .database.c. c_Impl does not exist […]

一个LiveData中有多个对象

我有这个实体: @Entity public class CartItem { @PrimaryKey(autoGenerate = true) private long id; private long itemId; private long serviceId; private String observation; @Ignore private Service service; @Ignore private Item item; } 正如你所看到的,它由其他对象组成,在这种情况下, Service和Item 。 在片段中,我以这种方式实现LiveData: CartItemViewModel model = CartItemViewModel.create(getActivity()); model.getLiveData().observe(this, new Observer<List>() { @Override public void onChanged(@Nullable List cartItems) { } 它正在工作, onChange方法正在按预期调用。 这是我在kotlin中的ViewModel: class CartItemViewModel […]

Android室 – 简单的选择查询 – 无法访问主线程上的数据库

我正在试用Room Persistence Library的示例。 我创建了一个实体: @Entity public class Agent { @PrimaryKey public String guid; public String name; public String email; public String password; public String phone; public String licence; } 创建一个DAO类: @Dao public interface AgentDao { @Query(“SELECT COUNT(*) FROM Agent where email = :email OR phone = :phone OR licence = :licence”) int agentsCount(String email, String […]