c# - Azure Offline Sync -


working azure app service offline sync .net backend. working totally fine when directly remove row server database not sync (updated) after clicking refresh button on mobile app if edit name attribute sync automatically after clicking refresh button. far know should automatically update mobile list if delete row server database updates after row edited. any help! can make work proper if row deleted database after refreshing shows new updated database list?

following todoactivity.cs class

 public class todoactivity : activity {     //mobile service client reference     private mobileserviceclient client;      //mobile service sync table used access data     private imobileservicesynctable<todoitem> todotable;      //adapter map items list view     private todoitemadapter adapter;      //edittext containing "new todo" text     private edittext textnewtodo;      const string applicationurl = @"https://my2doservice.azurewebsites.net";              const string localdbfilename = "local.db";      protected override async void oncreate(bundle bundle)     {         base.oncreate(bundle);          // set our view "main" layout resource         setcontentview(resource.layout.activity_to_do);          currentplatform.init();          // create mobile service client instance, using provided         // mobile service url         client = new mobileserviceclient(applicationurl);         await initlocalstoreasync();          // mobile service sync table instance use         todotable = client.getsynctable<todoitem>();          textnewtodo = findviewbyid<edittext>(resource.id.textnewtodo);          // create adapter bind items view         adapter = new todoitemadapter(this, resource.layout.row_list_to_do);         var listviewtodo = findviewbyid<listview>(resource.id.listviewtodo);         listviewtodo.adapter = adapter;          // load items mobile service         onrefreshitemsselected();     }      public async task initlocalstoreasync()     {         // new code initialize sqlite store         string path = path.combine(system.environment.getfolderpath(system.environment.specialfolder.personal), localdbfilename);          if (!file.exists(path)) {             file.create(path).dispose();         }          var store = new mobileservicesqlitestore(path);         store.definetable<todoitem>();          // uses default conflict handler, fails on conflict         // use different conflict handler, pass parameter initializeasync. more details, see http://go.microsoft.com/fwlink/?linkid=521416         await client.synccontext.initializeasync(store);     }      //initializes activity menu     public override bool oncreateoptionsmenu(imenu menu)     {         menuinflater.inflate(resource.menu.activity_main, menu);         return true;     }      //select option menu     public override bool onoptionsitemselected(imenuitem item)     {         if (item.itemid == resource.id.menu_refresh) {             item.setenabled(false);              onrefreshitemsselected();              item.setenabled(true);         }         return true;     }      private async task syncasync(bool pulldata = false)     {         try {             await client.synccontext.pushasync();              if (pulldata) {                 await todotable.pullasync("alltodoitems", todotable.createquery()); // query id used incremental sync             }         }         catch (java.net.malformedurlexception) {             createandshowdialog(new exception("there error creating mobile service. verify url"), "error");         }         catch (exception e) {             createandshowdialog(e, "error");         }     }      // called when refresh menu option selected     private async void onrefreshitemsselected()     {         try         {             await syncasync(pulldata: true); // changes mobile service             await refreshitemsfromtableasync(); // refresh view using local database         }         catch(exception e)         {                createandshowdialog(e, "error");         }     }      //refresh list items in local database     private async task refreshitemsfromtableasync()     {         try {             // items weren't marked completed , add them in adapter             var list = await todotable.where(item => item.complete == false).tolistasync();              adapter.clear();              foreach (todoitem current in list)                 adapter.add(current);          }         catch (exception e) {             createandshowdialog(e, "error");         }     }      public async task checkitem(todoitem item)     {         if (client == null) {             return;         }          // set item completed , update in table         item.complete = true;         try {             await todotable.updateasync(item); // update new item in local database             await syncasync(); // send changes mobile service              if (item.complete)                 adapter.remove(item);          }         catch (exception e) {             createandshowdialog(e, "error");         }     }      [java.interop.export()]     public async void additem(view view)     {         if (client == null || string.isnullorwhitespace(textnewtodo.text)) {             return;         }          // create new item         var item = new todoitem {             text = textnewtodo.text,             complete = false         };          try {             await todotable.insertasync(item); // insert new item local database             await syncasync(); // send changes mobile service              if (!item.complete) {                 adapter.add(item);             }         }         catch (exception e) {             createandshowdialog(e, "error");         }          textnewtodo.text = "";     }      private void createandshowdialog(exception exception, string title)     {         createandshowdialog(exception.message, title);     }      private void createandshowdialog(string message, string title)     {         alertdialog.builder builder = new alertdialog.builder(this);          builder.setmessage(message);         builder.settitle(title);         builder.create().show();     } } 

you need turn on soft delete on server. add following in server initialization code:

domainmanager = new entitydomainmanager<todoitem>(context, request, enablesoftdelete: true); 

soft delete allows notify clients record has been deleted. when delete record, it’s marked deleted. allows other clients download new state , update offline sync cache.

to learn more, see 30 days of zumo.v2 (azure mobile apps): day 19 – asp.net table controllers.


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -