Hiding DataBaseHelper & DAOs from my Activity - android

I want to call CRUD operations on Order objects in my Activity. I was wondered is the following implementation of a "Service" class a good way to do this? I don't want any reference to DatabaseHelper or DAO objects in my Activity code as I don't think this would be desireable.
Here is my Service class
public class OrderService
{
private static OrderService instance;
private static Dao<Order, Integer> orderDAO;
static public void init(Context ctx) {
if (null == instance) {
instance = new OrderService(ctx);
}
}
public static OrderService getInstance() {
return instance;
}
private OrderService(Context ctx) {
DatabaseHelper helper = DatabaseHelper.getInstance(ctx);
helper.getWritableDatabase();
orderDAO = helper.getOrderDao();
}
public Order getOrderWithId(int orderId) {
Order myOrder = null;
try {
myOrder = orderDAO.queryForId(orderId);
} catch (SQLException e) {
e.printStackTrace();
}
return myOrder;
}
public Order neworder(Order order) {
try {
orderDAO.create(order);
} catch (SQLException e) {
e.printStackTrace();
}
return order;
}
public void deleteorder(Order order) {
try {
orderDAO.delete(order);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateorder(Order order) {
try {
orderDAO.update(order);
} catch (SQLException e) {
e.printStackTrace();
}
}
public List <Order> getordersForCategory(int orderId) {
List <Order> orders = null;
try {
orders = orderDAO.queryForAll();
} catch (SQLException e) {
e.printStackTrace();
}
return orders;
}
}
and here is how I intend to use the service
public class OrderProcessingActivity extends Activity {
int orderID;
private Order order;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myview);
order = OrderService.getInstance().getOrderWithId(orderID);
......
Does this look like a good way to access the SQLlite DB ?
I have read about "Service" implementations that can be configured in Android so I was sondered is this something that I should be using instead?

Despite moving your database logic to a different class, you're doing all of your database operations in the UI thread, which is not ideal. Also note that even though your class is called "service" it doesn't inherit from any of the Service classes in Android.
One alternate approach would be to do your database operations from the doInBackground method of an AsyncTask, return your needed data from that method. Then, use the returned data to update your activity in the onPostExecute method.

This is more or less the approach I take. My application architecture typically looks like this:
Activity <--> Service <--> DAO <--> SQLite
This looks pretty close to what you have, so I'd say it looks good! I normally don't implement it as a singleton, however, as I don't like to keep the same Context around for the entire lifetime of the app. Instead, I pass in the Context to create a service from each Activity.

Related

Android DBFlow does not save objects

I have a database like this:
#Database(name = QuestionDatabase.NAME, version = QuestionDatabase.VERSION)
public class QuestionDatabase {
public static final String NAME = "QuestionDatabase"; // we will add the .db extension
public static final int VERSION = 1;
}
and a table like this:
#Table(database = QuestionDatabase.class)
public class Question extends BaseModel {
#PrimaryKey
public int localID;
#Column
public int Id;
#Column
public String Answer;
#Column
public String ImageURL;
#Column
public boolean IsFavorite;
#Column
public boolean IsSolved;
}
and an asynctask to retrive data from server:
public class QuestionRetriever extends AsyncTask<Integer, Void, Integer> {
private Activity callerActivity;
private QuestionAdapter questionsAdapter;
private List<Question> callerQuestions;
private Integer pageSize = 10;
public QuestionRetriever(Activity callerActivity, QuestionAdapter questionsAdapter, List<Question> questions){
this.callerActivity = callerActivity;
this.questionsAdapter = questionsAdapter;
this.callerQuestions = questions;
}
#Override
protected Integer doInBackground(Integer... pageNumbers) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.1.33:313/")
.addConverterFactory(GsonConverterFactory.create())
.build();
QuestionWebService service = retrofit.create(QuestionWebService.class);
Call<List<Question>> call = service.getQuestionsPaged(pageNumbers[0].toString(), pageSize.toString());
try {
Response<List<Question>> excecuted = call.execute();
List<Question> questions = excecuted.body();
FastStoreModelTransaction
.insertBuilder(FlowManager.getModelAdapter(Question.class))
.addAll(questions)
.build();
callerQuestions.addAll(questions);
callerActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
questionsAdapter.notifyDataSetChanged();
}
});
//Get TotalQuestionCount if not yet
if (((StatefulApplication) callerActivity.getApplication()).getQuestionCount() == -1){
Call<Integer> call2 = service.getQuestionsSize();
try {
((StatefulApplication) callerActivity.getApplication()).setQuestionCount(call2.execute().body());
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e){
e.printStackTrace();
}
return 1;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
//TODO: show loader
}
#Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
//TODO: hide loader
}
}
as you see every thing seems ok and eve after running FastStoreModelTransaction nothing wrong happens. no errors.
the init job is done in splash screen activity like this:
private void initializeEveryRun() {
//Initializing DBFlow
//DBFlow needs an instance of Context in order to use it for a few features such as reading from assets, content observing, and generating ContentProvider.
//Initialize in your Application subclass. You can also initialize it from other Context but we always grab the Application Context (this is done only once).
FlowManager.init(new FlowConfig.Builder(getApplicationContext()).build());
}
any idea about what should cause this problem or any solution to try?
TG.
I found the answer!!!
As you see in the model, the Id is the identifier of the object retrieved from server and LocalId is the auto-increment identifier that is stored locally. This was the problem. I've used the Id field as Primary Key and added a field named OnlineId for server side identifer and everything is ok now.
Is this a bug or I was using that wrong?
TG.
This is not execute transaction, it's just transaction creation.
As you can see it this test DBFlow - FastModelTest.kt.
FastStoreModelTransaction
.insertBuilder(FlowManager.getModelAdapter(Question.class))
.addAll(questions)
.build();
You must execute your transaction like this :
FlowManager.getDatabase(QuestionDatabase.class).executeTransaction(<<YourTransaction>>);
Otherwise, if you already had a DatabaseWrapper instance you can do <<YourTransaction>>.excute(<<YourDatabaseWrapper>>);.

