【问题标题】:Query PFUser not working查询 PFUser 不工作
【发布时间】:2015-06-11 17:58:55
【问题描述】:

我正在使用此查询来查找用户,它有效,但它只是向我显示了第一个用户。我希望它向用户显示 UITextField 的文本。 我怎样才能做到这一点 ? (我有一个文本字段,我在其中输入一个名称,然后它应该显示已解析的用户名称)

PFQuery *query = [PFUser query];

NSArray *users = [query findObjects];

userQuerys.text = users[0][@"username"];

非常感谢

【问题讨论】:

    标签: ios xcode parse-platform pfuser


    【解决方案1】:

    此代码将获取所有PFUsers,其中username 等于name 参数:

    - (void)searchUsersNamed:(NSString *)name withCompletion:(void (^)(NSArray *users))completionBlock {
        PFQuery *query = [PFUser query];
        [query whereKey:@"username" equalTo:name];
        [query findObjectsInBackgroundWithBlock:^(NSArray *users, NSError *error) {
             if (!error) {
                 // we found users with that username
                 // run the completion block with the users.
                 // making sure the completion block exists
                 if (completionBlock) {
                     completionBlock(users);
                 }
             } else {
                 // log details of the failure
                 NSLog(@"Error: %@ %@", error, [error description]);
             }
         }];
    }
    

    一个例子,如果你需要用结果来更新 UI,例如一个表格:

    - (void)someMethod {
        // we will grab a weak reference of self to perform
        // work inside the completion block
        __weak ThisViewController *weakSelf = self; 
        //replace ThisViewController with the correct self class
    
        [self searchUsersNamed:@"Phillipp" withCompletion:^(NSArray *users) {
            //perform non-UI related logic here.
            //set the found users inside the array used by the
            //tableView datasource. again, just an example.
            weakSelf.users = users;
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                //pefrorm any UI updates only
                //for example, update a table
                [weakSelf.tableView reloadData];
            }];
        }];
    }
    

    一个小提示:这里的completionBlock如果有错误不会运行,但是即使没有找到用户它也会运行,所以你必须处理它(如果需要。在这个例子中,它是不需要的)。

    避免在该 mainQueue 方法上运行与 UI 无关的逻辑,您可能会锁定主线程,这会带来糟糕的用户体验。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      相关资源
      最近更新 更多