How to use asynchronous methods (JSON Restful services) in iOS? - ios

I have an iOS application that sync data from a JSON restful web service. This method is called from an external class (not UI controller). This class has a sync method and It sends and retrieves data without any problem. My problem is how do I pause the UI till I get my result.
The following would provide the idea about the code.
UIController Class
let C : Customer = Customer(UserName: UserName!, Password: Password!)
let S : Syncronization = Syncronization()
S.Sync(C)
Syncronization class
Class Syncronization : NSObject, NSURLSessionDataDelegate
func Sync(C : Customer){
var datastr = ""
datastr = "http://192.168.248.134:8008/MobileWeb.svc/GetFirstTimeSync/" + C.UserName + "/" + C.Password
let url:NSURL = NSURL(string: datastr)!
self.buffer = NSMutableData()
let defaultConfigObject:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session:NSURLSession = NSURLSession(configuration: defaultConfigObject, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
let req:NSMutableURLRequest = NSMutableURLRequest(URL: url)
req.HTTPMethod = "POST"
session.dataTaskWithURL(url).resume()
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
print("Recieved with data")
buffer.appendData(data)
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if error == nil {
print("Download Successful")
print("Done with Bytes " + String(buffer.length))
self.parseJSONA(self.buffer)
}
else {
print("Error %#",error!.userInfo);
print("Error description %#", error!.localizedDescription);
print("Error domain %#", error!.domain);
}
}
func parseJSONA(data:NSMutableData) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! Array<AnyObject>
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
I have tried dispacther methods, but I believe I do not know how to use that so far cause most of the examples have down the services & data exchange on UI Controllers.
Any kind of help is appreciated. Thanks

You can give the Sync class a completion handler closure.
In your UIViewController:
S.completion = {
information in
UIElement.updateWith(information)
}
and of course you'll need to add a member to your Syncronization class:
var completion:((information:String)->())!
and you can call completion("here's some info!") from inside parseJSON() or URLSession() of the Syncronization class
Here's some reading on closures in Swift

If I understand you correctly, you want to be able to do something in your UI based on the outcome of your call to S.Sync(C)
One way of doing that is to include a closure as a parameter to your Sync function.
Here's how I would do that (Disclaimer...I haven't checked everything in a compiler, so there might be errors along the way. See how far you get, and if there are problems, just write again :-)):
enum SynchronizationResult {
case Success(Array<AnyObject>)
case Failure(NSError)
}
class Syncronization : NSObject, NSURLSessionDataDelegate {
var functionToExecuteWhenDone: ((SynchronizationResult) -> Void)?
func Sync(C : Customer, callback: (SynchronizationResult) -> Void){
functionToExecuteWhenDone = callback
var datastr = ""
datastr = "http://192.168.248.134:8008/MobileWeb.svc/GetFirstTimeSync/" + C.UserName + "/" + C.Password
let url:NSURL = NSURL(string: datastr)!
self.buffer = NSMutableData()
let defaultConfigObject:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
let session:NSURLSession = NSURLSession(configuration: defaultConfigObject, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
let req:NSMutableURLRequest = NSMutableURLRequest(URL: url)
req.HTTPMethod = "POST"
session.dataTaskWithURL(url).resume()
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
print("Recieved with data")
buffer.appendData(data)
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if error == nil {
print("Download Successful")
print("Done with Bytes " + String(buffer.length))
self.parseJSONA(self.buffer)
} else {
print("Error %#",error!.userInfo);
print("Error description %#", error!.localizedDescription);
print("Error domain %#", error!.domain);
let result = SynchronizationResult.Failure(error!)
if let functionToExecuteWhenDone = functionToExecuteWhenDone {
functionToExecuteWhenDone(result)
}
}
}
func parseJSONA(data:NSMutableData) {
do {
let json = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! Array<AnyObject>
let result = SynchronizationResult.Success(json)
if let functionToExecuteWhenDone = functionToExecuteWhenDone {
functionToExecuteWhenDone(result)
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
let result = SynchronizationResult.Failure(error)
if let functionToExecuteWhenDone = functionToExecuteWhenDone {
functionToExecuteWhenDone(result)
}
}
}
}
So...we introduce an Enum called SynchronizationResult to handle the outcome of fetching data.
We then add a function to be called when done, as a parameter to the Sync function:
func Sync(C : Customer, callback: (SynchronizationResult) -> Void)
This method will be called with a SynchronizationResult as a parameter and returns void.
We store that callback in functionToExecuteWhenDone for later usage.
Depending on whether you see any errors along the way or everything is sunshine, we generate different SynchronizationResult values along the way and call your functionToExecuteWhenDone with the current SynchronizationResult when we are ready (when parsing is done or we have failed)
And in your ViewController you'd do something along the lines of
let C : Customer = Customer(UserName: UserName!, Password: Password!)
let S : Syncronization = Syncronization()
S.Sync(C) { (result) in
switch result {
case .Success(let json):
//Your code to update UI based on json goes here
case .Failure(let error):
//Your code to handle error goes here
}
}
I hope this makes sense and is what you needed.

this might help you
https://thatthinginswift.com/background-threads/
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// do downloading or sync task here
dispatch_async(dispatch_get_main_queue()) {
// update some UI with downloaded data/sync data
}
}

let qualityOfServiceClass = QOS_CLASS_BACKGROUND
let backgroundQueue = dispatch_get_global_queue(qualityOfServiceClass, 0)
dispatch_async(backgroundQueue, {
print("This is run on the background queue")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("This is run on the main queue, after the previous code in outer block")
})
})
Found here

