Using Room in CryptoOracle
- The application can still work and show persisted data when there is no internet connection.
- The network traffic is reduced because requests are sent from the application only when the data is outdated and new data is available to be persisted.
- name of the cryptocurrency
- date for which the other information in the object is
- value representing the real close price for the day
- value representing the prediction of the close price for this day
public class Coin {
private String cryptoCurrency;
private double realValue;
private double predictedValue;
private String date;
}
The next step is to tell Room that this class is going to be used as a table schema. To do this we annotate the class with the @Entity annotation.
@Entity
public class Coin {
private String cryptoCurrency;
private double realValue;
private double predictedValue;
private String date;
}
@Entity
public class Coin{
@PrimaryKey
@NonNull
private String id;
private String cryptoCurrency;
private double realValue;
private double predictedValue;
private String date;
}
@Entity
public class Coin{
@PrimaryKey
@NonNull
private String id;
private String cryptoCurrency;
private double realValue;
private double predictedValue;
private String date;
public Coin(String id, String cryptoCurrency, double realValue, double predictedValue, String date) {
this.id = id;
this.cryptoCurrency = cryptoCurrency;
this.realValue = realValue;
this.predictedValue = predictedValue;
this.date = date;
}
@NonNull
public String getId() {
return id;
}
public void setId(@NonNull String id) {
this.id = id;
}
public String getCryptoCurrency() {
return cryptoCurrency;
}
public void setCryptoCurrency(String cryptoCurrency) {
this.cryptoCurrency = cryptoCurrency;
}
public double getRealValue() {
return realValue;
}
public void setRealValue(double realValue) {
this.realValue = realValue;
}
public double getPredictedValue() {
return predictedValue;
}
public void setPredictedValue(double predictedValue) {
this.predictedValue = predictedValue;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
The next step is to create a DAO. DAO stands for data access object. It contains all the methods used for accessing the database. To begin with, we create an interface as shown below. We can also use abstract class instead of interface.
public interface CoinDao {
}
Then we annotate the interface with the @Dao annotation. This way we tell Room to use it to generate an implementation of this interface at compile time when it is referenced by a @Database annotated class.
@CoinDao
public interface CoinDao {
}
Finally we add all the methods used for database interactions. Room comes with some out-of-the-box methods of the basic CRUD operations like save, delete, etc. We can also create custom complex queries as shown below. To do so, we use the @Query annotation and pass a String SQL statement as its parameter. Then we create a method definition corresponding to the SQL statement. Each time we call a DAO’s method the corresponding SQL statement is executed and a result is returned as the defined Java structure. The final two queries return a LiveData object for which we will talk more on a next chapter. But for now, it is an observable holder and it is part of the Android Architecture Components.
@Dao
public interface CoinDao {
@Insert(onConflict = REPLACE)
void save(Coin coin);
@Insert(onConflict = REPLACE)
void saveAll(List coins);
@Delete
void delete(Coin coin);
@Query("DELETE FROM Coin Where cryptoCurrency = :cryptoCurrency")
void deleteAll(String cryptoCurrency);
@Query("SELECT * FROM Coin WHERE date = :date")
LiveData> getPredictionsForToday(String date);
@Query("SELECT * FROM Coin WHERE cryptoCurrency = :cryptoCurrency")
LiveData> getHistoricalData(String cryptoCurrency);
}
The very final thing we need to do is create a Database class responsible for the database creation.
@Database(entities = {Coin.class}, version = 1, exportSchema = false)
public abstract class CryptoDatabase extends RoomDatabase {
public abstract CoinDao coinDao();
private static CryptoDatabase INSTANCE;
public static CryptoDatabase getDatabaseInstance() {
if(INSTANCE == null) {
INSTANCE = Room.databaseBuilder(App.getAppContext(), CryptoDatabase.class, "crypto_mind_database").build();
}
return INSTANCE;
}
public static void destroyDatabase() {
INSTANCE = null;
}
}
Letzte Beiträge
Share :
Share :
Weitere Beiträge
MVVM’s Model implementation in Android application. Room
As we said in the previous article Model is the data in our application. These are classes representing objects that we persist in our database or that we get from network calls to services.
Machine learning concepts. Data preparation
Each machine learning process related to the use of neural networks consists of at least two parts. The first part is related to data loading and preparation for training.
Dependency Injection
Each Object-oriented application consists of many classes that work together in order to solve a problem. However, when writing a complex application, application classes should be as independent as possible.