通过解析查询多个值

时间:2015-03-02 16:30:21

标签: ios objective-c parse-platform pfquery

所以我的应用程序加载到用户的联系人中并存储他们的电话号码,但我想查询这些电话号码与存储在Parse中的电话号码,以确定他们的任何联系人是否正在使用该应用程序。

所以,我知道如何通过在for循环中使用类似下面的代码来处理每个电话号码作为单个查询,

                NSString *phoneNumber = CFBridgingRelease(ABMultiValueCopyValueAtIndex(phoneNumbers, i));
               //parse query for any matches to the phone number
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"phoneNumber" equalTo:phonenumberfieldfriend.text];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        // The find succeeded.
        NSLog(@"Successfully retrieved %d users.", objects.count);
        // Do something with the found objects
        if (objects.count == 0) {
            //uialert letting the user know that no phone number matches the query
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No User"
                                                            message:@"No user matches this phone number"
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            phonenumberfieldfriend.text = @"";







        }
        //if there is only one number matching the query
        if (objects.count ==1) {
            for (PFObject *object in objects) {
                NSLog(@"%@", objects);
                usernamefriend =[ object objectForKey:@"username"];
                numberfriend = [object objectForKey:@"phoneNumber"];
                firstnamefriend = [object objectForKey:@"firstName"];
                lastnamefriend  = [object objectForKey:@"lastName"];
                emailfriend = [object objectForKey:@"email"];

                add.hidden=true;
                phonenumberfieldfriend.hidden=true;
                confirmuser.hidden=false;
                NSLog(@"one user entered %@",usernamefriend);



            }
        }
        //if there is more than one phonenumber matching the query as
        //the user to input the friends username
        //instead
        if (objects.count>1) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"More than one user!"
                                                            message:@"More than one user with this number please enter a username instead!"
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
             phonenumberfriend.text=@"Please enter a username";
            add.hidden=true;
            adduser.hidden=false;

        }

    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}];}

但我想知道是否可以通过一次查询搜索所有联系人电话号码?

1 个答案:

答案 0 :(得分:0)

如果我了解您的愿景,您可以采用多种方式:

一种方法是简单地预先形成查询,然后执行:

query getFirstObjectInBackground:

https://parse.com/docs/ios/api/Classes/PFQuery.html#//api/name/getFirstObjectInBackground

这会将其缩小到只有一个与您的查询参数匹配的电话号码。

getObjectInBackground:

换句话说,只需删除findObjects并替换为getFirstObjectInBackground,将返回匹配的第一个结果