Shayma.ABZ Shayma.ABZ - 1 year ago 126
Swift Question

AFNetworking Block the UI

I'm using AFNetworking to request data from server, in case of bad internet connection, it will take a long time to receive data , so if i don't want to wait and decided to go back, it will not allow me (block the UI), what i should do to be able to go back if i don't want to wait for data to be loaded

this is the method i'm using to request data from API:

let manager = AFHTTPRequestOperationManager()
manager.requestSerializer = AFJSONRequestSerializer(writingOptions: []) //1
manager.responseSerializer = AFJSONResponseSerializer() //2
manager.responseSerializer.acceptableContentTypes = NSSet(objects: "application/json") as Set<NSObject>
manager.responseSerializer.acceptableContentTypes = NSSet(array: ["text/plain", "text/html", "application/json"]) as Set<NSObject>


manager.GET(url as String,
parameters: dictionary,
success: {(operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in

print("inside connectionManager success")

failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
print("inside failure block of get ")


Not sure what could be blocking the UI looking at this code, since those methods (should be) asynchronous already... do you have something that calls this code that blocks until this webservice call is finished? otherwise try wrap the whole thing in a dispatch queue like

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {
 // do stuff

your delegate callbacks though should go back onto the main thread in case there are ui updates

dispatch_sync(dispatch_get_main_queue(), {