重新进入聊天室后,聊天室中删除的消息将重新出现

时间:2016-10-02 15:44:57

标签: ios objective-c sqlite nsarray nsdictionary

当我在聊天室选择要删除的邮件后按删除按钮时,会触发以下代码。

- (void)deleteButtonPressed:(id)sender {

    if (arrayToDelete.count) {
        for (NSString *str in arrayToDelete) {
            NSLog(@"msgID --> %@",str);

            [self.chatModel.dataSource removeObject:str]; //??? Remove data from the screen

            [[FMDBManager sharedInstance] deleteMessageByMessageId:str]; //??? Delete data from database

        }
        [arrayToDelete removeAllObjects];
        [self.chatTableView reloadData];
    }

}

此行成功删除聊天室中的所选邮件。

[self.chatModel.dataSource removeObject:str]; //??? Remove data from the screen

当我走出聊天室并重新进入时,这些消息仍然存在,所以我在下面有这一行。

[[FMDBManager sharedInstance] deleteMessageByMessageId:str]; //??? Delete data from database

我认为以上行应该从数据库中删除那些选定的消息,但是当我重新进入聊天室时,我仍然会看到这些消息。以下是相关代码。

- (void)deleteMessageByMessageId:(NSString *)messageId {
    FMDatabase *db = [self getterDataBase];
    [db open];

    NSString *sqlString = [NSString stringWithFormat:@"DELETE FROM message WHERE messageId = '%@'",messageId];
    BOOL status = [db executeUpdate:sqlString];
    NSLog(@"Delete MessageById:%@ Status:%d",messageId,status);
    [db close];
}

我发现当聊天室调用viewDidLoad时,它最终将调用callBackGetChannelLogNew方法,其中服务器将数据与聊天室tableview和本地数据库同步。

- (void)callBackGetChannelLogNew:(NSDictionary *)resultDataDic status:(enumAPI_STATUS)eAPI_STATUS {

    if (isFirstTimeUpdate) {

    }

    if (eAPI_STATUS == API_STATUS_SUCCEE) {

        NSString *readString=[NSString stringWithFormat:@"%@",resultDataDic[@"read_arr"]];

        if ([readString isEqualToString:@""]) {

            // NSLog(@"read_arr is empty");
        }

        else {
            NSArray *read_arr=resultDataDic[@"read_arr"];
            // Copy read_arr
            self.readArray=[read_arr mutableCopy];
                     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{
                [self dealWithReadArray:read_arr];
            });
        }
        NSArray *data = [resultDataDic objectForKey:@"msg"];
        if (data.count > 0) {
            apiHaveData = YES;
        } else {
            apiHaveData = NO;
            self.loadIngView.hidden = YES;
            isLoadingData = NO;
            return;
        }
                   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^{

            // Reverse order of data
            NSArray* reversedArray = [[data reverseObjectEnumerator] allObjects];
            NSMutableArray *messageFromOtherArray = [NSMutableArray new];
            NSMutableArray *messageAllArray = [NSMutableArray new];

            for (int i = 0; i < reversedArray.count; i++) {
                NSDictionary *_dic = reversedArray[i];

                NSString *fromId = [_dic objectForKey:@"fid"];

                NSString *message = [NSString stringWithFormat:@"%@",[_dic objectForKey:@"say"]];

                if ([ObjectManager getChatMessageKindWithString:message] == MessageTypeText) {
                    message = [ObjectManager decryptWithString:message];
                }

                NSString *messageId = [_dic objectForKey:@"mid"];
                NSString *toId = [_dic objectForKey:@"tid"];
                NSDateFormatter *_formatter = [[NSDateFormatter alloc] init];
                _formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss.SSS";
                NSDate *date_t = [NSDate dateWithTimeIntervalSince1970:[[_dic objectForKey:@"t"] doubleValue]/1000.0]; //換算成日期
                NSString *stringDate = [_formatter stringFromDate:date_t];

                NSString *sendDate = stringDate;
                NSString *lid = _dic[@"lid"];

                NSMutableDictionary *myDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                              fromId,@"fromId",
                                              message,@"message",
                                              messageId,@"messageId",
                                              sendDate,@"sendDate",
                                              toId,@"toId",
                                              lid,@"lid",
                                              nil];
                NSString *isRead;
                if (_chatRoomType == ChatRoomTypePrivate) {
                    if ([_dic[@"r"] intValue]) {
                        isRead = @"1";
                        myDic[@"isRead"] = isRead;
                        lastReadMessageId = [NSString stringWithFormat:@"%@",messageId];
                    }
                }

                if (i == 0) {
                    if (lidForAPI != [_dic[@"lid"] intValue]) {
                        lidForAPI = [_dic[@"lid"] intValue];
                    } else {
                        dispatch_async(dispatch_get_main_queue(), ^{
                            apiHaveData = NO;
                            self.loadIngView.hidden = YES;
                            isLoadingData = NO;
                        });
                        return ;
                    }

                }

                if (![myDic[@"fromId"] isEqualToString:[User sharedUser].account]) {
                    [messageFromOtherArray addObject:myDic];
                }

                if (_chatRoomType == ChatRoomTypeGroup) {
                    [myDic setObject:@"1" forKey:@"isGroupMessage"];
                }

                [myDic setObject:@"1" forKey:@"did_I_Read"];

                [messageAllArray addObject:myDic];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                [self setupViewWithMessageArray:messageAllArray]; //???? Here server sync-up data with tableview
            });
 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^{
                if (_chatRoomType == ChatRoomTypePrivate) {
                    if (messageFromOtherArray.count > 0 && isUplaodLastRead == NO) {
                        isUplaodLastRead = YES;
                        NSDictionary *lastReadMsgDic = messageFromOtherArray.lastObject;
                        [self callMsgReadAPI:lastReadMsgDic];
                    }
                } else {
                    if (messageAllArray.count > 0 && isUplaodLastRead == NO) {
                        isUplaodLastRead = YES;
                        NSDictionary *lastReadMsgDic = messageAllArray.lastObject;
                        [self callMsgReadAPI:lastReadMsgDic];
                    }
                }

                self.chatModel.channelTopic = _topic;

                NSArray *read_arr=resultDataDic[@"read_arr"];
                [self dealMySendMessageReadedWithReadArray:read_arr AndMessageArray:messageAllArray];

                [self saveMessageWithArray:messageAllArray]; //???? Here server sync-up data with local db
           });            
        });
    }
}

