Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get data from two tables from parse database in iOS app

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 247
    Comment on it

    If two tables are connected through pointers, we can get data from both the tables using "include" key.

    For example- There are two tables "Task" and "Task_Detail" and a pointer of Task_Detail is added as a column in table Task with name "detail". Using a single query one can get data from both the table as below.

    PFQuery *firstQuery = [PFQuery queryWithClassName:@"Task" predicate:   [NSPredicate predicateWithFormat:@"creator != %@",[PFUser currentUser]]]; // NSPredicate is used to only get those tasks which have current user as a value in creator column
            [firstQuery includeKey:@"detail"];
           // includeKey will also fetch the corresponding detail record from Task_Detail
            [firstQuery findObjectsInBackgroundWithBlock:^(NSArray *data, NSError *error){
    
           }];
    

    In the same example you can also get record from another table if it is attached to Task_Detail.

    Suppose Task_Detail table has a pointer type column, "votes" to another table "Vote". Using below code we can get data from Task, and detail for this particular task from Task_Detail table and also the votes on this task from Vote table using a single query.

    PFQuery *firstQuery = [PFQuery queryWithClassName:@"Task" predicate:   [NSPredicate predicateWithFormat:@"creator != %@",[PFUser currentUser]]]; // NSPredicate is used to only get those tasks which have current user as a value in creator column
                [firstQuery includeKey:@"detail"];
               [firstQuery includeKey:@"detail.votes"];
               // includeKey will also fetch the corresponding detail record from Task_Detail
                [firstQuery findObjectsInBackgroundWithBlock:^(NSArray *data, NSError *error){
              // data array will have PFObjects containing value for Task record, Task_Detail record and Vote record
               }];
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: