This is a companion discussion topic for the original entry at https://www.raywenderlich.com/8984-mvvm-on-android/lessons/4
This is a companion discussion topic for the original entry at https://www.raywenderlich.com/8984-mvvm-on-android/lessons/4
Hi
In the CreaturemonApplication
we have the following code:
companion object {
lateinit var database: CreatureDatabase
}
That means that the database
dependency will be static available for the hole app. Once we have it on the Application
class.
My question is: that is the best approach for sharing dependencies ?
Thanks for the question, Amadeu!
The best approach is probably to use dependency injection to give the repository access to the database and the DAOs. We’re going to take a look at dependency injection in a future course. Both with a framework like Dagger and also without, e.g. you can pass the database into the repository constructor as a property.
In the the Creature class, I had to change val to var in order to run the app.
error: Cannot find setter for field.
private final com.raywenderlich.android.creaturemon.model.CreatureAttributes attributes = null;
^
Otherwise I got the above error.
It would be great to see this code working with coroutines instead of AsyncTask.
Also with this lesson we break CreatureGeneratorTest with
warning: There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate unwanted constructors. public final class Creature {
Does this mean that changing @PrimaryKey @NonNull val name: String = "" to @PrimaryKey @NonNull val name: String
is okay?
Thanks for the suggestion @arcanewater! When we next update the course, we’ll switch to coroutines for sure, especially now that AsyncTask
has been officially deprecated.
On your question, your change sounds like it will work, but I’ve not tried it. Let us know if you see any issues when making that change. Otherwise, try the @Ignore
annotation as suggested in the warning.
Thanks again!
Thanks for answer, the solution i posted it works, but i am not sure where to include @Ignore just to try it out at the data class empty constructor or?
Hi @arcanewater. Generally annotations like @Ignore
are put on either the class or method that is showing the warning. Let me know if that works if you have a chance. Thanks!