3

I have one app in which data sync part is involved, Currently I am doing by sending data in the form of XML through NSMutableURLRequest and receving through initWithContentsOfURL.

But as the amount of data getting huge,time to sync is also increasing.

If anyone have idea how to implement this in better way,or any changes made to above implementation. So that sync time is reduced.

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

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

I am not asking about how make connection..

In my app user can add,update & delete from iphone as well as from server means website. I want to know how to sync the data in proper way (Proper Structure of Data in Xml). so data user can see there entry on the both side. Thanks in advance...

Rupesh
  • 2,061
  • 5
  • 22
  • 36

5 Answers5

2

You might want to check AFNetworking. Very easy to use and it has very good performance. You can also use the built in XML requests to send/receive XML data asynchronously and handle them as you please.

If you have any question on how to do certain things feel free to ask.

Y2theZ
  • 10,162
  • 38
  • 131
  • 200
  • Are you affiliated with AFNetworking in any way? Also, you are aware that the OP was not asking for a library recommendation, per se, right? – Andrew Barber Sep 10 '12 at 13:06
  • no I am not affiliated with AFNetworking, it just came to my mind and I just thought I throw it here in case he is interested. no need to get mad about it. I can delete the answer if it affects you – Y2theZ Sep 11 '12 at 07:37
1

As I have not got the right answer what i was expecting. I think I was not able to put my question Properly. Let me specify my Current Implementation.

1)User Enter some Data (Here means Task of Daily Schedule) from the app.

2)Same funcnality is provided from website.

3)When User Enter any Task from App he can see same task on Portal (I do background Syncing).

4)User Can Add,Edit,Delete Task From both Side.

5)In the Following Way I handled the Add,Edit & Delete Task.

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 

Put All the Task in Add tag which has status = 1.

Put All the Task in Edit tag which has status = 2.

Put All the Task in Delete tag which has status = 3.

Now on server side This Xml is parse and its inserted, updated , Deleted according to tag.

The same method which I have mentioned above is followed on server side, and Add Edit Delete xml is created. I parser the task on app side do the insertion, updation and Deletion according to xml.

So My question was How do I improve this type of Sync Model. Any one Knows better syncing Model in which from both side (Website & App) user can add,edit,delete.

Rupesh
  • 2,061
  • 5
  • 22
  • 36
1
//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];
Kuldeep
  • 2,589
  • 1
  • 18
  • 28
  • Yes i am using the same way to request to server – Rupesh Mar 22 '12 at 07:39
  • 1
    @kuldeep: what do you mean by "that will cost less time"? Do you mean that asynchronous are faster that synchronous service call? Benefit of using Asynchronous calling is that UI remains active and that make better user experience. In synchronous calling UI just hang until you receive data. – Iducool Sep 11 '12 at 05:15
0

"Divide and conquer" rule can give idea for solution of almost all problems :)

Here I have divided your procedure into two steps.
1. Downloading data.
2. Parsing data for use or might be persistence storage.

          Now you can achieve possible speed if you can do both of these in parallel. You can do these if you will use "Asynchronous" connection(i.e. NSURLConnection or any third party library). In - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data (i.e. Delegate method of NSURLConnection) you will receive some data chunk so parse it asynchronously. In this way you can merely decrease the parsing time to almost zero. For asynchronous parsing you can use libXml library.

Here is a APPLE's sample code. Check the portion of LibXmlit is doing the same as I explained earlier. In short practical of my explanation:Sample Code

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

Update: For beginners who don't know how to do synch with server. Here is a series of great tutorials: How to synchornize

Iducool
  • 3,543
  • 2
  • 24
  • 45
0

You have 3 possible problem points with using XML to transfer large amounts of data to an iOS device. First you have the size of the data itself. Second you have the processing time once the data is received. Third you have the latency of the connection (which there is little you can do about unless you control both ends). I'll address all three, but focus on the size of the data since that's what you seem to be having trouble with.

Data Size

If large datasets are your problem compression is your friend. One method I have had great success with is zipping your data in memory and then base64 encoding this zipped data so that it won't have odd characters in transfer. I've used this with generic binary data as well as with specific rigid data formats. Here is the related snippet and you can see this method as it is used for packaging the 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 reverse the process to get the data back out. This can be used with just about any data type, not just XML, though the more compression benefits you get the better this will help you. Text compresses well--it works great for XML.

Additionally you can change data formats. After using XML many times, I have moved to json as it is often more compact and easier to work with. XML excels for generic solutions but if you don't need something strictly generic it can be overkill. My favorite tech joke: XML is like violence. If it's not solving your problem, you're not using enough of it. =) Additionally Apple and 3rd parties have good support for json on iOS.

Lastly for the data itself, make sure you are not sending unnecessary data over and over. on-device caching is your friend here. Break up the data into reusable pieces where possible. I know this isn't always possible, but it can definitely make a difference for large data sets.

Processing time

Make sure that you use instruments to profile your calls and that the actual data transfer is the problem not processing the data once you get it. NSXMLParser can be a bit difficult to wrap your head around at first glance but it is very efficient at what it does and can be very fast in addition to playing nice with your memory. Additionally sending unneeded data over and over or storing the data on disk in many many small operations rather than large reads or writes can cause performance issues.

Latency

Often you cannot control this factor but if it is a problem then you can limit the amount of repeated network calls and load up more data in less network calls.

Finally this link has a good writeup about some of the more technical aspects that you may be interested in.

Community
  • 1
  • 1
slycrel
  • 4,275
  • 2
  • 30
  • 30