search

Home  >  Q&A  >  body text

objective-c - 我现在有10000条网络请求,我怎么在第一条请求有结果返回,在请求第二条,依次类推

我现在的需求就是有10000条网络请求,让他们按照顺序执行,第一天请求完数据之后,存到数据库,让后在请求第二条,一次类题,不知到大家有没有好的方法。

PHPzPHPz2753 days ago806

reply all(2)I'll reply

  • 世界只因有你

    世界只因有你2017-05-02 09:34:54

    If your requests are regular, for example, the IDs are consecutive, you can process the ID in the callback or proxy of each successful request before initiating the next request.
    A relatively simple and crude way:
    Use NSOperationQueue, then set maxConcurrentOperationCount to 1, and add all 10,000 requests. If the executed request has no result, the queue is canceled. However, this saves code but not memory.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-02 09:34:54

    function apiCall (i){
        var i = i || 0 ;
        $http.get(i++).then(function(response){
            if(response.status=='ok'){
                apiCall(i);
            }
        },function(error){
            console.log(error);
        });
    }

    reply
    0
  • Cancelreply