Related

Observing progress while using URLSessionDataTask and URLSessionDownloadTask both for same remote url

I'm using URLSessionDataTask to show an image, and using URLSessionDownloadTask to download the image on my app. Also I needed to observe download progress, so I use KVO at Progress's fractionCompleted.
The thing is fractionCompleted value just has 0.05 and 1.0. I think this issue happened for using same remote url. Because when I don't use URLSessionDataTask(I can't show the image though), I get right progress update.
Why is it happened? Is there any reference?
Edited
First, I don't want to use URLSession delegate to get progress. I'm using two VCs(first one is RemoteGridViewController using URLSessionDataTask to show image and second one is MediaViewerController). MediaViewerController delegate to first VC for downloading the image on camera roll. I know I can use URLSession delegate method to notify progress by using NotificationCenter or something like that, but I don't want. This way makes strong coupling(?) between the VCs(I guess...)
Second, RemoteGridViewController use URLSession to show an image by URLSessionDataTask and cache it.
guard let url = URL(string: url) else {
completionHandler(.failure(NetworkManagerError.urlError))
return
}
var req = URLRequest(url: url)
req.cachePolicy = .returnCacheDataElseLoad
if let cachedResponse = sharedCache?.cachedResponse(for: req) {
completionHandler(.success(cachedResponse.data))
} else {
sessionDataTask = session.dataTask(with: req) { [weak self] (data, response, error) in
if let error = error {
completionHandler(.failure(error))
print(error.localizedDescription)
return
}
guard let response = response as? HTTPURLResponse, let data = data else {
completionHandler(.failure(NetworkManagerError.dataError))
return
}
self?.sharedCache?.storeCachedResponse(CachedURLResponse(response: response, data: data), for: req)
completionHandler(.success(data))
}
sessionDataTask?.resume()
}
Third, MediaViewerController call MediaViewerDelegate method, which is downloadImage(itemAt:for:) to download an image.
extension RemoteGridViewController: MediaViewerDelegate {
func downloadImage(itemAt indexPath: IndexPath, for mediaViewer: MediaViewerController) {
let data = unsplashData[indexPath.item]
let urlString = data.regular
guard let url = URL(string: urlString) else { return }
mediaViewer.presentProgressView()
networkManager.downloadTaskForImage(with: url, progressHandler: mediaViewer.observe(_:)) { result in
switch result {
case .success(let image):
DispatchQueue.main.async {
mediaViewer.toastView(with: true)
}
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
case .failure(let error):
DispatchQueue.main.async {
mediaViewer.toastView(with: false)
}
print(error.localizedDescription)
}
}
}
}
class NetworkManager {
private let session: URLSession!
private var sessionDownloadTask: URLSessionDownloadTask?
func downloadTaskForImage(with url: URL, progressHandler: (Progress?) -> (), completionHandler: #escaping (Result<UIImage, Error>) -> ()) {
sessionDownloadTask = session.downloadTask(with: url) { (location, response, error) in
if let error = error {
completionHandler(.failure(error))
return
}
guard let response = response as? HTTPURLResponse, (200...299).contains(response.statusCode) else {
completionHandler(.failure(NetworkManagerError.responseError))
return
}
guard let location = location else {
completionHandler(.failure(NetworkManagerError.urlError))
return
}
do {
let data = try Data(contentsOf: location)
if let image = UIImage(data: data) {
completionHandler(.success(image))
} else {
completionHandler(.failure(NetworkManagerError.dataError))
}
} catch let error {
completionHandler(.failure(error))
}
}
progressHandler(sessionDownloadTask?.progress)
sessionDownloadTask?.resume()
}
}
You can track for the downloaded bytes to total expected bytes to track the progress of downloading image.
Use following delegate method:
URLSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)
and following snippet:
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
// println("download task did write data")
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
//Your code to download and save image to camera role
}
}
I hope this may have resolved your query to check for exact progress with downloading.