此行将数据从服务器同步到tableview

dispatch_async(dispatch_get_main_queue(), ^{
                    [self setupViewWithMessageArray:messageAllArray]; //???? Here server sync-up data with tableview
                });

以下是方法setupViewWithMessageArray

- (void)setupViewWithMessageArray:(NSArray *)messageAllArray {
    if (!isFirstTimeUpdate) {
        isFirstTimeUpdate = YES;
        self.chatModel.dataSource = nil;
        [self.chatTableView reloadData];
        self.chatModel.dataSource = [[NSMutableArray alloc] init];
        [self addMessageWithArray:messageAllArray];
        [self.chatTableView reloadData];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.chatModel.dataSource.count-1 inSection:0];
        [self.chatTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO]; 
    } else {
        [self addMessageWithArray:messageAllArray];
        [self reloadTableViewWithoutMove];
    }
    self.loadIngView.hidden = YES;
    isLoadingData = NO;
    if (_chatRoomType == ChatRoomTypePrivate) {
        if (lastReadMessageId) {
            [self.chatModel setPrivateChatListAllReadFormMessageId:lastReadMessageId];
        }        
    }
}

此行将数据从服务器同步到本地数据库

[self saveMessageWithArray:messageAllArray]; //???? Here server sync-up data with local db

下面是方法saveMessageWithArray

- (void)saveMessageWithArray:(NSArray *)messageArray {
    for (NSDictionary *myDic in messageArray) {
        if (![[FMDBManager sharedInstance] didMessageExistWithMessageID:[myDic objectForKey:@"messageId"]]) {
            [[FMDBManager sharedInstance] SaveMessage:myDic];
        }
        else {
            NSString *mid=[NSString stringWithFormat:@"%@",myDic[@"messageId"]];
            NSString *isRead = myDic[@"isReaed"];
            if (isRead) {
                [[FMDBManager sharedInstance] UpdateisReadWithMessageID:mid];
            }
        }

    }
}

所以我想现在我的问题是如何在服务器同步之前用arrayToDelete更新messageAllArray?

0 个答案:

没有答案