• Stars
    star
    162
  • Rank 232,284 (Top 5 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created over 11 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Synchronous requests for AFNetworking 1.x, 2.x, and 3.x

AFNetworking-Synchronous

A minimal category which extends AFNetworking to support synchronous requests.

Version License Platform Downloads Build

It's synchronous

Usage

4.x

  pod 'AFNetworking', '~> 4.0'
  pod 'AFNetworking-Synchronous/4.x'
#import <AFNetworking.h>
#import <AFHTTPSessionManager+Synchronous.h>

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSError *error = nil;
NSData *result = [manager syncGET:@"/document/123"
                       parameters:paramDict
                       headers:headerDict
                             task:NULL
                            error:&error];

Your synchronous request will never return if it is dispatched on the session manager's completion queue.

You really should not perform a synchronous network request on the main thread on iOS, as it's likely to cause a crash when run outside the debugger. You probably should not on OS X either, as it's likely to cause lags in the UI.

If you must do so, create a separate queue for the completion handlers:

manager.completionQueue = dispatch_queue_create("AFNetworking+Synchronous", NULL);

3.x

  pod 'AFNetworking', '~> 3.0'
  pod 'AFNetworking-Synchronous/3.x'
#import <AFNetworking.h>
#import <AFHTTPSessionManager+Synchronous.h>

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSError *error = nil;
NSData *result = [manager syncGET:@"/document/123"
                       parameters:paramDict
                             task:NULL
                            error:&error];

Your synchronous request will never return if it is dispatched on the session manager's completion queue.

You really should not perform a synchronous network request on the main thread on iOS, as it's likely to cause a crash when run outside the debugger. You probably should not on OS X either, as it's likely to cause lags in the UI.

If you must do so, create a separate queue for the completion handlers:

manager.completionQueue = dispatch_queue_create("AFNetworking+Synchronous", NULL);

2.x

  pod 'AFNetworking', '~> 2.0'
  pod 'AFNetworking-Synchronous/2.x'
#import <AFNetworking.h>
#import <AFHTTPRequestOperationManager+Synchronous.h>

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSError *error = nil;
NSData *result = [manager syncGET:@"/document/123"
                       parameters:paramDict
                        operation:NULL
                            error:&error];

Currently there is no support for AFHTTPSessionManager.

1.x

  pod 'AFNetworking', '~> 1.0'
  pod 'AFNetworking-Synchronous/1.x'
#import <AFNetworking.h>
#import <AFHTTPRequestOperationManager+Synchronous.h>

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:...];
NSError *error = nil;
NSData *result = [client synchronouslyGetPath:@"/document/123"
                                   parameters:paramDict
                                    operation:NULL
                                        error:&error];

Discussion

First, consider adopting an asynchronous design

Before you decide to use this category, consider whether you can adopt an asynchronous design instead. As @mattt wrote, asynchronism a tough thing to get your head around, but it's well worth the mental overhead. Rather than creating methods that fetch and return network data, use blocks or delegate methods to call back with the results when you have them.

Using the asynchronous API has many advantages:

  • When you start an operation on the main thread, you return control to the run loop immediately, so your UI can remains responsive. Blocking the main thread for a long time is never a good idea. "Be responsive," Apple urges in the OS X user experience guidelines. Asynchronous network operations allow you to do that.
  • AFNetworking makes asynchronous code easy to write and easy to read. With block-based success and failure handlers, you don't need to implement delegate protocols or provide selectors for callbacks.
  • AFNetworking and Grand Central Dispatch take care of threading for you, so your code does not need to manage threads, run selectors in the background, or invoke dispatch_async. Your completion blocks will be executed on the main thread (unless you configure the operations otherwise).
  • You can provide a better user experience while waiting for a response. Networks are unreliable, particularly for mobile users, and servers can be bogged down. Your users' experiences will be better if you design for a slow connection, which you can only do asynchronously.

However, in some cases, a synchronous response is better, such as when the document architecture or another framework is handling the multithreading for you, and expects a synchronous result. This code attempts to provide a safe and reliable way to use the framework synchronously.

While it overrides the default success and failure queues to avoid a deadlock, it can't anticipate every possible situation. In particular, you should not set the queue from which you're invoking as the processing queue, which will cause a deadlock.

The main thread

You shouldn't call these methods from the main thread. On iOS, if your application enters the background while one of these methods is running on the main thread, a deadlock may result and your application could be terminated.

AFImageRequestOperation processingBlock and custom operation subclasses

This category is suitable for most of the request operation subclasses built into AFNetworking, which process their response objects synchronously.

If you're using the processingBlock on AFImageRequestOperation, which contains essential processing in the completion handler, or your subclass performs other asynchronous processing in the completion handler, use the version in the using-completion-blocks branch.

All custom subclasses must override -responseObject. See AFHTTPRequestOperation+ResponseObject.h for more information.

Development

This project includes integration tests which use the delightful service httpbin. To run them, run pod install inside the TestProject folder, then load the workspace and execute the test action.

Acknowledgements

License

This project is licensed under the MIT license.

More Repositories

1

ObjCMongoDB

Mac OS and iOS library for MongoDB and BSON
Objective-C
163
star
2

zsh-startup-timer

A zsh plugin to print the time it takes for the shell to start up
Python
21
star
3

SimpleObjectPool

Simple thread-safe object pool in Objective-C
Objective-C
13
star
4

react-boxplot

Simple SVG box plots in React
JavaScript
12
star
5

rollup-plugin-cpy

Rollup plugin to easily copy files and folders
JavaScript
7
star
6

ng-bubble-cloud

Simple bubble-cloud directive for AngularJS + D3
JavaScript
6
star
7

ObjCBSON

High-performance BSON serialization and deserialization in Objective-C
Objective-C
5
star
8

chainsmoker

Elegant, generically typed, boolean micromatch for lists of file paths
TypeScript
4
star
9

icedfrisby-nock

Concise support for mock requests in IcedFrisby
JavaScript
3
star
10

RKBSONSerialization

A RestKit BSON serialization implementation using ObjCMongoDB
Objective-C
3
star
11

github-limited

Command-line utility for troubleshooting Github rate limits
JavaScript
2
star
12

hasbin-cli

From the command line, check whether a binary exists in the PATH environment variable
JavaScript
2
star
13

link-into

Create a tree of symlinks from glob patterns
JavaScript
2
star
14

apod-slack-lambda

The Astronomy Picture of the Day via Slack, with a little help from AWS Lambda
JavaScript
1
star
15

function-profiler

Profile a function
JavaScript
1
star
16

wabac

A versioned cache backed by cloud storage
JavaScript
1
star
17

AFNetworking-Categories

Categories to tweak the interface of AFNetworking
Objective-C
1
star
18

node-aws-lambda-scheduler

Minimalist utility to automatically schedule AWS Lambda functions
JavaScript
1
star