AFHTTPRequestOperationManager+Synchronous.h 5.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // AFHTTPRequestOperationManager+Synchronous.h
  2. //
  3. // Copyright (c) 2013 Paul Melnikow and other contributors
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. #import "AFHTTPRequestOperationManager.h"
  23. /**
  24. ## First, consider adopting an asynchronous design
  25. 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.
  26. Using the asynchronous API has many advantages:
  27. - 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," Appe urges in the OS X user experience guidelines. Asynchronous network operations allow you to do that.
  28. - 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.
  29. - 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).
  30. - 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.
  31. 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.
  32. 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.
  33. ## The Main Thread
  34. 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.
  35. ## AFImageRequestOperation processingBlock and custom operation subclasses
  36. 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 instead.
  37. All custom subclasses must override `-responseObject`. See AFHTTPRequestOperation+ResponseObject.h for more information.
  38. */
  39. @interface AFHTTPRequestOperationManager (Synchronous)
  40. - (id)syncGET:(NSString *)path
  41. parameters:(NSDictionary *)parameters
  42. operation:(AFHTTPRequestOperation *__autoreleasing *)operationPtr
  43. error:(NSError *__autoreleasing *)outError;
  44. - (id)syncPOST:(NSString *)path
  45. parameters:(NSDictionary *)parameters
  46. operation:(AFHTTPRequestOperation *__autoreleasing *) operationPtr
  47. error:(NSError *__autoreleasing *) outError;
  48. - (id)syncPUT:(NSString *)path
  49. parameters:(NSDictionary *)parameters
  50. operation:(AFHTTPRequestOperation *__autoreleasing *) operationPtr
  51. error:(NSError *__autoreleasing *) outError;
  52. - (id)syncDELETE:(NSString *)path
  53. parameters:(NSDictionary *)parameters
  54. operation:(AFHTTPRequestOperation *__autoreleasing *) operationPtr
  55. error:(NSError *__autoreleasing *) outError;
  56. - (id)syncPATCH:(NSString *)path
  57. parameters:(NSDictionary *)parameters
  58. operation:(AFHTTPRequestOperation *__autoreleasing *) operationPtr
  59. error:(NSError *__autoreleasing *) outError;
  60. - (id)synchronouslyPerformMethod:(NSString *)method
  61. URLString:(NSString *)URLString
  62. parameters:(NSDictionary *)parameters
  63. operation:(AFHTTPRequestOperation *__autoreleasing *)operationPtr
  64. error:(NSError *__autoreleasing *)outError;
  65. @end