antf
antf

Reputation: 3222

How to send all the Address Book contents to a server in a fast way

I am writing an app where I need to send all the Address Book contents (namely the names and the numbers) to a server where crossing will be made on them to inform the user which friends from his contacts have this app installed on their device (something similar to most communication apps nowadays). My problem is not in the concept of how to do this, my problem is in the speed and synchronization between the clients and the server.

To retrieve the names and numbers from the Address Book I do a simple loop and store the answers in a NSMutableArray as follows:

for(CFIndex i=0; i< CFArrayGetCount(peopleMutable); i++)
{
    record = CFArrayGetValueAtIndex(peopleMutable, i);
    multi = ABRecordCopyValue(record, kABPersonPhoneProperty);

    //Contact ID
    contactID = (int)ABRecordGetRecordID(record);

    //Full Name
    fullName = [NSString stringWithFormat:@"%@ %@", (NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty), (NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty)];

    int multiCount = ABMultiValueGetCount(multi);
    for(CFIndex j=0; j<multiCount; j++)
    {
        number = (NSString *)ABMultiValueCopyValueAtIndex(multi, j);
        [arrayNamesToSend addObject:[NSString stringWithFormat:@"%u;%@;%@;", contactID, fullName, number];];
    }
}

Well this loop finishes in around one second (maximum 2 seconds even if you have more than 1000 contact). Lets talk about an average case: on the average one might have 500 names in the Address Book, each name is on the average 15 characters long, and each phone number is on the average 15 characters (in international format), so this leads to a total of 15,000 character to be sent to the server. Based on my modest knowledge in client server programming, it is not recommended to send large amount of bytes in one message from the client to the server, and recommended sizes are between 1024 and 4096 bytes per one message.

Now how do I send these contacts to the server? I thought of:

  1. Can't send all the 15,000 characters at once in one message
  2. Sending one by one (I mean contact by contact) is a very slow choice, or I can say not a realistic choice
  3. Divide the whole array into chunks of 2,000 bytes (this is what I did) and send them to the server. Well theoretically this works but when I tried it I faced a problem. Sending 2,000 bytes from the iPhone to the server is very fast, but saving the received 2,000 bytes at the server in the database takes around 3 seconds, and this is causing the server to lose the subsequent contacts that were sent by the client by the time of the save.

Any suggestions on how to solve this issue? Is there a way to upload the whole Address Book as one file to the server and then the server will resolve it and does its work? Many thanks in advance.

Upvotes: 3

Views: 1195

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185831

This is a REALLY BAD IDEA. You are violating all sorts of privacy concerns by sending address book data to the server. And what's more, you don't need it. What you should do is hash all of the data you care about (using something like SHA1, or perhaps HMAC just to be safe) and send the hashes to the server. You can do your comparisons using the hashes just as well as you can using the raw data, but this way you don't have the serious privacy concerns inherent in sending actual personal data to your server.


In any case, your actual question is about sending a large amount of data to the server. I think your concerns are unjustified. What's wrong with sending all the data as one "message"? It sounds like you only have 15kB or so of data. That's really not that large. If you were sending even a single image to the server you'd go way over that size.

Upvotes: 9

Related Questions