使用Parse

时间:2015-08-06 00:44:11

标签: ios swift parse-platform addressbook relation

我正在构建一个应用程序并在注册/登录时,它将通过用户地址簿,获取电话号码,检查用户类在Parse上的电话号码列,如果它们不是&#39 ; t朋友,然后添加它们。

我在AddressBookHelper.swift中有一个函数来帮助获取电话号码:

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

}

}

然后在ParseHelper.swift中我再次检查Parse User类:

static func lookUpUserFromAddressBook(addressBook: [String], completionBlock: PFArrayResultBlock) {

    for numbers in addressBook{
        let query = User.query()
        query!.whereKey("telephone", equalTo:numbers)
        query!.findObjectsInBackgroundWithBlock(completionBlock)
    }

}

最后,当点击按钮时,我将用户添加为朋友,如果还不是朋友的话:

@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)")
        }

    }

}

我这样做是对的吗?它有效,但代码似乎有点长。有更简单的方法吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

你应该看看PFQuery的whereKey:containedIn:。这将返回所有用户的列表,这些用户的电话号码与您在阵列中传递的任何号码相匹配。这将大大减少您检索用户联系人所需的查询次数。

// Find users with any of these phone numbers
let numbers = ["1234567890", "1111111111", "222222222"]
query.whereKey("phoneNumber", containedIn: numbers)

来自Parse iOS开发者指南: https://parse.com/docs/ios/guide#queries

  

如果要检索与多个不同值匹配的对象,则为您   可以使用whereKey:containedIn :,提供一个可接受的数组   值。这对于用单个查询替换多个查询通常很有用   查询。

您还可以使用PFObject.saveAllInBackground()在一个查询中创建所有关注关系,这需要保存PFObject数组。