How to use session.uploadTask in background i am getting crash

I am getting crash
it says *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Completion handler blocks are not supported in background sessions. Use a delegate instead.'
var Boundary = "\(boundary.generateBoundaryString())_boundary"
private lazy var session: URLSession = {
let config = URLSessionConfiguration.background(withIdentifier: "MyUniqeId")
config.isDiscretionary = true
config.sessionSendsLaunchEvents = true
return URLSession(configuration: config, delegate: self, delegateQueue: nil)
}()
func webServiceForUploadImages(urlStr:String,params:[String:String],fileUrl:String,imageData:Data,success :#escaping (AppMedia) -> Void ,failure:#escaping (NSError) -> Void) -> Void{
let url = Constant.BASE_URL + urlStr
print(url)
if(reachAbility.connection != .none){
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "POST"
request.allHTTPHeaderFields = Header.headers()
request.setValue("multipart/form-data; boundary=\(Boundary)", forHTTPHeaderField: "Content-Type")
let data = try! createBody(with: params, filePathKey: "file", paths: [fileUrl], boundary: "\(Boundary)", imageData: imageData)
session.uploadTask(with: request, from: data) { (data, response, err) in
if response != nil{
guard let response = response as? HTTPURLResponse else {return}
handleError.shared.HandleReponseTokenExpireError(dataResponse: response, success: { (response) in
})
if(err != nil){
print("\(err!.localizedDescription)")
}
guard let responseData = data else {
print("no response data")
return
}
if let responseString = String(data: responseData, encoding: .utf8) {
DispatchQueue.main.async {
let dict = Utility.jsonToDict(str: responseString)
let mediaDict = AppMedia(fromDictionary: dict as! [String : Any])
Constant.KAppDelegate.hideProgressHUD()
success(mediaDict)
}
// print("uploaded to: \(responseString)")
}
}else{
DispatchQueue.main.async {
failure(err! as NSError)
Constant.KAppDelegate.hideProgressHUD()
Constant.KAppDelegate.showErrorMessage(title: "Error", message: Constant.ServerError, msgType: .error)
}
}
}.resume()
}else{
self.showErrorMsg(str: Constant.ConnectivityError)
}
}
let config = URLSessionConfiguration.background(withIdentifier: "MyUniqeId")
using this gives me crash
To upload using background URLSessionConfiguration there are a few special considerations:
Cannot use completion handlers (because the app might not be running when you finish the upload). You must use delegate-base methods, e.g. uploadTask(with:fromFile:).
For example:
func startUpload(for request: URLRequest, from data: Data) throws -> URLSessionUploadTask {
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(UUID().uuidString)
try data.write(to: fileURL)
let task = session.uploadTask(with: request, fromFile: fileURL)
task.resume()
return task
}
That obviously assumes that you’ve specified your delegate and implemented the appropriate delegate methods:
extension BackgroundSession: URLSessionDelegate {
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
DispatchQueue.main.async {
self.savedCompletionHandler?()
self.savedCompletionHandler = nil
}
}
}
extension BackgroundSession: URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
print(error)
return
}
print("success")
}
}
Note, we cannot use uploadTask(with:from:) because that’s using Data for that second parameter, which is not allowed for background sessions. Instead one must save the body of the request into a file and then use uploadTask(with:fromFile:).
Remember to handle the scenario where the upload finishes when your app is not running. Namely, the app delegate’s handleEventsForBackgroundURLSession must capture the completion handler. For example, I’ll have a property in my BackgroundSession to save the completion handler:
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: #escaping () -> Void) {
BackgroundSession.shared.savedCompletionHandler = completionHandler
}
And then you want to implement urlSessionDidFinishEvents(forBackgroundURLSession:) and call the saved completion handler:
extension BackgroundSession: URLSessionDelegate {
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
DispatchQueue.main.async {
self.savedCompletionHandler?()
self.savedCompletionHandler = nil
}
}
}
By the way, Downloading Files in the Background discusses many of these considerations (e.g. delegate API rather than closure based API, app delegate issues, etc.). It even discusses the requirements that upload tasks are file-based, too.
Anyway, here is a sample BackgroundSession manager:
import os.log
// Note, below I will use `os_log` to log messages because when testing background URLSession
// you do not want to be attached to a debugger (because doing so changes the lifecycle of an
// app). So, I will use `os_log` rather than `print` debugging statements because I can then
// see these logging statements in my macOS `Console` without using Xcode at all. I'll log these
// messages using this `OSLog` so that I can easily filter the macOS `Console` for just these
// logging statements.
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: #file)
class BackgroundSession: NSObject {
var savedCompletionHandler: (() -> Void)?
static var shared = BackgroundSession()
private var session: URLSession!
private override init() {
super.init()
let identifier = Bundle.main.bundleIdentifier! + ".backgroundSession"
let configuration = URLSessionConfiguration.background(withIdentifier: identifier)
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
}
}
extension BackgroundSession {
#discardableResult
func startUpload(for request: URLRequest, from data: Data) throws -> URLSessionUploadTask {
os_log("%{public}#: start", log: log, type: .debug, #function)
let fileURL = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(UUID().uuidString)
try data.write(to: fileURL)
let task = session.uploadTask(with: request, fromFile: fileURL)
task.resume()
return task
}
}
extension BackgroundSession: URLSessionDelegate {
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
os_log(#function, log: log, type: .debug)
DispatchQueue.main.async {
self.savedCompletionHandler?()
self.savedCompletionHandler = nil
}
}
}
extension BackgroundSession: URLSessionTaskDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if let error = error {
os_log("%{public}#: %{public}#", log: log, type: .error, #function, error.localizedDescription)
return
}
os_log("%{public}#: SUCCESS", log: log, type: .debug, #function)
}
}
extension BackgroundSession: URLSessionDataDelegate {
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didBecome downloadTask: URLSessionDownloadTask) {
os_log(#function, log: log, type: .debug)
}
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
os_log("%{public}#: received %d", log: log, type: .debug, #function, data.count)
}
}
And, of course, my app delegate:
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
...
func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: #escaping () -> Void) {
BackgroundSession.shared.savedCompletionHandler = completionHandler
}
}
Needless to say, that’s a lot to worry about with uploads in conjunction with background URLSession. If you’re uploading huge assets (e.g. videos) over slow connections, perhaps you need that. But the other (simpler) alternative is to use a default URLSession configuration, and just tell the OS that even if the user leaves your app, request a little more time to finish the upload. Just use standard completion handler pattern with default URLSession, and marry that with the techniques outlined in Extending Your App's Background Execution Time. Now, that only buys you 30 seconds or so (it used to be 3 minutes in older iOS versions), but often that’s all we need. But if you think it may take more than 30 seconds to finish the uploads, then you’ll need background URLSessionConfiguration.
Change your
let config = URLSessionConfiguration.background(withIdentifier: "MyUniqeId")
To
let config = URLSession(configuration: URLSessionConfiguration.default)
complete code will like below
let config = URLSession(configuration: URLSessionConfiguration.default)
var Boundary = "\(boundary.generateBoundaryString())_boundary"
private lazy var session: URLSession = {
let config = URLSession(configuration: URLSessionConfiguration.default)
config.isDiscretionary = true
config.sessionSendsLaunchEvents = true
return URLSession(configuration: config, delegate: self, delegateQueue: nil)
}()
func webServiceForUploadImages(urlStr:String,params:[String:String],fileUrl:String,imageData:Data,success :#escaping (AppMedia) -> Void ,failure:#escaping (NSError) -> Void) -> Void{
let url = Constant.BASE_URL + urlStr
print(url)
if(reachAbility.connection != .none){
var request = URLRequest(url: URL(string: url)!)
request.httpMethod = "POST"
request.allHTTPHeaderFields = Header.headers()
request.setValue("multipart/form-data; boundary=\(Boundary)", forHTTPHeaderField: "Content-Type")
let data = try! createBody(with: params, filePathKey: "file", paths: [fileUrl], boundary: "\(Boundary)", imageData: imageData)
session.uploadTask(with: request, from: data) { (data, response, err) in
if response != nil{
guard let response = response as? HTTPURLResponse else {return}
handleError.shared.HandleReponseTokenExpireError(dataResponse: response, success: { (response) in
})
if(err != nil){
print("\(err!.localizedDescription)")
}
guard let responseData = data else {
print("no response data")
return
}
if let responseString = String(data: responseData, encoding: .utf8) {
DispatchQueue.main.async {
let dict = Utility.jsonToDict(str: responseString)
let mediaDict = AppMedia(fromDictionary: dict as! [String : Any])
Constant.KAppDelegate.hideProgressHUD()
success(mediaDict)
}
// print("uploaded to: \(responseString)")
}
}else{
DispatchQueue.main.async {
failure(err! as NSError)
Constant.KAppDelegate.hideProgressHUD()
Constant.KAppDelegate.showErrorMessage(title: "Error", message: Constant.ServerError, msgType: .error)
}
}
}.resume()
}else{
self.showErrorMsg(str: Constant.ConnectivityError)
}
}
now call it in GCD in you viewController or what ever place you want.
DispatchQueue.global().async {
// call your webServiceForUploadImages with completion blocks
}
NOTE:
Only do if you are in need of completion blocks if completion blocks are not necessary do as suggested in exception use delegation

