与Parse和MessagesViewController私聊

时间:2015-01-07 18:20:58

标签: ios objective-c parse-platform chat message

我正在与Parse和框架MessageViewController进行私聊。 我成功显示了从用户A到用户B的所有消息对话,但我无法区分谁是发件人或带有泡泡的接收者。 在解析中我有一个类“聊天”,列有:“objectId”,“DestinateurId”(接收者),“senderId”(发送者),“text”,“createdAt”。当我发送消息时,这很好地完成了数据库。 我只想要当用户A向用户B发送消息时,我们看到用户A使用BubbleMessageStyleOutgoing,用户B将接收BubbleMessageStyleIncoming。 (目前,每条消息都在BubbleMessageStyleOutgoing中)

首先,我查询来自发件人和收件人的邮件。有了这个,我得到了我需要显示的所有信息。 问题:我无法从viewDidLoad中访问值NSArray *消息,因此如何区分此消息来自发送方或接收方? (因为我需要区分其他方法( - (BubbleMessageStyle)messageStyleForRowAtIndexPath:(NSIndexPath *)indexPath)

这是我的代码:

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;
@property (strong, nonatomic) NSArray *msg;
@end

ChatViewController.m:

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

@interface ChatViewController ()

@end
id message;
NSDate *receiveDate;
NSString *text;
@implementation ChatViewController

#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]];
    //[query1 orderByDescending:@"createdAt"];
    PFQuery *query2 = [PFQuery queryWithClassName:@"Chat"];
    [query2 whereKey:@"DestinateurId" equalTo:[[PFUser currentUser] objectId]];
    [query2 whereKey:@"senderId" equalTo:self.destinateurChat];
    //[query2 orderByDescending:@"createdAt"];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        PFQuery *orQuery = [PFQuery orQueryWithSubqueries:@[query1, query2]];
        [orQuery orderByDescending:@"createdAt"];
        [orQuery findObjectsInBackgroundWithBlock:^(NSArray *messages, NSError *error) {
            // Do real error handling in your app...
self.msg = messages;
            for (PFObject *message in messages) {
                if ([message[@"senderId"] isEqualToString:[[PFUser currentUser] objectId]]) {
                    // This is an outgoing message
                    self.senderMsg = [messages valueForKey:@"text"];
                    self.messages = [[NSMutableArray alloc] init];
                    [self.messages addObjectsFromArray:self.senderMsg];
                    [self.tableView reloadData];
                } else {
                    // This is an incoming message
                    self.destinateurMsg = [messages valueForKey:@"text"];
                    [self.messages addObjectsFromArray:self.destinateurMsg];
                    [self.tableView reloadData];
                }
            }
        }];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    });


    NSLog(@"%@", self.senderMsg);
NSLog(@"%@", self.msg);
    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];
   }

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.messages.count;
}

#pragma mark - Messages view controller
- (BubbleMessageStyle)messageStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.destinateurMsg ? BubbleMessageStyleIncoming : BubbleMessageStyleOutgoing;
}


- (NSString *)textForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [self.messages objectAtIndex:indexPath.row];
}

- (void)sendPressed:(UIButton *)sender withText:text
{
    [self.messages addObject:text];
    if((self.messages.count - 1) % 2)
        [MessageSoundEffect playMessageSentSound];
    else
        [MessageSoundEffect playMessageReceivedSound];
    [self finishSend];
    PFObject *chat = [PFObject objectWithClassName:@"Chat"];
    [chat setObject:self.destinateurChat forKey:@"DestinateurId"];
    [chat setObject:text forKey:@"text"];
    [chat setObject:[[PFUser currentUser] objectId] forKey:@"senderId"];

    [chat saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"An error occurred!"
                                                                message:@"Please try sending your message again."
                                                               delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [SVProgressHUD dismiss];
            [alertView show];
        }
        else {
            // Everything was successful!
            [SVProgressHUD dismiss];
        }
    }];
}

- (void)backToInboxView{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end

我很乐意听从你的建议。

1 个答案:

答案 0 :(得分:0)

我会尝试做一个&#34;或&#34;查询。

以下是关于此主题的Parse示例代码:

PFQuery *lotsOfWins = [PFQuery queryWithClassName:@"Player"];
[lotsOfWins whereKey:@"wins" greaterThan:@150];

PFQuery *fewWins = [PFQuery queryWithClassName:@"Player"];
[fewWins whereKey:@"wins" lessThan:@5];
PFQuery *query = [PFQuery orQueryWithSubqueries:@[fewWins,lotsOfWins]];
[query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
    // results contains players with lots of wins or only a few wins.
}];

对你来说,这看起来像这样:

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 *orQuery = [PFQuery orQueryWithSubqueries:@[query1, query2]];
[orQuery findObjectsInBackgroundWithBlock:^(NSArray *messages, NSError *error) {
    // Do real error handling in your app...
    for (PFObject *message in messages) {
        if ([message[@"senderId"] isEqualToString:[[PFUser currentUser] objectId]]) {
            // This is an outgoing message

        } else {
            // This is an incoming message

        }
    }
}];