Android ORMLite slow create object

I am using ormLite to store data on device.
I can not understand why but when I store about 100 objects some of them stores too long time, up to second.
Here is the code
from DatabaseManager:
public class DatabaseManager
public void addSomeObject(SomeObject object) {
try {
getHelper().getSomeObjectDao().create(object);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public class DatabaseHelper extends OrmLiteSqliteOpenHelper
public Dao<SomeObject, Integer> getSomeObjectDao() {
if (null == someObjectDao) {
try {
someObjectDao = getDao(SomeObject.class);
} catch (Exception e) {
e.printStackTrace();
}
}
return someObjectDao;
}
Any ideas to avoid this situations?
Thanks to Gray!
Solution is, as mentioned Gray, using callBatchTasks method:
public void updateListOfObjects (final List <Object> list) {
try {
getHelper().getObjectDao().callBatchTasks(new Callable<Object> (){
#Override
public Object call() throws Exception {
for (Object obj : list){
getHelper().getObjectDao().createOrUpdate(obj);
}
return null;
}
});
} catch (Exception e) {
Log.d(TAG, "updateListOfObjects. Exception " + e.toString());
}
}
Using this way, my objects (two types of objects, 1st type - about 100 items, 2nd type - about 150 items) store in 1.7 sec.
See the ORMLite documentation.

Saving Parcelable data

I have a class which implements Parcelable, and could not implement Serializable because it contains some basic Android classes that I can not modify.
Some of the objects in this class are for example Location and PendingIntent (which are all conveniently Parcelable).
My problem is saving this information between the instances of my main Activity.
Currently, I'm holding a static reference to this class, which works well. But I assume that when I re-install the app, and probably when updates will come around, I won't be able to trust that this static member won't be re-initialized.
I tried to write this Parcelable to a file, but using marshall() is not always working (I'm getting Binder can't be marshalled error).
How can I safely save this information?
Thanks
Using static in your example leads to memory leaks and is not a good way to do anything.
I suggest using static only in 3 cases:
static final String or int - constants
on inner classes (so that they don't contain reference to outer class)
on util or in some cases (like CustomFragment.newInstance) factory methods
The question is why would you want to persist PendingIntent? Its usecase is for inter-process-communication.
I use a StateControl class to handle reading/writing to disc:
public class StateControl {
Context mContext;
Thread worker;
WriteObjectToFile writer;
// StateControl Constructor
public StateControl(Context context) {
mContext = context;
// Construct a writer to hold and save the data
writer = new WriteObjectToFile();
// Construct a worker thread to handle the writer
worker = new Thread(writer);
}// end of StateControl constructor
// Method to save the global data
public void saveObjectData(Object object, String key) {
if (object == null){
// I had a different action here
} else {
// Write the data to disc
writer.setParams(new WriteParams(object, key));
worker.run();
}
}// end of saveGlobalData method
// Method to read the Global Data
public Object readObjectData(String key){
Object returnData = (Object) readObjectFromFile(key);
if (returnData == null){
// I had a different action here
} else {
return returnData;
}
}// end of readGlobalData method
// Method to erase the Global data
public void clearObjectData(String key){
writer.setParams(new WriteParams(null, key));
worker.run();
}// end of clearGlobalData method
private class WriteObjectToFile implements Runnable {
WriteParams params;
public void setParams(WriteParams params) {
this.params = params;
}
public void run() {
writeObjectToFile(params.getObject(), params.getFilename());
}
private boolean writeObjectToFile(Object object, String filename) {
boolean success = true;
ObjectOutputStream objectOut = null;
try {
FileOutputStream fileOut = mContext.openFileOutput(filename, Activity.MODE_PRIVATE);
objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(object);
fileOut.getFD().sync();
} catch (IOException e) {
success = false;
e.printStackTrace();
} finally {
if (objectOut != null) {
try {
objectOut.close();
} catch (IOException e) {
// do nothing
}
}// end of if
}// End of try/catch/finally block
return success;
}
}// end of writeObjectToFile method
private Object readObjectFromFile(String filename) {
ObjectInputStream objectIn = null;
Object object = null;
try {
FileInputStream fileIn = mContext.getApplicationContext().openFileInput(filename);
objectIn = new ObjectInputStream(fileIn);
object = objectIn.readObject();
} catch (FileNotFoundException e) {
// Do nothing
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (objectIn != null) {
try {
objectIn.close();
} catch (IOException e) {
// do nowt
}
}
}
return object;
}
private static class WriteParams {
Object object;
String filename;
public WriteParams(Object object, String filename) {
super();
this.object = object;
this.filename = filename;
}
public Object getObject() {
return object;
}
public String getFilename() {
return filename;
}
}
}
Then invoke the public methods to kick off the writing/reading. For this version, I also having it taking place in a separate thread, but you could modify that if you needed to.
Binder
Most developers will not implement this class directly, instead using
the aidl tool to describe the desired interface, having it generate
the appropriate Binder subclass.
from the official documentation
Do you need to store the Binder object with the rest of your object? Maybe you can save your object without the Binder instance, and re-create the Binder object with aidl after you restore the object

Can I use ORMlite/SQLlite outside of a containing application

I want to unit test a DatabaseService class I have created ( Modified a WishList application I found online )
package com.test.db;
import java.sql.SQLException;
import java.util.List;
import android.content.Context;
import com.test.model.WishList;
public class WishListService
{
private static WishListService instance;
public static void init(Context ctx)
{
if (null == instance)
{
instance = new WishListService(ctx);
}
}
static public WishListService getInstance()
{
return instance;
}
private DatabaseHelper helper;
private WishListService(Context ctx)
{
helper = DatabaseHelper.getInstance(ctx);
}
public List<WishList> getAllWishLists()
{
List<WishList> wishLists = null;
try
{
wishLists = helper.getWishListDao().queryForAll();
} catch (SQLException e)
{
e.printStackTrace();
}
return wishLists;
}
public void addWishList(WishList l)
{
try
{
helper.getWishListDao().create(l);
} catch (SQLException e)
{
e.printStackTrace();
}
}
public WishList getWishListWithId(int wishListId)
{
WishList wishList = null;
try
{
wishList = helper.getWishListDao().queryForId(wishListId);
} catch (SQLException e)
{
e.printStackTrace();
}
return wishList;
}
public void deleteWishList(WishList wishList)
{
try
{
helper.getWishListDao().delete(wishList);
} catch (SQLException e)
{
e.printStackTrace();
}
}
public void updateWishList(WishList wishList)
{
try
{
helper.getWishListDao().update(wishList);
} catch (SQLException e)
{
e.printStackTrace();
}
}
}
My question is is there any way to instantiate the DatabaseHelper without having to create a test activity and pass it in as the Context that DatabaseHelper requires?
Ideally I want to Unit test this class as a standard JUnit test , not an Android JUnit test
I'm not sure this is a good idea. ORMLite has non-Android JDBC package that you could use to drive Sqlite directly via JDBC. However if the goal is to test your Android database classes, you will be running a lot of non-android code which would, in my view, invalidate the tests. I'm considered mocking out all of the Android classes but it starts to become a maze of twisting passages before long.
I think your best way to proceed would be to put up with the Android junit tests. Take a look at the ORMLite Android test package files for examples. The [BaseDaoTest file] has all of the setup/shutdown methods that you can customize for your own tests.

Auto delete nested objects in ORMLite

I have these to classes:
public class Station {
#DatabaseField(foreign = true, foreignAutoCreate = true)
private OpeningTimes openingTimes;
}
public class OpeningTimes {
#DatabaseField(generatedId = true)
int _id;
}
Now OpeningTimes row is auto created, when I call createOrUpdate method on StationDao. That's great!
I would be also thankful, if I could delete Station object and its nested object OpeningTimes automatically.
Now I have to do it this way in Station class and it seems quite confusing. Is there any more elegant way?
public void deleteFromDb(DatabaseHelper dbHelper) {
try {
openingTimes.deleteFromDb(dbHelper);
dbHelper.getStationDao().delete(this);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
EDIT:
I have been trying also this, but with SQL Statement errors
#DatabaseField(foreign = true, foreignAutoCreate = true, columnDefinition="INTEGER, FOREIGN KEY(`openingTimes_id`) REFERENCES openingtimes(`_id`)")
I would consider doing this at the DAO level instead of at the persisted object level. What I recommend is creating your own StationDao interface and your own StationDaoImpl implementation. The ORMLite docs an example of this.
public interface StationDao extends Dao<Station, Integer> {
// we will just be overriding some of the delete methods
}
Then create your implementation which would override the delete() method and delete any children objects. Something like the following:
public class StationDaoImpl extends BaseDaoImpl<Station, Integer>
implements StationDao {
private final Dao<OpeningTimes, Integer> openTimesDao;
public AccountDaoImpl(ConnectionSource connectionSource) throws SQLException {
super(connectionSource, Station.class);
openTimesDao = DaoManager.createDao(connectionSource, OpeningTimes.class);
}
#Override
public int delete(Station station) throws SQLException {
if (station.openTimes != null) {
openTimesDao.delete(station.openTimes);
}
return super.delete(station);
}
}
If you are using your own DAO then you would have to make sure it is configured using #DatabaseTable(daoClass = StationDaoImpl.class).

Categories

Resources