mardi 24 février 2015

Android Azure Offline Mobile Service: How to make transactions?

is there a way to make a transaction when pulling from multiple tables ?


For example , I have a table of polls and questions. If I pull the poll but I lost connection when I download the questions, then the ideal scenario should be rollback the SQLite database to maintain consistence. Else, there would be incomplete polls stored on the database.


Here is part of my code(By the way, I'm using Azure Mobile Service SDK 2.0.1-Beta)



protected void doInBackground()
{
MobileServiceTable<Poll> mPoll;
MobileServiceTable<Questions> mQuestions;
MobileServiceSyncTable<Poll> msPoll;
MobileServiceSyncTable<Questions> msQuestions;
...

//Queries start here

Query q;
Log.d(TAG,"Pulling from poll");
q=mPoll.where().field("company_id").eq(val(company_id));
msPoll.pull(q).get(TIMEOUT, TIMEOUT_UNIT);

//Get all the polls pulled by the previous operation
MobileServiceList<Poll> res=msPoll.read(mPoll.where()).get(TIMEOUT, TIMEOUT_UNIT);
if(isCancelled())
{
return false;
}

for(Poll row:res)
{
Log.d(TAG,"Pulling from questions where poll_id="+row.getId_poll());
q=mQuestions.where().field("poll_id").eq(val(row.getId_poll()));
msQuestions.pull(q).get(TIMEOUT, TIMEOUT_UNIT);
if(isCancelled())
{
return false;
}
...
}


What I've tried before was get the SQLite database for my table and then surround the query with transaction method like this:



protected void doInBackground()
{
db.beginTransaction();
try
{
//Poll query and Question query like above.
db.setTransactionSuccessful();
}finish{
db.endTransaction();
}
}


But the methods from Azure Mobile Service stop working because the database is locked.


I hope someone could post a way to do transactions on Azure Mobile Service.


Thanks!


Aucun commentaire:

Enregistrer un commentaire