【问题标题】:How to add objects of two PFquery data using parse?如何使用解析添加两个 PFquery 数据的对象?
【发布时间】:2013-07-06 05:48:15
【问题描述】:

我有两个查询,一个是friendsrequestQuery,另一个是用户查询我想在从用户查询中获取数据时将friendrequestQuery详细信息的所有数据添加到用户查询中。

NSPredicate *predicate12=[NSPredicate predicateWithFormat:@"((UserFriendId == %@ )AND (UserId!=%@)) OR ((UserFriendId != %@ )AND (UserId==%@)) ",@"Nwk44aeSrz",@"Nwk44aeSrz",@"Nwk44aeSrz",@"Nwk44aeSrz"];
PFQuery *innerQuery = [PFQuery queryWithClassName:@"FriendsDetails" predicate:predicate12];
[innerQuery whereKey:@"BlockStatus" equalTo:@"No"];
PFQuery * userQuery = [PFUser query];

[userQuery whereKey:@"objectId" matchesKey:@"UserFriendId" inQuery:innerQuery];
[userQuery whereKey:@"objectId" matchesKey:@"UserId" inQuery:innerQuery];

[userQuery whereKey:@"objectId" notEqualTo:@"Nwk44aeSrz"];

我将解释我真正需要的是,在内部查询表中,我有 10 列,但我需要这些特定的 coloum 数据 converstionid、lastmessage、lastdate 的数据,同时重新获取用户查询的数据。 现在我得到了 userquery 的详细信息,而不是 innerquery 的详细信息,所以我需要 Innerquerydetails 的详细信息。 请帮帮我。

【问题讨论】:

    标签: ios parse-platform pfquery


    【解决方案1】:

    我认为您最好存储指针而不是 Id,这样您也可以包含该数据。

    我不会使用谓词,我意识到您的查询使用谓词更清晰,但我认为这样更容易理解逻辑。您可以自己将其转换回谓词。

    // get your user
    PFUser * userForQuery; // set up your user for the query, if it's current user, = [PFUser currentUser];
    
    // first part of predicate
    PFQuery * innerQueryA = [PFQuery queryWithClassName:@"FriendsDetails"];
    [innerQueryA whereKey:@"UserFriend" equalTo:userForQuery]; // from  @"UserFriendId"
    [innerQueryA whereKey:@"User" notEqualTo:userForQuery]; // from @"UserId"
    
    // second part of predicate
    PFQuery * innerQueryB = [PFQuery queryWithClassName:@"FriendsDetails"];
    [innerQueryB whereKey:@"userFriend" notEqualTo:userForQuery]; // from @"UserFriendId"
    [innerQueryB whereKey:@"user" equalTo:userForQuery]; // from @"UserId"
    
    // combine
    PFQuery * query = [PFQuery orQueryWithSubqueries:@[innerQueryA, innerQueryB]];
    [query whereKey:@"BlockStatus" equalTo:@"No"];
    
    // Now, as you remember, we are storing a pointer in @"User" as opposed to an id @"UserId" as you had it. Because of     
    // this, we will use parse's includeKey: feature.
    
    [query includeKey:@"User"]; // tells the query to include the data associated with these keys;
    
    NSMutableArray * usersRetrieved = [[NSMutableArray alloc]init];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {    
            // objects will contain all of your objects
            for (PFObject * object in objects) {
                [usersRetrieved addObject:object[@"User"]]; // all the data should be available for the @"User" object
            }
    
            // objects will contain all the @"friendDetails" objects
            // usersRetrieved will contain all the @"User" objects
        }
        else {
        }
    }];
    

    我意识到这会稍微改变您的数据结构,但它应该为您提供所需的所有数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 2020-05-19
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多