Swift Alamofire在大量请求中释放内存

Swift Alamofire在大量请求中释放内存,swift,alamofire,Swift,Alamofire,我必须执行大量POST请求(超过150个),每个请求都包含大量数据(大约2-4Mb) 我在for in循环中执行此类请求,如下所示: @IBAction func synchToServer(sender: AnyObject) { var GlobalUserInitiatedQueue: dispatch_queue_t { return dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value

我必须执行大量POST请求(超过150个),每个请求都包含大量数据(大约2-4Mb)

我在for in循环中执行此类请求,如下所示:

    @IBAction func synchToServer(sender: AnyObject) {

    var GlobalUserInitiatedQueue: dispatch_queue_t {
        return dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)
    }

    dispatch_async(GlobalUserInitiatedQueue) {

        var serviceGroup = dispatch_group_create()

        var fetchRequest = NSFetchRequest(entityName: "Biography")
        let predicate = NSPredicate(format: "synchronization == nil")
        fetchRequest.predicate = predicate

        if let fetchResults = managedObjectContext!.executeFetchRequest(fetchRequest, error: nil) as? [Biography] {  //This pulls around 150-200 results
            var total = 0
            for l in fetchResults {  //This is where all the requests happen....

                var apiURL = getAppSetting("apiurl") ?? ""  //This variable comes from app settings, global method
                let apikey = getAppSetting("api_key") ?? "" //This variable comes from app settings, global method

                dispatch_group_enter(serviceGroup)
                //In this request l.getJSON returns the data from the entoty as a JSON object
                request(Method.POST, apiURL + "api/biography/" + apikey, parameters: l.getJSON(l), encoding: ParameterEncoding.JSON)
                    .validate(statusCode: [200])
                    .validate(contentType: ["application/json"])
                    .responseJSON{(req,response,data,error) in

                            var json = JSON(data!)

                            l.synch_status = json["status"].stringValue
                            l.synch_message = json["description"].stringValue
                            l.synchronization = NSDate()

                            var err: NSError?

                            if(!managedObjectContext!.save(&err)) {
                                println(err?.localizedDescription)
                            }

                        dispatch_group_leave(serviceGroup)
                }

                total++

                if((total % 10) == 0) {
                    //This waits for the requests to finish in groups of 10 maximum
                    dispatch_group_wait(serviceGroup, DISPATCH_TIME_FOREVER)
                }
            }

            //this waits for the reminder of requests
            dispatch_group_wait(serviceGroup, DISPATCH_TIME_FOREVER)
        }            
    }        
}

但是,这会使内存使用量随着每个请求不断增加,直到最终导致应用程序崩溃。有没有办法告诉alamofire释放已完成请求的内存?

您应该使管理器的
nsursession
无效以执行任何必要的清理。您应该使管理器的
nsursession
无效以执行任何必要的清理。您应该使管理器的
nsursession
无效以执行任何必要的清理清理。