Skip to content

Commit 280df03

Browse files
committed
私信数据丢失or重复
1 parent fade07e commit 280df03

1 file changed

Lines changed: 37 additions & 15 deletions

File tree

Coding_iOS/Models/PrivateMessages.m

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,41 @@ - (NSDictionary *)toPollParams{
121121
}
122122

123123
- (NSInteger)p_lastId{
124-
NSInteger last_id;
125-
if (!_list || _list.count <= 0) {
126-
last_id = 0;
124+
__block NSInteger last_id = 0;
125+
[_list enumerateObjectsUsingBlock:^(PrivateMessage *obj, NSUInteger idx, BOOL *stop) {
126+
if (obj.sender.id.integerValue != obj.friend.id.integerValue) {
127+
last_id = obj.id.integerValue;
128+
*stop = YES;
129+
}
130+
}];
131+
return last_id;
132+
}
133+
134+
- (BOOL)p_addMsg:(PrivateMessage *)aMsg{
135+
NSInteger curId = aMsg.id.integerValue;
136+
137+
__block NSInteger add_index;
138+
__block BOOL needToAdd = NO;
139+
140+
if (self.list.count > 0) {
141+
[self.list enumerateObjectsUsingBlock:^(PrivateMessage *obj, NSUInteger idx, BOOL *stop) {
142+
if (curId == obj.id.integerValue) {
143+
needToAdd = NO;
144+
*stop = YES;
145+
}else if (curId > obj.id.integerValue){
146+
needToAdd = YES;
147+
add_index = idx;
148+
*stop = YES;
149+
}
150+
}];
151+
if (needToAdd) {
152+
[self.list insertObject:aMsg atIndex:add_index];
153+
}
127154
}else{
128-
PrivateMessage *last_Msg = [_list firstObject];
129-
last_id = last_Msg.id.integerValue;
155+
needToAdd = YES;
156+
[self.list addObject:aMsg];
130157
}
131-
return last_id;
158+
return needToAdd;
132159
}
133160

134161
- (void)configWithObj:(id)anObj{
@@ -157,20 +184,15 @@ - (void)configWithPollArray:(NSArray *)pollList{
157184
if (pollList.count <= 0) {
158185
return;
159186
}
160-
NSInteger last_id = [self p_lastId];
161-
__block NSInteger bridge_index;
187+
162188
__block BOOL hasNewData = NO;
189+
__weak typeof(self) weakSelf = self;
163190
[pollList enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(PrivateMessage *obj, NSUInteger idx, BOOL *stop) {
164-
if (obj.id.integerValue > last_id) {
191+
if ([weakSelf p_addMsg:obj]) {
165192
hasNewData = YES;
166-
bridge_index = idx;
167-
*stop = YES;
168193
}
169194
}];
170195
if (hasNewData) {
171-
NSRange freshDataRange = NSMakeRange(0, bridge_index +1);
172-
NSArray *freshDataList = [pollList subarrayWithRange:freshDataRange];
173-
[self.list insertObjects:freshDataList atIndexes:[NSIndexSet indexSetWithIndexesInRange:freshDataRange]];
174196
[self reset_dataList];
175197
}
176198
}
@@ -197,7 +219,7 @@ - (void)sendSuccessMessage:(PrivateMessage *)sucessMsg andOldMessage:(PrivateMes
197219
return;
198220
}
199221
[self.nextMessages removeObject:oldMsg];
200-
[self.list insertObject:sucessMsg atIndex:0];
222+
[self p_addMsg:sucessMsg];
201223
[self reset_dataList];
202224
}
203225
- (void)deleteMessage:(PrivateMessage *)msg{

0 commit comments

Comments
 (0)