car in 場站查車鎖車 ios
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.8KB

  1. //
  2. // JSONModel+networking.m
  3. // JSONModel
  4. //
  5. #import "JSONModel+networking.h"
  6. #import "JSONHTTPClient.h"
  7. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  8. #pragma GCC diagnostic ignored "-Wdeprecated-implementations"
  9. BOOL _isLoading;
  10. @implementation JSONModel(Networking)
  11. @dynamic isLoading;
  12. -(BOOL)isLoading
  13. {
  14. return _isLoading;
  15. }
  16. -(void)setIsLoading:(BOOL)isLoading
  17. {
  18. _isLoading = isLoading;
  19. }
  20. -(instancetype)initFromURLWithString:(NSString *)urlString completion:(JSONModelBlock)completeBlock
  21. {
  22. id placeholder = [super init];
  23. __block id blockSelf = self;
  24. if (placeholder) {
  25. //initialization
  26. self.isLoading = YES;
  27. [JSONHTTPClient getJSONFromURLWithString:urlString
  28. completion:^(NSDictionary *json, JSONModelError* e) {
  29. JSONModelError* initError = nil;
  30. blockSelf = [self initWithDictionary:json error:&initError];
  31. if (completeBlock) {
  32. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
  33. completeBlock(blockSelf, e?e:initError );
  34. });
  35. }
  36. self.isLoading = NO;
  37. }];
  38. }
  39. return placeholder;
  40. }
  41. + (void)getModelFromURLWithString:(NSString*)urlString completion:(JSONModelBlock)completeBlock
  42. {
  43. [JSONHTTPClient getJSONFromURLWithString:urlString
  44. completion:^(NSDictionary* jsonDict, JSONModelError* err)
  45. {
  46. JSONModel* model = nil;
  47. if(err == nil)
  48. {
  49. model = [[self alloc] initWithDictionary:jsonDict error:&err];
  50. }
  51. if(completeBlock != nil)
  52. {
  53. dispatch_async(dispatch_get_main_queue(), ^
  54. {
  55. completeBlock(model, err);
  56. });
  57. }
  58. }];
  59. }
  60. + (void)postModel:(JSONModel*)post toURLWithString:(NSString*)urlString completion:(JSONModelBlock)completeBlock
  61. {
  62. [JSONHTTPClient postJSONFromURLWithString:urlString
  63. bodyString:[post toJSONString]
  64. completion:^(NSDictionary* jsonDict, JSONModelError* err)
  65. {
  66. JSONModel* model = nil;
  67. if(err == nil)
  68. {
  69. model = [[self alloc] initWithDictionary:jsonDict error:&err];
  70. }
  71. if(completeBlock != nil)
  72. {
  73. dispatch_async(dispatch_get_main_queue(), ^
  74. {
  75. completeBlock(model, err);
  76. });
  77. }
  78. }];
  79. }
  80. @end