Comments (4)
You should use Alamofire if you use Swift.
from afhttpsessionoperation.
@pronebird - I'd be inclined to agree, though using Swift in your project doesn't preclude using AFNetworking (especially if you're in a mixed Swift/Objective-C environment). Some people/shops are already using AFNetworking, so this was designed for them.
from afhttpsessionoperation.
@aofeng2009 - This is a bit ambiguous, but let's imagine that you wanted to issue GET
requests to download images (which creates NSURLSessionDataTask
objects), you could do something like:
let documentsURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL:nil, create:false)
let manager = AFHTTPSessionManager()
manager.responseSerializer = AFHTTPResponseSerializer()
let queue = NSOperationQueue()
queue.maxConcurrentOperationCount = 3
let urlStrings = [
"http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/s72-55482.jpg",
"http://spaceflight.nasa.gov/gallery/images/apollo/apollo10/hires/as10-34-5162.jpg",
"http://spaceflight.nasa.gov/gallery/images/apollo-soyuz/apollo-soyuz/hires/s75-33375.jpg",
"http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/as17-134-20380.jpg",
"http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/as17-140-21497.jpg",
"http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/as17-148-22727.jpg"
]
for urlString in urlStrings {
let operation = AFHTTPSessionOperation(manager: manager, HTTPMethod: "GET", URLString: urlString, parameters: nil, uploadProgress: nil, downloadProgress: nil, success: { task, responseObject in
// do something with responseObject, e.g. save in documents
guard let imageData = responseObject as? NSData else {
print("not NSData")
return
}
let filename = task.originalRequest!.URL!.lastPathComponent!
let fileURL = documentsURL.URLByAppendingPathComponent(filename)
do {
try imageData.writeToURL(fileURL, options: [])
print("saved \(filename)")
} catch {
print(error)
}
}, failure: { task, error in
print(error)
})
queue.addOperation(operation)
}
from afhttpsessionoperation.
Or, if you really want to use AFURLSessionManager
and create the NSURLRequest
yourself, you could do:
let documentsURL = try! NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL:nil, create:false)
let manager = AFHTTPSessionManager()
manager.responseSerializer = AFHTTPResponseSerializer()
let queue = NSOperationQueue()
queue.maxConcurrentOperationCount = 3
let urlStrings = [
"http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/s72-55482.jpg",
"http://spaceflight.nasa.gov/gallery/images/apollo/apollo10/hires/as10-34-5162.jpg",
"http://spaceflight.nasa.gov/gallery/images/apollo-soyuz/apollo-soyuz/hires/s75-33375.jpg",
"http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/as17-134-20380.jpg",
"http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/as17-140-21497.jpg",
"http://spaceflight.nasa.gov/gallery/images/apollo/apollo17/hires/as17-148-22727.jpg"
]
for urlString in urlStrings {
let request = NSURLRequest(URL: NSURL(string: urlString)!)
let operation = AFURLSessionOperation.dataOperationWithManager(manager, request: request, uploadProgress: nil, downloadProgress: nil, completionHandler: { task, responseObject, error in
// do something with responseObject, e.g. save in documents
guard error == nil && responseObject != nil else {
print(error)
return
}
guard let imageData = responseObject as? NSData else {
print("not NSData")
return
}
let filename = request.URL!.lastPathComponent!
let fileURL = documentsURL.URLByAppendingPathComponent(filename)
do {
try imageData.writeToURL(fileURL, options: [])
print("saved \(filename)")
} catch {
print(error)
}
})
queue.addOperation(operation)
}
from afhttpsessionoperation.
Related Issues (14)
- Thread safety HOT 4
- (minor) missing parameters in 'auto-filled' method sigs HOT 1
- batch requests HOT 1
- Add cocoapods spec HOT 1
- Operation went isFinished=YES without being started by the queue HOT 1
- Not Work for dependency request HOT 1
- AFHTTPSessionOperation not run on background HOT 1
- Why the location of 'if' in 'setExecuting' and 'setFinished' is different? HOT 2
- No method to create operation from NSURLRequest HOT 2
- cancelAllOperations HOT 1
- Semantic issue in library after updating xcode
- The App is Crashing on AsynchroniousOperetionManager.m file while called setFinished method
- app crash with EXC_BAD_ACCESS KERN_INVALID_ADDRESS 0x000000000000000a
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from afhttpsessionoperation.