Title: How to check if my contact list members are registered with my app just like whatsapp does?
Tags: ios;swift;chat
Question: I am creating a chat app, for which I need to check each of my contact list members if they are registered with my app and show the registered members list in my app.
Currently I am calling an API to check every number one by one. In case of 800 contacts it is getting called 800 times. I know this is not best way to do it. So, can some one please help me out and suggest me to do it in better way?

Below is my code:

``` func createContactNumbersArray() {
    for i in 0...self.objects.count-1 {
        let contact:CNContact! = self.objects[i]
        if contact.phoneNumbers.count &gt; 0 {
            for j in 0...contact.phoneNumbers.count-1 {
                print((contact.phoneNumbers[j].value).value(forKey: "digits")!)
                let tappedContactMobileNumber = (contact.phoneNumbers[j].value).value(forKey: "digits")
                let phoneNo = self.separateISDCodeMobileNo(mobileNo:tappedContactMobileNumber as! String)
                contactNumbersArray.append(phoneNo.1)
            }
        }
    }
    callGetAppUserDetailService(mobileNumber: self.contactNumbersArray[currentIndex] as! String)
}
```

I am doing this whole process in the background and refreshing the member list on front in current scenario.
I want to make the whole process as fast as Whatsapp.

Comment: Currently its one at a time.

Comment: Yeah. I think the same. Will change it. Thanks for the suggestion.

Comment: I have my own backend but there is another backend developer. Well I followed @kathayatnk suggestion. And for now it is more helpful. Thanks all for your suggestions and answers.

Comment: Does your API expect one number at time or whole number array?

Comment: That seems bad. If you have access to the API, you really should change that to accept Array of phone numbers and return only those numbers that are registered. This will reduce the call to API

Comment: Right. Without a proper API there is no way to do it. Do you build your own back-end or use some public ones like Firebase, Twillio, ConnectyCube?

Here is the accepted answer: There is no way to do it without back-end modifications. So you need to work closely with your back-end engineer and build an API for this. 

Here is an advise how you can build this API:

To have 2 APIs: 

1) Upload the whole user address book to the back-end in a single request, something like:

```let addressBook = NSMutableOrderedSet()

let contact1 = AddressBookContact()
contact1.name = "Jony Ive"
contact1.phone = "281.895.2518"
addressBook.add(contact1)

let contact2 = AddressBookContact()
contact2.name = "Steve Why"
contact2.phone = "412739123123"
addressBook.add(contact2)

let deviceUDID = nil

Request.uploadAddressBook(withUdid: deviceUDID, addressBook: addressBook, force: false, successBlock: { (updates) in

}) { (error) in

}
```

2) As a next step - retrieve a list of already registered users with phones from your address book, something like this:

```let deviceUDID = nil

Request.registeredUsersFromAddressBook(withUdid: nil, successBlock: { (users) in

}) { (error) in

}
```

so now you can show it in UI

Found this example here https://developers.connectycube.com/ios/address-book

Comment for this answer: Yes, actually I have done the same. Thanks for the help. :)

Here is another answer: I think you need to save user's phone number to your database when user registers your app. Then you can find out contacts which have already used your chat app.

Comment for this answer: Thanks for the answer Dogan. Yes. I am doing this already. I am confused about calling the API for so many times like if I have 200 contacts then I have to call the API 200 times. I don't know if I am doing right or not. Is there any better way to achieve this?
Comment for this answer: Yes, I can. But if the app has 1000 or more users, then we'll get that big list and then I'll have to check it from that list.
Comment for this answer: Can you make a service call that returns only the contacts that are changed by the status of app usage?  Then you need to consider updating just these contacts rather than checking all 200 contacts.

Here is another answer: In fact,you can do little on client.
The normal way is: Server provide an interface that support check a phonenumber array, and return the phonenumbers has registered on server.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 6, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2018:00:09/14/18', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<ios><swift><chat>', 'Title': 'How to check if my contact list members are registered with my app just like whatsapp does?', 'ViewCount': 1114, 'id': 52327462, 'user_AboutMe': '<p>Mobile Application Developer with 7+ years of experience. Have worked on both Objective C &amp; Swift and React Native.</p>\n', 'user_AccountId': 9793180.0, 'user_DisplayName': 'Gautam Shrivastav', 'user_Reputation': 998.0, 'user_Views': 144.0, 'user_id': 7257122.0}}