首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >查询PFUser不工作

查询PFUser不工作
EN

Stack Overflow用户
提问于 2015-06-12 01:58:56
回答 1查看 67关注 0票数 0

我正在使用这个查询来查找用户,它是有效的,但它只显示了第一个用户。我想让它向我显示一个UITextField文本的用户。我该怎么做呢?(我有一个文本字段,在那里我输入一个名称,然后它应该显示解析后的用户和名称)

代码语言:javascript
复制
PFQuery *query = [PFUser query];

NSArray *users = [query findObjects];

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

非常感谢

EN

回答 1

Stack Overflow用户

发布于 2015-06-12 02:29:23

此代码将获取其中username等于name参数的所有PFUser

代码语言:javascript
复制
- (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,例如,一个表:

代码语言:javascript
复制
- (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无关的逻辑,你可能会锁定主线程,这是糟糕的用户体验。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30788115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档