I inherited the project from a very experienced Java developer. The project is Android app and the guy who coded is is very experienced in coding regular Java apps. He introduced some file structures that I haven’t come across before (I haven’t coded Java apps outside Android area). I can follow the code with no problem and I actually have finished a few add-ons so far, but now I would like to know more of why he used such approach since it may help me myself write a better code. On the other hand, if his approach is not suitable for Android apps, I would like to know that as well so I don’t use such structures.
I will divide all questions into points.
-
When dealing with a database, he made 3 layers: entities, data_access and controllers. This is a kind of MVC structure. Is it advised to use it in Android apps?
-
In controller part, he made the following file structure:
controller/ async/ AsyncAddObject.java AsyncExecutorAdd.java AsyncExecutorGetAll.java Entity1Controller.java Entity2Controller.java Entity3Controller.java IController.java
All async files are the generics.
Is this structure familiar to you? I remember executors used in Java, but have never used such approach in Android development. If this is a proper way, what is Executor used for?
What about IController.java
– something usual in regular Java coding?
+++++++++
Then when we want to add some entry into database, he uses the following code
Entity1 ent = new Entity1();
ent.setSomething(someParameter);
AsyncAddObject<Entity1> obj = new AsyncAddObject<Entity1>();
obj.setController(mEntity1Controller);
obj.setObj(ent);
new AsyncExecutorAdd<Entity1>().execute(obj);
Does this look confusing to you? I usually call some database method and pass object to it. For example, I would do it like this
Entity1 ent = new Entity1();
ent.setSomething(someParameter);
MyDatabaseClass.addEntity1(ent); //just an example, adding to database will be done on non-Main thread.
This is it. I apologize for a long post.
2
On Android events are triggered on the main UI thread, but you can not do any work on the main UI thread.
What the developer might be doing is sending the task of addObject to another thread so that the main UI thread can continue.
Looks like he has created a few classes to make these kinds of tasks easy to do. This is good practice, but I am making an assumption. Without seeing the code of these files we won’t know for sure.
In your example;
MyDatabaseClass.addEntity1(ent);
If that is writing to a SQLite database on the Android device, then there is the risk that the database is stored on an SD card. SD cards are a bottleneck for performance on mobile devices, and this will block the main UI thread. You should not be doing any time consuming tasks on that thread.
I’m not even sure if Android will let you do it. There are some cases where the SDK will throw an exception if you attempt to do certain operations in the main thread.
7