在我在swift项目中使用的一个库中,一行会导致应用程序崩溃。我试着理解并解决它,但没有运气。我知道它是由数组索引错误引起的。有人可以帮忙吗?
崩溃报告
Fatal Exception: NSRangeException
0 CoreFoundation 0x180a42e38 __exceptionPreprocess
1 libobjc.A.dylib 0x1800a7f80 objc_exception_throw
2 CoreFoundation 0x180922ebc -[__NSArrayM removeObjectAtIndex:]
3 0x10000ac70 -[ChatSectionManager messageForIndexPath:] (ChatSectionManager.m:435)
4 0x10001c194 -[Chat tableView:cellForRowAtIndexPath:] (Chat.m:596)
5 UIKit 0x185ee2f40 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:]
6 UIKit 0x185ee30a8 -[UITableView _createPreparedCellForGlobalRow:willDisplay:]
ChatSectionManager.m
- (QBChatMessage *)messageForIndexPath:(NSIndexPath *)indexPath {
if (indexPath.item == NSNotFound) {
return nil;
}
QMChatSection *currentSection = self.chatSections[indexPath.section];
//crashes here line 435
return currentSection.messages[indexPath.item];
}
Chat.m
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.chatSectionManager messagesCountForSectionAtIndex:section];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.chatSectionManager.chatSectionsCount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *) indexPath {
QBChatMessage *messageItem = [self.chatSectionManager messageForIndexPath:indexPath];
... 包含 removeObjectAtIndex
- (void)deleteMessages:(NSArray *)messages animated:(BOOL)animated {
dispatch_async(_serialQueue, ^{
NSMutableArray *messagesIDs = [NSMutableArray array];
NSMutableArray *itemsIndexPaths = [NSMutableArray array];
NSMutableIndexSet *sectionsIndexSet = [NSMutableIndexSet indexSet];
self.editableSections = self.chatSections.mutableCopy;
for (QBChatMessage *message in messages) {
NSIndexPath *indexPath = [self indexPathForMessage:message];
if (indexPath == nil) continue;
QMChatSection *chatSection = self.chatSections[indexPath.section];
[chatSection.messages removeObjectAtIndex:indexPath.item];
if (chatSection.isEmpty) {
[sectionsIndexSet addIndex:indexPath.section];
[self.editableSections removeObjectAtIndex:indexPath.section];
// no need to remove elements whose section will be removed
NSArray *items = [itemsIndexPaths copy];
for (NSIndexPath *index in items) {
if (index.section == indexPath.section) {
[itemsIndexPaths removeObject:index];
}
}
} else {
[itemsIndexPaths addObject:indexPath];
}
}
dispatch_sync(dispatch_get_main_queue(), ^{
self.chatSections = self.editableSections.copy;
self.editableSections = nil;
if ([self.delegate respondsToSelector:@selector(chatSectionManager:didDeleteMessagesWithIDs:atIndexPaths:withSectionsIndexSet:animated:)]) {
[self.delegate chatSectionManager:self didDeleteMessagesWithIDs:messagesIDs atIndexPaths:itemsIndexPaths withSectionsIndexSet:sectionsIndexSet animated:animated];
}
});
});
}
注意:此次崩溃是随机发生的,我不知道为什么
FABRIC CRASH REPORT http://crashes.to/s/679e90f0c90
答案 0 :(得分:1)
这绝对是deleteMessages
的问题,看看你的主题16在崩溃的地方:
QMChatSectionManager.m第228行 __48- [QMChatSectionManager deleteMessages:animated:] _ block_invoke
检查哪条线228确切地知道问题是什么,但是我会在这里走出去并猜测它
[chatSection.messages removeObjectAtIndex:indexPath.item];
所以你要在线程16上删除messageForIndexPath
主线程上引用的数据结构。正如您所注意到的,这是对竞争条件的公开邀请,几乎不可能按需复制。
修复方法是不修改后台线程中chatSection
的内容。看起来你已经有了正确的想法,你在主线程上分配完成的部分列表,
self.chatSections = self.editableSections.copy;
将该逻辑应用于部分列表中更改的每个项目,随机崩溃将消失。