I'm working on an Android application in which I have to manage the online / offline synchronization. I'm using a background service in order to listen for network changes. If the network is availbale, the treatments are saved remotely. Else, the treatments are saved locally. For this, I'm using ORMLite.
FIRST STEP:
I'm in my TraitementActivity. When I click on a button, I create the object that I want to save. I'm using an IntentService to do this asynchroniously:
IntentServiceTraitement.startActionSaveRemoteManager(TraitementActivity.this, t);
SECOND STEP:
I'm in my IntentService.
public static void startActionSaveRemoteManager(Context context, TraitementManager tm) {
Intent intent = new Intent(context, IntentServiceTraitement.class);
intent.setAction(ACTION_SAVE_REMOTE_MANAGER);
intent.putExtra("traitement_manager", tm);
context.startService(intent);
}
public IntentServiceTraitement() {
super("IntentServiceTraitement");
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_SAVE_REMOTE_MANAGER.equals(action)) {
TraitementManager tm = intent.getParcelableExtra("traitement_manager");
handleActionSaveRemoteManager(tm);
}
}
}
private void handleActionSaveRemoteManager(TraitementManager obj) {
if (obj != null) {
boolean res = mRestService.saveTraitementManager(obj);
if (!res) {
saveLocalTraitementManager(obj);
} else {
mResultReceiverTraitement.send(3, null);
}
}
}
The startService() method calls the OnStartCommand() method which calls finally the handleActionSaveRemoteManager() method.
THIRD STEP:
I'm in handleActionSaveRemoteManager(Traitement obj) method. In this method, I try to save my object (obj) remotely (I'm using a rest client with Spring for Android). If the network is available and if my object is inserting successfully in remote database, I do anything. However, if the network is unvailable and if the remote insertion failed, I have to save my object locally.
FOURTH STEP:
I'm in my background service: When the network is again available, my background service receive a notification and I call a method in order to save remotely the objects wich are present in my local DB:
List<TraitementManager> lstManager = mLocalManager.getAllLocalTraitementsManager();
for (TraitementManager tm : lstManager) {
IntentServiceTraitement.startActionSaveRemoteManager(this, tm);
}
Like in my Activity, I'm calling my IntentService to try to save my objects remotely.
Here is my local DAO class:
public class TraitementManagerDAO extends BaseDaoImpl {
public TraitementManagerDAO(ConnectionSource connectionSource) throws SQLException {
super(connectionSource, TraitementManager.class);
}
public boolean saveLocal(TraitementManager obj) {
try {
if (create(obj) == 1) {
return true;
}
return false;
} catch (SQLException e) {
Log.e("ERROR SAVE", e.toString());
e.printStackTrace();
return false;
}
}
public List<TraitementManager> getAllLocal() {
List<TraitementManager> obj = null;
try {
obj = queryForAll();
deleteAllLocal(obj);
} catch (SQLException e) {
e.printStackTrace();
}
return obj;
}
public boolean deleteAllLocal(List<TraitementManager> obj) {
try {
for (TraitementManager tm : obj) {
delete(tm);
}
return true;
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
MY PROBLEM IS THE FOLLOWING:
I achieved to save objects locally but deleting them doesn't work ... When I call the getAllLocal() method (TraitementManager class), I call another method: deleteAllLocal(). But it doesn't work. So I can save objects in local db but I can't delete them.
I have used several methods. I have tried to delete objects by id but it doesn't work. I have checked my DatabaseHelper and my DataBaseManager (configuration classes for ORMLite) I have checked permissions in the manifest. I have no error in the logcat.
I'm using this dependency for ORMLite: compile 'com.j256.ormlite:ormlite-android:4.48'
Any ideas ?
Thanks for advance,
Aucun commentaire:
Enregistrer un commentaire