从Parse查询中访问NSArray

时间:2015-01-07 15:05:38

标签: ios parse-platform nsarray chat pfquery

在我的查询中,NSLog很好地写好了NSLog(@"DANS MSG ENVOYE PAR USER : %@", _messages,但在viewDidLoad中的查询中我无法访问NSArray中的值

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

   }

1 个答案:

答案 0 :(得分:2)

您无法在这些行中访问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]
  }
}]