Why do I get the error 'Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}'?

I am new to Swift and iOS development but I am trying to download and parse data that is stored in a MySQL Database.
I keep getting the error:
Domain=NSCocoaErrorDomain Code=3840 "No value." UserInfo={NSDebugDescription=No value.}
I have posted my code below but I don't think the problem is in the parseJSON function but instead in the actual download of the data as when I print the 'data' out it returns '<>'.
Here is my code:
//properties
weak var delegate: HomeModelProtocal!
var data : NSMutableData = NSMutableData()
let urlPath: String = "http://localhost/service.php" //this will be changed to the path where service.php lives
// Function to download the incoming JSON data
func downloadItems(){
let url: URL = URL(string: urlPath)!
var session: URLSession!
let configuration = URLSessionConfiguration.default
session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
let task = session.dataTask(with: url)
task.resume()
}
func urlSession(_ session: URLSession, task: URLSessionDataTask, didCompleteWithError error: Error?) {
self.data.append(data as Data)
}
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
if error != nil{
print("Failed to download data")
}else{
print("Data downloaded")
print(data)
self.parseJSON()
}
}
func parseJSON(){
var jsonResult: NSMutableArray = NSMutableArray()
do{
jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options: []) as! NSMutableArray
} catch let error as NSError {
print("**** sake its happened again \(error)")
}
var jsonElement: NSDictionary = NSDictionary()
let locations: NSMutableArray = NSMutableArray()
for i in 0 ..< jsonResult.count{
jsonElement = jsonResult[i] as! NSDictionary
let location = LocationModel()
//the following insures none of the JsonElement values are nil through optional binding
if let exerciseName = jsonElement["stationName"] as? String,
let bodyPart = jsonElement["buildYear"] as? String
{
print(exerciseName, bodyPart)
location.exerciseName = exerciseName
location.bodyPart = bodyPart
}
locations.add(location)
}
DispatchQueue.main.async(execute: { () -> Void in
self.delegate.itemsDownloaded(items:locations)
})
}
Especially bad thing in your code:
//This method is not being called...
func urlSession(_ session: URLSession, task: URLSessionDataTask, didCompleteWithError error: Error?) {
self.data.append(data as Data) //<-This line adding self.data to self.data
}
There is no urlSession(_:task:didCompleteWithError:) method which takes URLSessionDataTask as its second parameter. So, this method would never be called.
And inside the method, self.data is appended to self.data, so even if the method is called, self.data is still being empty...
You need to implement this method instead:
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
self.data.append(data)
}
But if you want to do nothing other than accumulating received data, you have no need to use delegates.
And you are using forced casting in your parseJSON() method:
jsonResult = try JSONSerialization.jsonObject(with: self.data as Data, options: []) as! NSMutableArray
without specifying .mutableContainers option. This also would crash your app.
And your code uses too much of NSSomethings.
With all such things fixed, you can get something like this:
//properties
weak var delegate: HomeModelProtocal!
let urlPath: String = "http://localhost/service.php" //this will be changed to the path where service.php lives
// Function to download the incoming JSON data
func downloadItems() {
let url: URL = URL(string: urlPath)!
let session = URLSession.shared
let task = session.dataTask(with: url) {data, response, error in
if let error = error {
print("Failed to download data: \(error)")
} else if let data = data {
print("Data downloaded")
print(data as NSData)
//print(String(data: data, encoding: .utf8))
self.parseJSON(data: data)
} else {
print("Something is wrong...")
}
}
task.resume()
}
func parseJSON(data: Data){
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data) as? [[String: AnyObject]] {
var locations: [LocationModel] = []
for jsonElement in jsonResult {
let location = LocationModel()
//the following insures none of the JsonElement values are nil through optional binding
if let exerciseName = jsonElement["stationName"] as? String,
let bodyPart = jsonElement["buildYear"] as? String
{
print(exerciseName, bodyPart)
location.exerciseName = exerciseName
location.bodyPart = bodyPart
}
locations.append(location)
DispatchQueue.main.async {
self.delegate.itemsDownloaded(items: locations)
}
}
} else {
print("bad JSON")
}
} catch let error as NSError {
print("**** sake its happened again \(error)")
}
}

