android-architecture-components

How do I run a ListenableWorker work on a background thread?

南楼画角 提交于 2019-12-18 04:24:44
问题 Since I need to perform work asynchronously in WorkManager, I need to use the ListenableWorker , which by default runs on the main (UI) thread. Since this work could be a long processing tasks that could freeze the interface, I wanted to perform it on a background thread. In the Working with WorkManager (Android Dev Summit '18) video, the Google engineer showed how to manually configure WorkManager to run works on a custom Executor , so I followed his guidance: 1) Disable the default

Unit testing Room and LiveData

南楼画角 提交于 2019-12-17 21:51:19
问题 I'm currently developing an app using the newly Android Architecture Components. Specifically I'm implementing a Room Database that returns a LiveData object on one of it's queries. Insertion and querying works as expected, however I have an issue testing the query method using unit test. Here is the DAO I'm trying to test: NotificationDao.kt @Dao interface NotificationDao { @Insert fun insertNotifications(vararg notifications: Notification): List<Long> @Query("SELECT * FROM notifications")

How to share an instance of LiveData in android app?

南楼画角 提交于 2019-12-17 21:16:34
问题 Simple use case I am using MVVM architecture and Android Architecture Components in my app. After user logs in, I am fetching multiple network data and want to have access to it from different ViewModels attached to different Activities lifecycle. I don't want to use Room Persistence Library in my app. I have seen some question about sharing a ViewModel between Activities or using a LiveData as static member, but I think most of the cases we need to access the same data in multiple screens. I

Room Persistence: Error:Entities and Pojos must have a usable public constructor

独自空忆成欢 提交于 2019-12-17 16:24:00
问题 I'm converting a project to Kotlin and I'm trying to make my model (which is also my entity) a data class I intend to use Moshi to convert the JSON responses from the API @Entity(tableName = "movies") data class MovieKt( @PrimaryKey var id : Int, var title: String, var overview: String, var poster_path: String, var backdrop_path: String, var release_date: String, var vote_average: Double, var isFavorite: Int ) I can't build the app cause of the following error Entities and Pojos must have a

Android ViewModel has no zero argument constructor

时光毁灭记忆、已成空白 提交于 2019-12-17 08:47:51
问题 I am following this documentation to learn about LiveData and ViewModel. In the doc, the ViewModel class has constructor as such, public class UserModel extends ViewModel { private MutableLiveData<User> user; @Inject UserModel(MutableLiveData<User> user) { this.user = user; } public void init() { if (this.user != null) { return; } this.user = new MutableLiveData<>(); } public MutableLiveData<User> getUser() { return user; } } However, when I run the code, I get exception: final

Return type for Android Room joins

烈酒焚心 提交于 2019-12-17 07:11:42
问题 Let's say I want to do an INNER JOIN between two entities Foo and Bar : @Query("SELECT * FROM Foo INNER JOIN Bar ON Foo.bar = Bar.id") List<FooAndBar> findAllFooAndBar(); Is it possible to force a return type like this? public class FooAndBar { Foo foo; Bar bar; } When I try to do that, I get this error: error: Cannot figure out how to read this field from a cursor. I've also tried aliasing the table names to match the field names, but that didn't work either. If this isn't possible, how

Crash starting service when observing ProcessLifecycleOwner ON_START

那年仲夏 提交于 2019-12-13 16:13:09
问题 I have a foreground monitoring class that registers as a LifecycleObserver to the ProcessLifecycleOwner 's Lifecycle . When I receive an ON_START event, I am starting a Service to run while the app is in the foreground. I am seeing IllegalStateException s every so often on app launch stating that starting a service in the background is not allowed. Is this not a reliable way to check if the app is in the foreground? I thought ProcessLifecycleOwner was suppose to be the answer to the old,

Room does not insert data to table

自闭症网瘾萝莉.ら 提交于 2019-12-13 16:11:18
问题 It is interesting point that it sometimes performs insert operation correctly. I do not know why and how this situation can be happens. So, I could not figure out where I made a mistake(s). Here is my project files. 1) SentFilesDao.java @Dao public interface SentFilesDao { @Query("SELECT id, name, type, status, dateTime FROM sent") LiveData<List<SentFilesEntity>> getAllSentFiles(); @Insert(onConflict = OnConflictStrategy.IGNORE) void insert(SentFilesEntity file); @Query("DELETE FROM sent")

Observing viewmodel for the second time returns null in android

≯℡__Kan透↙ 提交于 2019-12-13 13:06:57
问题 In my android app,im following architecture components with mvvm pattern. my app makes a network call to display the weather information.api call is being made from repository which returns a livedata of response to the viewmodel,which inturn is observed by my main activity. the app works fine except for one condition,whenever i disconnect the internet to test the fail case,it inflates error view as required in the error view i have a retry button,which makes the method call to observe the

Room database, SELECT * FROM table by a given day

╄→尐↘猪︶ㄣ 提交于 2019-12-12 20:27:05
问题 I'm using Room Persistence Library in my Android project, and store date using date type converter: object DateConverter { @TypeConverter @JvmStatic fun fromTimestamp(value: Long?): Date? = if (null == value) null else Date(value) @TypeConverter @JvmStatic fun dateToTimestamp(date: Date?): Long? = date?.time } How could I select from my table by a given day using using Room DAO? 回答1: @Query("SELECT * FROM table_name WHERE date BETWEEN :dayst AND :dayet") Object getFromTable(long dayst, long