V
V
Vladislav Pavlenko2016-03-28 15:06:31
Objective-C
Vladislav Pavlenko, 2016-03-28 15:06:31

Why is the data in the collection view not updated correctly?

Hello! The first time I encountered such a problem, there is not much experience in development.
I receive data from the server, display it in the collection view. Everything is fine, it is displayed as it should.
I start receiving data from websockets, I do reloadData for the collection view and the data is not correctly assigned to me.
There are 3 rooms. The first room contains devices, the second and third are empty. Temperature and humidity data are coming. Here is the initial display, the first room has zero values, the other two have default values.
On websockets data "device id":"value" comes. I'm doing a reloadData collection view and this is the problem. The third room is assigned the values ​​from the first.
Tell me, please, how to solve this problem?
This is where the data is processed. (yes, I "pull" the sockets myself)

- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message
{
    //NSLog(@"The websocket received a message: %@", message);
    
    NSString *data = [message stringByReplacingOccurrencesOfString:@"GET" withString:@""];
    NSLog(@"%@", data);
    
    if ([data containsString:@"val"])
    {
        self.websocketsModel = [WebsocketsModel arrayOfModelsFromString:data error:nil];
        [self.collectionView reloadData];
    }
    
    // интервал на запрос данных 30 сек
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.webSocket send:@"GET"];
    });
}

Next, the collection view method. A bit hardcoded, but I'm still learning :)
([_rooms objectAtIndex:indexPath.item - 1] - because of the extra slot for electricity)
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idElectricityCell = @"electricityCell";
    static NSString *idRoomCell = @"roomCell";
    
    
    if (indexPath.item)
    {
        DashboardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:idRoomCell forIndexPath:indexPath];
    
        cell.nameRoomLabel.text = [[_rooms objectAtIndex:indexPath.item - 1] title];
        
        for (DevicesModel *device in [[_rooms objectAtIndex:indexPath.item - 1] devices])
        {
            // если девайс "температура"
            if ([[device valueForKeyPath:@"deviceTypeId"] isEqual:@8])
            {
                NSString *valueByTemperatureLabel = nil;
                
                // если модель сокетов не пустая
                if (self.websocketsModel)
                {
                    for (WebsocketsModel *obj in self.websocketsModel)
                        if ([obj.idDevice isEqualToString:@"48"])
                            valueByTemperatureLabel = obj.value;
                    
                    cell.temperatureLabel.text = valueByTemperatureLabel;
                }
                
                else
                    cell.temperatureLabel.text = [NSString stringWithFormat:@"%@", [device valueForKeyPath:@"value"]];
            }
            
            // если девайс "влажность"
            if ([[device valueForKeyPath:@"deviceTypeId"] isEqual:@9])
            {
                NSString *valueByTemperatureLabel = nil;
                
                // если модель сокетов не пустая
                if (self.websocketsModel)
                {
                    for (WebsocketsModel *obj in self.websocketsModel)
                        if ([obj.idDevice isEqualToString:@"49"])
                            valueByTemperatureLabel = obj.value;
                    
                    cell.humidityLabel.text = valueByTemperatureLabel;
                }
                else
                    cell.humidityLabel.text = [NSString stringWithFormat:@"%@", [device valueForKeyPath:@"value"]];
            }
        }
        return cell;
    }
    
    else
    {
        DashboardEletricityCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:idElectricityCell forIndexPath:indexPath];
        
        cell.electricity.text = [NSString stringWithFormat:@"%@", _powerAnalitic.wattsNow];
        cell.costElectricityPerDay.text = [NSString stringWithFormat:@"%@", _powerAnalitic.cost];
        
        return cell;
    }
    
    return nil;
}

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
G
German Polyansky, 2016-03-28
@pavlenkovs

Well, you should check the pars in didReceiveMessage, by breakpoints, whether it reaches the CollectionView at all, then check what happens inside the cell filling delegate, look carefully at the warning'and that's it, there may be a type casting problem somewhere. In the question it is very important to describe not the code, but where and what is lost / which fragment does not work or does not work correctly, narrow the question and more debug information

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question