Download image directly to a photo album

Currently I'm using Alamofire for network requests. How can I download an image directly to a photo album that already exists using the PHPhotoLibrary from Photos.framework?
P.S.: I don't mind to have a solution using NSURLSession by itself.
Considerations: The file can't be stored in disk temporarily. I want the data in memory and save it once into the disk using the Photos.framework.
I figure it out by myself. I implemented it with a dataTaskWithRequest from NSURLSession.
Initially I thought using the NSData(contentsOfURL:) but the contentsOfURL method does not return until the entire file is transferred. It should only be used for local files and not for remote ones because if you're loading a large content and the device has a slow connection then the UI will be blocked.
// Avoid this!
let originalPhotoData = NSData(contentsOfURL: NSURL(string: "http://photo.jpg")!)
So, it is possible to load the content into a NSData with a data task. One solution can be like:
let request = NSMutableURLRequest(URL: NSURL(string: urlString.URLString)!)
request.allHTTPHeaderFields = [
"Content-Type": "image/jpeg"
]
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
if let e = error {
print(e.description)
return
}
guard let image = UIImage(data: data) else {
print("No image")
return
}
// Success
// Note: does not save into an album. I removed that code to be more concise.
PHPhotoLibrary.sharedPhotoLibrary().performChanges {
let creationRequest = PHAssetChangeRequest.creationRequestForAssetFromImage(image)
}, completionHandler: { (success : Bool, error : NSError?) -> Void in
if success == false, let e = error){
print(e)
}
}
}
task.resume()
The data is only available in the final stage but you can instead access the data and see the progress using a session delegate.
class Session: NSObject, NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let queue = NSOperationQueue.mainQueue()
var session = NSURLSession()
var buffer = NSMutableData()
var expectedContentLength = 0
override init() {
super.init()
session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: queue)
}
func dataTaskWithRequest(request: NSURLRequest) -> NSURLSessionDataTask {
// The delegate is only called if you do not specify a completion block!
return session.dataTaskWithRequest(request)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveResponse response: NSURLResponse, completionHandler: (NSURLSessionResponseDisposition) -> Void) {
buffer = NSMutableData()
expectedContentLength = Int(response.expectedContentLength)
print("Expected content length:", expectedContentLength)
completionHandler(NSURLSessionResponseDisposition.Allow)
}
func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
buffer.appendData(data)
let percentageDownloaded = Float(buffer.length) / Float(expectedContentLength)
print("Progress: ", percentageDownloaded)
}
func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
print("Finished with/without error \(error)")
// Use the buffer to create the UIImage
}
}

