iOS:停止无限数量的请求

时间:2015-05-05 09:00:24

标签: ios request nsoperation nsoperationqueue alamofire

在我班上我有这段代码:

这是下载JSON并检查每个元素内是否有下载文件的URL的方法,我可以停止" my_doc_r"当我想要

func requestDocuments(completion:(response:AnyObject)->()){

         my_doc_r = request(.POST, "http://example.com/json/docs")
            .responseJSON { (_, _, JSON, error) in

                if error == nil{

                    var array = JSON as! NSArray
                    completion(response:array)

                    self.attachments_to_download = 0

                    for var i = 0; i < array.count; ++i{

                        if array.objectAtIndex(i).objectForKey("file_url") as! String != ""{

                            var url_file = array.objectAtIndex(i).objectForKey("file_url") as! String

                            self.downLoadFile(url_file)
                        }
                    }                  
                }
                else{

                }

        }


    }

否则在这里我有无限数量的&#34;下载&#34;请求,如果我需要停止所有?我能做什么? 我和经理一起试过,但我没有取得好成绩。 如果我想停止所有下载,我该怎么办? 或者最好的解决方案是参加一个下载完成以启动另一个下载?

func downLoadFile(url:String){

        download(.GET, url, { (temporaryURL, response) in
            if let directoryURL = NSFileManager.defaultManager()
                .URLsForDirectory(.DocumentDirectory,
                    inDomains: .UserDomainMask)[0]
                as? NSURL {

                    let pathComponent = response.suggestedFilename

                    var finalPath = NSURL()

                    finalPath = directoryURL.URLByAppendingPathComponent(pathComponent!)

                    //check same file with same name, delete and replace
                    if NSFileManager.defaultManager().fileExistsAtPath(finalPath.absoluteString!) {
                        NSFileManager.defaultManager().removeItemAtPath(finalPath.absoluteString!, error: nil)
                    }

                    return finalPath
            }

            return temporaryURL
        })


    }

1 个答案:

答案 0 :(得分:0)

最后我找到了解决方案:

我更改了第一行并在我的内容中添加了一行:

@{Html.Kendo().Grid<Cee.DomainObjects.DomainObjects.BrandConnector>()
.Name("BrandConnectorGrid")
.Filterable()
.Events(e => e.Edit("onEdit"))
.DataSource(dataSource => dataSource
.Ajax()
.Events(e => e.Error("error_handler").RequestEnd("onRequestEnd"))
.ServerOperation(false)
.Model(model =>
{
  model.Id(p => p.brand_id);
  model.Field(e => e.CompanyConnectorList).DefaultValue(new 
  List<Cee.DomainObjects.DomainObjects.CompanyConnector>());
})
.Read(read => read.Action("_AjaxBinding", "BrandConnector",new{companyID = 0 }).Type(HttpVerbs.Post))
.Update(update => update.Action("_UpdateBinding", "BrandConnector").Type(HttpVerbs.Post)))
                               .Columns(columns =>
                               {
                                   columns.Bound(c => c.brand_connector_id).Width(0).Hidden(true);
                                   columns.Bound(c => c.company_id).Width(0).Hidden(true);
                                   columns.Bound(c => c.brand_id).Width(0).Hidden(true);
                                   columns.Bound(u => u.brand_name).Title("Brand").Width("18%").HtmlAttributes(new { @class = "brkWord", @readonly = "readonly" });
                                   columns.ForeignKey(u => u.connector_name, Model.CompanyConnectorList, "company_connector_id", "connector_name").Title("Connector").Width

("16%").HtmlAttributes(new { @class = "brkWord"     }).EditorTemplateName("company_connector_id");
 columns.Command(p => p.Edit().Text("Edit").HtmlAttributes(new { @title = "Edit" })).Width("16%").Title("Edit");
                               })
.Editable(editable => editable.Mode(GridEditMode.InLine).CreateAt(GridInsertRowPosition.Top))
                            .Pageable(pageable => pageable.Refresh(true).PageSizes(GlobalCode.recordPerPageList).ButtonCount(GlobalCode.PageSize).Input(true).Numeric(true))
                                                        .HtmlAttributes(new { @class = "dynamicWidth" })
                                       .Sortable(sorting => sorting.Enabled(true))
                                                        .Render();
                            }

当我想要取消所有请求时:

func downLoadFile(url:String){

let my_req_down = download(.GET, url, { (temporaryURL, response) in //define the request

......
......

downloads_requests.addObject(my_req_down) //add all request in an array
}

它工作得很好!!!