Search code examples
iosparse-platformnsarraychatpfquery

Access NSArray out from Parse query


In my query, the NSLog is good that writes the good things NSLog(@"DANS MSG ENVOYE PAR USER : %@", _messages, but out the query in viewDidLoad I can't access the value from my NSArray

NSLog(@"DESTINATEUR MSG : %@", self.senderMsg);
NSLog(@"EN DEHORS QUERY : %@", self.messages);

Have I to implement @synthesize ? What am I doing wrong ?

Here is my code.

ChatViewController.h :

#import "MessagesViewController.h"

@interface ChatViewController : MessagesViewController

@property (strong, nonatomic) NSMutableArray *messages;
@property (strong, nonatomic) NSString *destinateurChat;
@property (strong, nonatomic) NSArray *senderMsg;
@property (strong, nonatomic) NSArray *destinateurMsg;

@end

ChatViewController.m :

#import "ChatViewController.h"
#import <Parse/Parse.h>
#import "SVProgressHUD.h"

@interface ChatViewController ()

@end
id message;
NSDate *receiveDate;
NSString *text;
@implementation ChatViewController
@synthesize destinateurMsg = _destinateurMsg;
@synthesize messages = _messages;

#pragma mark - View lifecycle
- (void)viewDidLoad
{

    [super viewDidLoad];
    self.title = @"Messages";
    PFQuery *query1 = [PFQuery queryWithClassName:@"Chat"];
    [query1 whereKey:@"DestinateurId" equalTo:self.destinateurChat];
    [query1 whereKey:@"senderId" equalTo:[[PFUser currentUser] objectId]];
    PFQuery *query2 = [PFQuery queryWithClassName:@"Chat"];
    [query2 whereKey:@"DestinateurId" equalTo:[[PFUser currentUser] objectId]];
    [query2 whereKey:@"senderId" equalTo:self.destinateurChat];
   // PFQuery *hotelQuery = [PFQuery orQueryWithSubqueries:@[query1, query2]];

    //Message envoyé par l'user
    [query1 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            _senderMsg = [objects valueForKey:@"text"];
            _messages = [[NSMutableArray alloc] init];
            [_messages addObjectsFromArray:_senderMsg];
            NSLog(@"DANS MSG ENVOYE PAR USER : %@", _messages);

    //Messages destiné à l'user
            [query2 findObjectsInBackgroundWithBlock:^(NSArray *objects2, NSError *error) {
                if (!error) {
                    self.destinateurMsg = objects2;
                    [self.messages addObjectsFromArray:self.destinateurMsg];
                } else {
                    // Log details of the failure
                    NSLog(@"Error: %@ %@", error, [error userInfo]);
                }
            }];
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];


    NSLog(@"DESTINATEUR MSG : %@", self.senderMsg);
    NSLog(@"EN DEHORS QUERY : %@", self.messages);
    UIButton *exitButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [exitButton addTarget:self action:@selector(backToInboxView) forControlEvents:UIControlEventTouchUpInside];
    [exitButton setTitle:@"Inbox" forState:UIControlStateNormal];
    exitButton.frame = CGRectMake(0.0, 0.0, 60, 60);
    [self.view addSubview:exitButton];

   }

Solution

  • You cannot access self.senderMsg and self.messages in these lines

    NSLog(@"DESTINATEUR MSG : %@", self.senderMsg);
    NSLog(@"EN DEHORS QUERY : %@", self.messages);
    

    because its executed before the Asynchronous request is completed.

    [query1 findObjectsInBackgroundWithBlock:] is an asynchronous function that is excecuted in a background thread. After calling those functions the execution inside viewDidLoad continues to the end of the function. At that time the arrays will be empty in the NSLog.

    If you want to use the values of the array use it inside the findObjectsInBackgroundWithBlock.

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
      if (!error) {
           // You can use it in this block.
           // Reload Data here like [self.tableView reloadData]
      }
    }]