Reputation: 1601
I'm building an app and at sign up/log in, it will go through the users address book, take the phone numbers, check against phone number column in the User class on Parse and if they aren't friends, then add them.
I have a function in AddressBookHelper.swift to help get the phone numbers:
class AddressBookHelper: NSObject {
let addressBook = AFAddressBookManager()
static var addressBookData = AFAddressBookManager.allContactsFromAddressBook()
static var contactsArray = [String]()
static func getContacts() -> [String] {
var array = [String]()
for contact in addressBookData{
let phoneNumberArray = contact.numbers as! [String]
for number in phoneNumberArray{
array.append(number)
}
}
return array
}
}
Then in ParseHelper.swift I do the check again Parse User class:
static func lookUpUserFromAddressBook(addressBook: [String], completionBlock: PFArrayResultBlock) {
for numbers in addressBook{
let query = User.query()
query!.whereKey("telephone", equalTo:numbers)
query!.findObjectsInBackgroundWithBlock(completionBlock)
}
}
And lastly, when the button is clicked, I add the user as a friend, if not already a friend:
@IBAction func importContacts(sender: AnyObject) {
ParseHelper.lookUpUserFromAddressBook(AddressBookHelper.getContacts()) {
(results: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if let objects = results as? [PFObject] {
for object in objects {
let userObject = object as! User
self.matchedUsers.append(userObject)
let query = PFQuery(className: "Friends")
query.includeKey("toUser")
query.whereKey("fromUser", equalTo: User.currentUser()!)
query.findObjectsInBackgroundWithBlock {
(objects: [AnyObject]?, error: NSError?) -> Void in
if error == nil {
if (objects!.count == 0) {
ParseHelper.addFollowRelationshipFromUser(fromUser: User.currentUser()!, toUser: userObject, completionBlock: { (success, error) -> Void in
if success{
self.performSegueWithIdentifier("skipped", sender: self)
}
else{
print("Error: \(error!) \(error!.userInfo)")
}
})
}
else{
self.performSegueWithIdentifier("skipped", sender: self)
}
} else {
print("Error: \(error) \(error!.userInfo)")
}
}
}
}
}
else {
print("Error: \(error!) \(error!.userInfo)")
}
}
}
Am I doing this the right way? It works, but the code seems a bit long. Is there an easier way to do this?
Thanks in advance
Upvotes: 1
Views: 463
Reputation: 3110
You should take a look at PFQuery's whereKey:containedIn:
. This will return you a list of all users whose phone numbers match any of the numbers you pass in an array. This will greatly reduce the number of queries you need to do to retrieve the user's contacts.
// Find users with any of these phone numbers
let numbers = ["1234567890", "1111111111", "222222222"]
query.whereKey("phoneNumber", containedIn: numbers)
From the Parse iOS Developers Guide: https://parse.com/docs/ios/guide#queries
If you want to retrieve objects matching several different values, you can use whereKey:containedIn:, providing an array of acceptable values. This is often useful to replace multiple queries with a single query.
You can also create all the follow relationships in one query using PFObject.saveAllInBackground() which takes an array of PFObjects to save.
Upvotes: 1