Use one of the following methods to create a Net instance
12345
// without baseURLletnet=Net()// with baseURLletnet=Net(baseUrlString:"http://www.puqiz.com/")
HttpRequest
GET Request
123456789101112131415161718
leturl="get_path"letparams=["integerNumber":1,"doubleNumber":2.0,"string":"hello"]net.GET(url,params:params,successHandler:{responseDatainletresult=responseData.json(error:nil)NSLog("result \(result)")},failureHandler:{errorinNSLog("Error")})// you can also make a request with absolute urlleturl="http://www.puqiz.com/get_path"net.GET(absoluteUrl:url,params:params,successHandler:{responseDatainletresult=responseData.json(error:nil)NSLog("result \(result)")},failureHandler:{errorinNSLog("Error")})
By using responseData in sucessHandler closure you can quickly
get json dictionary
get image
parse xml
for GET, POST, PUT, DELETE request.
12345678
// get json dictionary from response dataletjsonDic=responseData.json(error:error)// get image from response dataletimage=responseData.image()// parse xml with delegateletresult=responseData.parseXml(delegate:self)
POST Request
Net will automatically check your params to send request as a URL-Encoded request or a Multi-Part request. So you can easily post with number, string, image or binary data.
Before using download/upload function you have to call setupSession method to setup the session.
12
// setup session without backgroundIdentifiernet.setupSession()
To perform background downloads or uploads, you have to call setupSession method with a background identifier string. Then your download/upload tasks can be run even when the app is suspended, exits or crashes.
123456789101112
// setup session with backgroundIdentifiernet.setupSession(backgroundIdentifier:"com.nghialv.download")// you can set eventsForBackgroundHandler closure// this closure will be invoked when a task is completed in the backgroundnet.eventsForBackgroundHandler={urlSessioninurlSession.getDownloadingTasksCount{downloadingTaskCountinifdownloadingTaskCount==0{NSLog("All files have been downloaded!")}}}
Download
123456789101112131415
letdownloadTask=net.download(absoluteUrl:url,progress:{progressinNSLog("progress \(progress)")},completionHandler:{fileUrl,erroriniferror!=nil{NSLog("Download failed")}else{NSLog("Downloaded to : \(fileUrl)")}})// you can control your taskdownloadTask.resume()downloadTask.suspend()downloadTask.cancel()
// submit your code for asynchronous execution on a global queue with high prioritygcd.async(.High){// your code}// or with main threadgcd.async(.Main){// your code}gcd.async(.Default){// your codegcd.async(.Main){// code run on main thread}}// with your custom queueletmyQueue=GCDQueue(serial:"myQueue")gcd.async(.Custom(myQueue)){// your code}// run with delaygcd.async(.Background,delay:5.0){// your code}// sync codegcd.sync(.Main){// your code}// applygcd.apply(.Default,10){indexin// your code}// oncevaronceToken:GCDOnce=0gcd.once(&onceToken){// your code}
- manage group of block with GCDGroup
123456789101112131415
// create groupletgroup=GCDGroup()// you can add async code to groupgroup.async(.Defaul){// your code}// you can set notify for this groupgroup.notify(.Main){// your code}// or wait synchronously for block in group to complete and timeout is 10 secondsgroup.wait(10)
- create your custom queue with CGDQueue
123456789101112131415
// create a serial queueletserialQueue=GCDQueue(serial:"mySerialQueue")// create a concurrent queueletconcurrentQueue=GCDQueue(concurrent:"myConcurrentQueue")// you can submit async barrier to queuemyQueue.asyncBarrier{// your code}// or sync codemyQueue.syncBarrier{// your code}