What's the best way to do Mobile & Server Sync?

I have one application that has a data sync part involved, I am currently doing by sending data in XML form via NSMutableURLRequest and receiving via initWithContentsOfURL.

But as the amount of data becomes huge, the time for synchronization also increases.

If anyone has any idea how best to implement this, or any changes made to the implementation above. Thus, synchronization time is reduced.

I wanted to know the best approach / model for doing Syncing.

Currently I am Following This method..
<ROOT>
<ADD></ADD>
<UPDATE></UPDATE>
<DELETE></DELETE>
</ROOT>

      

I am not asking how to establish a connection.

In my application, user can add, update and remove from iphone as well as from server website. I want to know how to sync data correctly (correct data structure in Xml). therefore, the data user can see the record there from both sides. Thanks in advance...

+3


source to share


5 answers


Since I don't have the correct answer, which is what I expected. I think I was not able to ask my question correctly. Let me point out my current implementation.

1) User Enter some data (here means Daily Schedule Task ) from the application.

2) The same functionality is provided on the website.

3) When a user enters any task from the app, he can see the same task in the portal (I am doing a background sync).

4) User can add, edit, delete a task from both sides.

5) In the following way I processed the task "Add, change and delete".

I have Status Column in the table.  

 Status = 0 means task is sync on both side.
 Status = 1 means newly added task
 Status = 3 means user deleted the task which was on server.

   a)when user add any task I insert that task with Status = 1.
    When I sync the task I extract all the task with status = 1.
   Put the task in ADD tag.

   b)On server side task is added in the database and it again send back to app with unique primary id. (needed when user edit any task.) I insert that task to my table with status = 0.

   c)Now when user edit any task I first check the status of task, if its Status = 1,I keep the status as 1. but status is 0 i changed that status = 2.

   d)If user delete any task I check the status if its status = 1 I delete that task, if its 0 or 2 I changes its status to 3.

  Now when Sync Method is Called, I search through Table 

      



Place the entire task in the Add tag, which has status = 1.

Place all tags in the Edit tag that have status = 2.

Place all tags in the Delete tag that have status = 3.

Now server side This Xml parses and inserts, updates, removes by tag.

The same method I mentioned above is done on the server side and Add Edit Delete xml. I am parsing the task on the application side by inserting, updating and deleting according to the xml.

So my question is how to improve this type of synchronization model. Anyone knows that it is better to synchronize the model, in which on both sides (site and application) the user can add, edit, delete.

+1


source


You might want to check out AFNetworking . It is very easy to use and has very good performance. You can also use inline XML requests to send / receive XML data asynchronously and process it however you want.



If you have a question about how to do certain things, do not hesitate to ask.

+2


source


//Better and effective to use the asynchronous request to the server that will cost less time and even your UI will be remained active

NSLog(@"Server Path:%@",dataUrl);
//connection
NSURLConnection *theConnection;
NSMutableURLRequest *Request = [NSMutableURLRequest requestWithURL:dataUrl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]; 
[Request setHTTPMethod:methodName]; 
theConnection = [[NSURLConnection alloc] initWithRequest:Request delegate:self];

      

+1


source


The Divide and Conquer Rule can provide an idea for solving almost all problems :)

Here I have split your procedure into two steps.
1. Loading data.
2. Analyze data to use or can be a repository of persistence.

                        Now you can do this if you use an "asynchronous" connection (ie NSURLConnection

or any third party library). In - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

(i.e. NSURLConnection Delegation Method) you will get some chunk of data for it to parse asynchronously. This way you can just reduce the parsing time to almost zero. For asynchronous parsing, you can use the libXml library.

Here is a sample APPLE code. Check out the part LibXml

, it does the same as before. Briefly practical from my explanations: Code example

You used the word "Data Synchronization" so I have not included anything related to UI concepts.

Update : For newbies who don't know how to sync with the server. Here's a series of great tutorials: How to sync

0


source


You have 3 possible problems using XML to transfer large amounts of data to an iOS device. First, you have the size of the data itself. Second, you have post-data processing time. Third, you have connection latency (about which little can be done if you don't control both ends). I'll address all three, but focus on data size since you have problems with.

Data size

If big data is your problem, compression is your friend. One of the ways I've had a lot of success with it is to pin your data in memory and then base64, zip-encode that data so that it doesn't have odd characters in transit. I've used this with generic binary data as well as specific hard data formats. Here is a related snippet and you can see this method as it is used to package data here :

NSString* encodedStr = nil;
NSData *bufferData = [dict objectForKey:@"someKey"];
if (bufferData)
{
    data = [LFCGzipUtility gzipData:bufferData];
    len = [data length];
    char* byteData2 = (char*)malloc(len);
    memcpy(byteData2, [data bytes], len);
    encodedStr = base64_encode(byteData2, len);
    free(byteData2);
}           

      

You can cancel the process to get the data back. It can be used with just about any data type, not just XML, although the more compression benefits you get will help you. The text compresses well - it works great for XML.

In addition, you can change data formats. After reusing XML, I switched to json as it is often more compact and easier to work with. XML is superior to generic solutions, but if you don't need something strictly generic, it might be overkill. My favorite joke: XML is violent. If that doesn't solve your problem, you're not using it enough. = Also, Apple and third parties have good json support on iOS.

Finally, for the data itself, make sure you are not submitting unnecessary data over and over again. device caching is your friend here. Break your data into reusable chunks where possible. I know this is not always possible, but it can definitely change the meaning for large datasets.

Time of processing

Make sure you are using tools to profile your calls and that the actual data transfer is a problem not handling the data once it is received. NSXMLParser can be a little tricky to wrap your head around at first glance, but it's very efficient at what it does and can be very fast in addition to playing with your memory. Also, sending unnecessary data over and over again, or storing data on disk in many small operations, rather than large reads or writes, can lead to performance issues.

Delay

Often you cannot control this factor, but if it is a problem, you can limit the number of repeated net calls and download more data in fewer net calls.

Finally this link has a good record of some of the more technical aspects that might interest you.

0


source







All Articles