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
Designing REST API architecture
The web layer is the top layer in the architecture. It is responsible for processing user queries and returning back responses to the user. These operations happen in the controllers.
View Model. Example
In this article we will go through the steps of creating a simple game screen. We will make it the traditional way without using View Model and we will see why it is absolutely wrong to persist data in the View.
Machine learning concepts. Network training and evaluation
The neural network model consists of two layers – an LSTM layer and an output Dense layer. The reason for choosing an LSTM layer is the need to process sequences of time-related data