search

Home  >  Q&A  >  body text

ios - 网络请求数据重复的可能

看代码:

#viewDidLoad

    [self getActivityListFromNetwork];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(refreshActivityListFromNetwork)
                                                 name:@"backRefresh"
                                               object:nil];

#refreshActivityListFromNetwork
- (void)refreshActivityListFromNetwork{
    [self.dataArray removeAllObjects];
    [self getActivityListFromNetwork];
    [self.mainTableView headerEndRefreshing];
}                                             
                                       
                                               

问题是:如果注销后重新登录的话会有一定概率出现重复数据。
分析原因是点击登录后,页面加载执行了getActivityListFromNetwork这个方法。这时候去请求数据。在没有返回数据前执行了refreshActivityListFromNetwork又一次去请求了数据。导致两次请求获得的数据都加载出来了。。。

怪我咯怪我咯2771 days ago479

reply all(5)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:55:42

    Execute removeAllObjects and reloadData after each data request is successful. In addition, it is best to provide a corresponding caching mechanism for your network request method. When there is no new data, there is no need to update the data source.

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 09:55:42

    Ask and answer your own questions. . . .

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:55:42

    Why is it not executed after the network request comes back to obtain the data[self.dataArray removeAllObjects];?

    reply
    0
  • 黄舟

    黄舟2017-04-18 09:55:42

    I agree with you upstairs, the data source removeAllObjects should be placed in the successful network request

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 09:55:42

    Um, why are you writing it in such a disgusting way? Agent, block deserves your own

    reply
    0
  • Cancelreply