Global variable doesn't store any data in anonymous method

trying to get nasa.gov asteroid's data. There is a asteroids global variable of array of Asteroid instances. There is about 1000 occurrences in the jsonData variable. When I append the occurrence at the line self.asteroids.append(), I can see it's adding. When the anonymous completionHandler method ends, variable self.asteroids is empty again, so it doesn't reload no data.
It doesn't make any sense to me since asteroids is a global variable and it should store any data appended to it. Can anyone help?
class ViewController: UITableViewController {
var asteroids = [Asteroid]()
override func viewDidLoad() {
super.viewDidLoad()
let connectionString: String = "https://data.nasa.gov/resource/y77d-th95.json"
let url = NSURL(string: connectionString)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) in
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
for index in 0 ... (jsonData.count - 1) {
self.asteroids.append(Asteroid(name: jsonData[index]["name"] as! NSString as String))
}
} catch {
print("Error")
return
}
})
task.resume()
self.tableView.reloadData()
}
Put the table view's reloadData method in the completion block, after the asteroids array has been modified.
Another way would be to reloadData in asteroid didSet method:
var asteroids = [Asteroid]() {
didSet {
dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
}
}
}
Code of the completion handler is called after the end of scope of viewDidLoad function. Because the dataTaskWithURL is an asynchronous operation.
Is it empty or you're reloading the table view before the dataTask finishes?
Try to move the reloadData inside the completion closure:
override func viewDidLoad() {
super.viewDidLoad()
let connectionString: String = "https://data.nasa.gov/resource/y77d-th95.json"
let url = NSURL(string: connectionString)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) in
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
for index in 0 ... (jsonData.count - 1) {
self.asteroids.append(Asteroid(name: jsonData[index]["name"] as! NSString as String))
}
self.tableView.reloadData()
} catch {
print("Error")
return
}
})
task.resume()
}
UPDATE: A second approach, if you're 100% sure you want the tableView to be updated after all data has been downloaded & parsed could be:
override func viewDidLoad() {
super.viewDidLoad()
let connectionString: String = "https://data.nasa.gov/resource/y77d-th95.json"
let url = NSURL(string: connectionString)!
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: { (data: NSData?, response: NSURLResponse?, error: NSError?) in
do {
let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions())
for index in 0 ... (jsonData.count - 1) {
self.asteroids.append(Asteroid(name: jsonData[index]["name"] as! NSString as String))
}
self.tableView.delegate = self
self.tableView.dataSource = self
} catch {
print("Error")
return
}
})
task.resume()
}

Resources