从 Parse 查询中访问 NSArray

Access NSArray out from Parse query

在我的查询中,NSLog 很好,写了好东西 NSLog(@"DANS MSG ENVOYE PAR USER : %@", _messages,但是在 viewDidLoad 中的查询中,我无法访问我的 [=6] 中的值=]

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

我必须实施@synthesize 吗?我做错了什么?

这是我的代码。

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];

   }

您无法在这些行中访问 self.senderMsgself.messages

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

因为它在异步请求完成之前执行。

[query1 findObjectsInBackgroundWithBlock:] 是在后台线程中执行的异步函数。调用这些函数后,viewDidLoad 中的执行将继续到函数结束。那时 NSLog 中的数组将为空。

如果您想使用数组的值,请在 findObjectsInBackgroundWithBlock 中使用它。

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