三個(gè)列表枫虏,因?yàn)槲沂褂昧艘粋€(gè)數(shù)據(jù)源,在弱網(wǎng)的情況下亮垫,隨便點(diǎn)擊刷新模软,就會(huì)出現(xiàn)數(shù)據(jù)錯(cuò)亂。
沒加鎖之前的寫法饮潦,雖然我在每次刷新之前都清空了數(shù)據(jù)源燃异,但是還是出現(xiàn)了數(shù)據(jù)錯(cuò)誤的情況,是因?yàn)樵诳焖傧吕布虞d和上啦刷新的同時(shí)继蜡,2個(gè)線程同時(shí)對數(shù)據(jù)源操作回俐,如果在2個(gè)線程都判斷infomations
為空時(shí),都添加數(shù)據(jù)稀并,此時(shí)就會(huì)出錯(cuò)仅颇。
[NetWorkClient getMessageList:noticeType page:page success:^(id response) {
if ([response[@"code"] integerValue] == 100) {
NSArray *arr=response[@"data"][@"messageList"];
if (arr.count<=0) {
[self.mainTableView.mj_footer endRefreshingWithNoMoreData];
}else{
if (_currentPage == 1) {
[_infomations removeAllObjects];
[self.infomations addObjectsFromArray:arr];
}else{
[self.infomations addObjectsFromArray:arr];
}
[self.mainTableView reloadData];
}
}
} failure:^(NSError *err) {
}];
解決辦法,就需要對這個(gè)方法進(jìn)行加鎖碘举。
[_mutexLock lock];
if ([response[@"code"] integerValue] == 100) {
NSArray *arr=response[@"data"][@"messageList"];
if (arr.count<=0) {
[self.mainTableView.mj_footer endRefreshingWithNoMoreData];
}else{
if (_currentPage == 1) {
[_infomations removeAllObjects];
[self.infomations addObjectsFromArray:arr];
}else{
[self.infomations addObjectsFromArray:arr];
}
[self.mainTableView reloadData];
}
}
[_mutexLock unlock];
參考:
http://www.cocoachina.com/ios/20161105/17967.html
https://segmentfault.com/q/1010000006002907