{"id":16190143,"url":"https://github.com/paulmelnikow/afnetworking-synchronous","last_synced_at":"2025-04-13T06:41:54.587Z","repository":{"id":7876395,"uuid":"9250155","full_name":"paulmelnikow/AFNetworking-Synchronous","owner":"paulmelnikow","description":"Synchronous requests for AFNetworking 1.x, 2.x, and 3.x","archived":false,"fork":false,"pushed_at":"2021-12-13T14:42:21.000Z","size":38526,"stargazers_count":162,"open_issues_count":3,"forks_count":34,"subscribers_count":11,"default_branch":"main","last_synced_at":"2025-03-24T11:45:30.380Z","etag":null,"topics":["afnetworking","http","synchronous"],"latest_commit_sha":null,"homepage":"","language":"Objective-C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/paulmelnikow.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-04-05T20:41:54.000Z","updated_at":"2023-09-12T02:45:37.000Z","dependencies_parsed_at":"2022-09-04T14:01:01.457Z","dependency_job_id":null,"html_url":"https://github.com/paulmelnikow/AFNetworking-Synchronous","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmelnikow%2FAFNetworking-Synchronous","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmelnikow%2FAFNetworking-Synchronous/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmelnikow%2FAFNetworking-Synchronous/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/paulmelnikow%2FAFNetworking-Synchronous/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/paulmelnikow","download_url":"https://codeload.github.com/paulmelnikow/AFNetworking-Synchronous/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248675435,"owners_count":21143763,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["afnetworking","http","synchronous"],"created_at":"2024-10-10T07:38:46.267Z","updated_at":"2025-04-13T06:41:54.566Z","avatar_url":"https://github.com/paulmelnikow.png","language":"Objective-C","funding_links":[],"categories":[],"sub_categories":[],"readme":"AFNetworking-Synchronous\n========================\n\nA minimal category which extends [AFNetworking][] to support synchronous\nrequests.\n\n[![Version](https://img.shields.io/cocoapods/v/AFNetworking-Synchronous.svg)](http://cocoapods.org/pods/AFNetworking-Synchronous)\n[![License](https://img.shields.io/cocoapods/l/AFNetworking-Synchronous.svg?style=flat)](http://cocoapods.org/pods/AFNetworking-Synchronous)\n[![Platform](https://img.shields.io/cocoapods/p/AFNetworking-Synchronous.svg?style=flat)](http://cocoapods.org/pods/AFNetworking-Synchronous)\n[![Downloads](https://img.shields.io/cocoapods/dm/AFNetworking-Synchronous.svg)](http://cocoapods.org/pods/AFNetworking-Synchronous)\n[![Build](https://img.shields.io/travis/paulmelnikow/AFNetworking-Synchronous.svg)](https://travis-ci.org/paulmelnikow/AFNetworking-Synchronous)\n\n![It's synchronous](https://raw.githubusercontent.com/paulmelnikow/AFNetworking-Synchronous/assets/synchronized_diving.gif)\n\n\nUsage\n-----\n\n### 4.x\n\n```rb\n  pod 'AFNetworking', '~\u003e 4.0'\n  pod 'AFNetworking-Synchronous/4.x'\n```\n\n```objective-c\n#import \u003cAFNetworking.h\u003e\n#import \u003cAFHTTPSessionManager+Synchronous.h\u003e\n\nAFHTTPSessionManager *manager = [AFHTTPSessionManager manager];\nNSError *error = nil;\nNSData *result = [manager syncGET:@\"/document/123\"\n                       parameters:paramDict\n                       headers:headerDict\n                             task:NULL\n                            error:\u0026error];\n```\n\nYour synchronous request will never return if it is dispatched on the session\nmanager's completion queue.\n\nYou really should not perform a synchronous network request on the main thread\non iOS, as it's likely to cause a crash when run outside the debugger. You\nprobably should not on OS X either, as it's likely to cause lags in the UI.\n\nIf you must do so, create a separate queue for the completion handlers:\n\n```objective-c\nmanager.completionQueue = dispatch_queue_create(\"AFNetworking+Synchronous\", NULL);\n```\n\n\n### 3.x\n\n```rb\n  pod 'AFNetworking', '~\u003e 3.0'\n  pod 'AFNetworking-Synchronous/3.x'\n```\n\n```objective-c\n#import \u003cAFNetworking.h\u003e\n#import \u003cAFHTTPSessionManager+Synchronous.h\u003e\n\nAFHTTPSessionManager *manager = [AFHTTPSessionManager manager];\nNSError *error = nil;\nNSData *result = [manager syncGET:@\"/document/123\"\n                       parameters:paramDict\n                             task:NULL\n                            error:\u0026error];\n```\n\nYour synchronous request will never return if it is dispatched on the session\nmanager's completion queue.\n\nYou really should not perform a synchronous network request on the main thread\non iOS, as it's likely to cause a crash when run outside the debugger. You\nprobably should not on OS X either, as it's likely to cause lags in the UI.\n\nIf you must do so, create a separate queue for the completion handlers:\n\n```objective-c\nmanager.completionQueue = dispatch_queue_create(\"AFNetworking+Synchronous\", NULL);\n```\n\n### 2.x\n\n```rb\n  pod 'AFNetworking', '~\u003e 2.0'\n  pod 'AFNetworking-Synchronous/2.x'\n```\n\n```objective-c\n#import \u003cAFNetworking.h\u003e\n#import \u003cAFHTTPRequestOperationManager+Synchronous.h\u003e\n\nAFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];\nNSError *error = nil;\nNSData *result = [manager syncGET:@\"/document/123\"\n                       parameters:paramDict\n                        operation:NULL\n                            error:\u0026error];\n```\n\nCurrently there is no support for AFHTTPSessionManager.\n\n### 1.x\n\n```rb\n  pod 'AFNetworking', '~\u003e 1.0'\n  pod 'AFNetworking-Synchronous/1.x'\n```\n\n```objective-c\n#import \u003cAFNetworking.h\u003e\n#import \u003cAFHTTPRequestOperationManager+Synchronous.h\u003e\n\nAFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:...];\nNSError *error = nil;\nNSData *result = [client synchronouslyGetPath:@\"/document/123\"\n                                   parameters:paramDict\n                                    operation:NULL\n                                        error:\u0026error];\n```\n\n\nDiscussion\n----------\n\n### First, consider adopting an asynchronous design\n\nBefore you decide to use this category, consider whether you can adopt an\nasynchronous design instead. As @mattt wrote, asynchronism a tough thing to\nget your head around, but it's well worth the mental overhead. Rather than\ncreating methods that fetch and return network data, use blocks or delegate\nmethods to call back with the results when you have them.\n\nUsing the asynchronous API has many advantages:\n\n - When you start an operation on the main thread, you return control to the\n   run loop immediately, so your UI can remains responsive. Blocking the main\n   thread for a long time is never a good idea. \"Be responsive,\" Apple urges\n   in the OS X user experience guidelines. Asynchronous network operations\n   allow you to do that.\n - AFNetworking makes asynchronous code easy to write and easy to read. With\n   block-based success and failure handlers, you don't need to implement\n   delegate protocols or provide selectors for callbacks.\n - AFNetworking and Grand Central Dispatch take care of threading for you, so\n   your code does not need to manage threads, run selectors in the background,\n   or invoke dispatch_async. Your completion blocks will be executed on the\n   main thread (unless you configure the operations otherwise).\n - You can provide a better user experience while waiting for a response.\n   Networks are unreliable, particularly for mobile users, and servers can be\n   bogged down. Your users' experiences will be better if you design for a slow\n   connection, which you can only do asynchronously.\n\nHowever, in some cases, a synchronous response is better, such as when the\ndocument architecture or another framework is handling the multithreading for\nyou, and expects a synchronous result. This code attempts to provide a safe\nand reliable way to use the framework synchronously.\n\nWhile it overrides the default success and failure queues to avoid a deadlock,\nit can't anticipate every possible situation. In particular, you should not\nset the queue from which you're invoking as the processing queue, which will\ncause a deadlock.\n\n### The main thread\n\nYou shouldn't call these methods from the main thread. On iOS, if your\napplication enters the background while one of these methods is running on the\nmain thread, a deadlock may result and your application could be terminated.\n\n### AFImageRequestOperation processingBlock and custom operation subclasses\n\nThis category is suitable for most of the request operation subclasses built\ninto AFNetworking, which process their response objects synchronously.\n\n**If you're using the processingBlock on AFImageRequestOperation, which\ncontains essential processing in the completion handler, or your subclass\nperforms other asynchronous processing in the completion handler, use the\nversion in the [using-completion-blocks branch][using-completion-blocks].**\n\nAll custom subclasses must override `-responseObject`. See `AFHTTPRequestOperation+ResponseObject.h` for more information.\n\n\nDevelopment\n-----------\n\nThis project includes integration tests which use the delightful service\n[httpbin][]. To run them, run `pod install` inside the `TestProject` folder,\nthen load the workspace and execute the test action.\n\n[httpbin]: https://httpbin.org/\n\n\nAcknowledgements\n----------------\n\n- [EliSchleifer][] for AFNetworking 2.x support\n\n\nLicense\n-------\n\nThis project is licensed under the MIT license.\n\n\n[EliSchleifer]: https://github.com/EliSchleifer\n[AFNetworking]: https://github.com/AFNetworking/AFNetworking\n[using-completion-blocks]: https://github.com/paulmelnikow/AFNetworking-Synchronous/tree/using-completion-blocks\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulmelnikow%2Fafnetworking-synchronous","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpaulmelnikow%2Fafnetworking-synchronous","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpaulmelnikow%2Fafnetworking-synchronous/lists"}