How to join common and consecutive elements of an array in Objective-C?

0

I'm new here, I hope you can help me with a question that has come up for a chat project I'm doing.

The problem is this, I get all the message history of a chat from PHP in a json and I pass it to an NSMutableArray and I show it in a TableView, on one side the messages of one and the other side of the other. If the same person sends many consecutive messages, it is very bad, because in each message the user's picture is shown and it becomes repetitive.

What I want to do is, in the array, join all the messages that belong to the same person and that have been sent consecutively, so that they are all shown together with a single photo of the person who sends them, just as it does WhatsApp and other platforms.

The array receives the values (idUserA, idUserB, fotoA, fotoB, mensaje and userQueEnvia)

I want to put together the messages in which the "userQueEnvia" is the same and consecutive.

You can guide me a bit, since I do not get anything about it.

Thank you very much.

    
asked by Jano 21.02.2017 в 22:03
source

2 answers

0

I suggest you refine your class model a bit. In such cases, it is convenient to rely on an ORM such as Realm link , which is a powerful tool for the UITableView to feed directly from the database through consultations. You can make queries filtered by user (Understanding that you can have a user entity with some one to many relationship to another message entity) and even order them by NSDate.

Then it's a matter of extending the UITableViewCell class to the message entities with which you work.

Also with Firebase link you might have interesting results, but you should also get your backend hand.

I hope the information is a good guide.

    
answered by 22.02.2017 в 16:01
0

Thank you very much, at the end of thinking about my question, I have come up with a solution, I do not know if it will be the best, but it does what it needed. I leave the code in case someone can serve:

NSMutableArray *tablaMensajes = [NSMutableArray alloc]init];
tablaMensajes = [jsonObject objectForKey:@"datos"];
NSMutableArray *temp = [[NSMutableArray alloc]init];
    for (NSMutableDictionary *mensaje in tablaMensajes) {
        if (temp.count == 0) [temp addObject:mensaje];
        else {
            if ([[mensaje objectForKey:@"actual"]isEqualToString:[[temp lastObject]objectForKey:@"actual"]]) {
                NSMutableDictionary *cambiar = [[NSMutableDictionary alloc]init];
                [cambiar setValue:[mensaje objectForKey:@"actual"] forKey:@"actual"];
                [cambiar setValue:[NSString stringWithFormat:@"%@\n\n%@", [[temp lastObject]objectForKey:@"mensaje"], [mensaje objectForKey:@"mensaje"]] forKey:@"mensaje"];
                [temp removeLastObject];
                [temp addObject:cambiar];
            } else [temp addObject:mensaje];
        }
    }
tablaMensajes = temp;
    
answered by 22.02.2017 в 21:08