繁体   English   中英

从解析查询访问NSArray

[英]Access NSArray out from Parse query

在我的查询中, 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];

   }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM