Silverlight client memo → server data synchronization memo code

Source: Internet
Author: User
# Region  Synchronization unit  Void  Syncunit () {progresscontent =  "Synchronization unit ..."  ;  VaR  Query = context. getunitsquery (). Where (P => P. shopuniqueid =  App  . Currentshop. uniqueid); context. Load (query, loadunitcallback,  Null  );}  Void  Loadunitcallback (  Loadoperation <  Rp_unit  > Loadop ){  // Server data  Ienumerable  <  Rp_unit  > Serverunits = context. rp_units;  // Client data  Ienumerable  <  Rp_unit  > Clientunits;  // Obtain data  Using  ( VaR  Helper =  New  Localdb.  Unithelper  () {Clientunits = helper. getlist ();}  // Obtain the intersection to synchronize the existing and updated objects.  VaR  Intersectunits = serverunits. Intersect (clientunits,  New  Unitentitycompare  ());  // Traverse the Intersection Set  Foreach  ( VaR  Item  In  Intersectunits ){  // Obtain the data to be updated on the server and the client based on the items in the intersection.  VaR  Serverunittoupdate = serverunits. First (P => P. uniqueid = item. uniqueid );  VaR  Clientunittoupdate = clientunits. First (P => P. uniqueid = item. uniqueid );  // Compare the update time. If the update time is the same, ignore the operation.  If  (Clientunittoupdate. updatedate! = Serverunittoupdate. updatedate ){ // Update the client if the server is newer  If  (Clientunittoupdate. updatedate <serverunittoupdate. updatedate ){  Using  (  VaR  Helper =  New  Localdb.  Unithelper  () {Helper. updateunit (serverunittoupdate );}}  // Otherwise, update the server  Else {Serverunittoupdate. updatedate = clientunittoupdate. updatedate; serverunittoupdate. namecn = clientunittoupdate. namecn; serverunittoupdate. nameen = clientunittoupdate. nameen ;}}}  // Obtain the difference set between the server and the client to synchronize entities that do not exist on the server or client.  VaR  Effectserver = serverunits. clientt (clientunits,  New  Unitentitycompare  ());  // Traverse the difference set. // you do not need to use foreach because you want to modify the set.  For  (  Int I =  0  ; I <effectserver. Count (); I ++ ){  VaR  Item = effectserver. elementat (I );  // If the last local update time is before the data update time, add the data to the client  If  (! Clientshopinfo. lastupdatedate. hasvalue | clientshopinfo. lastupdatedate <servershopinfo. lastupdatedate ){  Using  (  VaR  Helper =  New  Localdb. Unithelper  () {Helper. addunit (item );}}  // Otherwise, the data has been deleted from the local database and deleted from the server database.  Else  {Context. rp_units.remove (item );}}  // Obtain the difference set between the server and the client to synchronize entities that do not exist on the server or client.  VaR  Required tclient = clientunits. Capacity T (serverunits,  New  Unitentitycompare  ());  // Traverse the difference set. // you do not need to use foreach because you want to modify the set.  Foreach (  VaR  Item  In  Required tclient ){  // If the last local update time is before the data update time, remove the data from the client  If  (Clientshopinfo. lastupdatedate <servershopinfo. lastupdatedate ){  Using  (  VaR  Helper =  New  Localdb.  Unithelper () {Helper. deleteunit (item );}}  // Otherwise, the client data is added to the server.  Else  {Context. rp_units.add (item) ;}} syncoperations. Remove (  "Unit"  );}  # Endregion 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.