Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor code

  • Loading branch information...
commit 7397ea599557870d26827d41a376b5d7f540b15b 1 parent aa9f877
@GlennChiu authored
View
44 GCHTTPRequestOperation.h
@@ -6,27 +6,27 @@
// Copyright (c) 2012 Glenn Chiu. All rights reserved.
//
-// This code is distributed under the terms and conditions of the MIT license.
+// This code is distributed under the terms and conditions of the MIT license.
-// Copyright (c) 2012 Glenn Chiu
+// Copyright (c) 2012 Glenn Chiu
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import <Foundation/Foundation.h>
@@ -40,16 +40,17 @@
@property (strong, nonatomic) NSInputStream *inputStream;
@property (strong, nonatomic) NSError *error;
-+ (id)HTTPRequest:(GCNetworkRequest *)networkRequest
- callBackQueue:(dispatch_queue_t)queue
-completionHandler:(void(^)(NSData *data, NSHTTPURLResponse *response))completionBlock
- errorHandler:(void(^)(NSData *data, NSHTTPURLResponse *response, NSError *error))errorBlock;
++ (GCHTTPRequestOperation *)HTTPRequest:(GCNetworkRequest *)networkRequest
+ callBackQueue:(dispatch_queue_t)queue
+ completionHandler:(void(^)(NSData *data, NSHTTPURLResponse *response))completionBlock
+ errorHandler:(void(^)(NSData *data, NSHTTPURLResponse *response, NSError *error))errorBlock;
- (id)initWithHTTPRequest:(GCNetworkRequest *)networkRequest
callBackQueue:(dispatch_queue_t)queue
completionHandler:(void(^)(NSData *data, NSHTTPURLResponse *response))completionBlock
errorHandler:(void(^)(NSData *data, NSHTTPURLResponse *response, NSError *error))errorBlock;
+
- (void)startRequest;
- (void)cancelRequest;
@@ -64,6 +65,7 @@ completionHandler:(void(^)(NSData *data, NSHTTPURLResponse *response))completion
@end
+
extern const struct _userinfo_keys {
__unsafe_unretained NSString *userinfo_key;
struct
View
43 GCHTTPRequestOperation.m
@@ -6,27 +6,27 @@
// Copyright (c) 2012 Glenn Chiu. All rights reserved.
//
-// This code is distributed under the terms and conditions of the MIT license.
+// This code is distributed under the terms and conditions of the MIT license.
-// Copyright (c) 2012 Glenn Chiu
+// Copyright (c) 2012 Glenn Chiu
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import "GCHTTPRequestOperation.h"
#import "GCNetworkRequest.h"
@@ -35,6 +35,12 @@
#error GCNetworkRequest is ARC only. Use -fobjc-arc as compiler flag for this library
#endif
+#ifdef DEBUG
+# define GCNRLog(fmt, ...) NSLog((@"%s [Line %d]\n" fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
+#else
+# define GCNRLog(...) do {} while(0)
+#endif
+
#if TARGET_OS_IPHONE
# if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
# define GC_DISPATCH_RELEASE(v) do {} while(0)
@@ -103,7 +109,7 @@ @implementation GCHTTPRequestOperation
void(^_uploadProgressBlock)(NSUInteger bytesWritten, NSUInteger totalBytesWritten, NSUInteger totalBytesExpectedToWrite);
}
-+ (id)HTTPRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispatch_queue_t)queue completionHandler:(void(^)(NSData *data, NSHTTPURLResponse *response))completionBlock errorHandler:(void(^)(NSData *data, NSHTTPURLResponse *response, NSError *error))errorBlock
++ (GCHTTPRequestOperation *)HTTPRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispatch_queue_t)queue completionHandler:(void(^)(NSData *data, NSHTTPURLResponse *response))completionBlock errorHandler:(void(^)(NSData *data, NSHTTPURLResponse *response, NSError *error))errorBlock
{
return [[self alloc] initWithHTTPRequest:networkRequest callBackQueue:queue completionHandler:completionBlock errorHandler:errorBlock];
}
@@ -160,6 +166,7 @@ + (NSThread *)workerThread
static dispatch_once_t oncePredicate = (dispatch_once_t)0;
dispatch_once(&oncePredicate, ^{
+
workerThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadMain:) object:nil];
[workerThread start];
});
View
42 GCJSONRequestOperation.h
@@ -6,27 +6,27 @@
// Copyright (c) 2012 Glenn Chiu. All rights reserved.
//
-// This code is distributed under the terms and conditions of the MIT license.
+// This code is distributed under the terms and conditions of the MIT license.
-// Copyright (c) 2012 Glenn Chiu
+// Copyright (c) 2012 Glenn Chiu
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import "GCHTTPRequestOperation.h"
@@ -34,9 +34,9 @@
@interface GCJSONRequestOperation : GCHTTPRequestOperation
-+ (id)JSONRequest:(GCNetworkRequest *)networkRequest
- callBackQueue:(dispatch_queue_t)queue
-completionHandler:(void(^)(id JSON, NSHTTPURLResponse *response))completionBlock
- errorHandler:(void(^)(id JSON, NSHTTPURLResponse *response, NSError *error))errorBlock;
++ (GCJSONRequestOperation *)JSONRequest:(GCNetworkRequest *)networkRequest
+ callBackQueue:(dispatch_queue_t)queue
+ completionHandler:(void(^)(id JSON, NSHTTPURLResponse *response))completionBlock
+ errorHandler:(void(^)(id JSON, NSHTTPURLResponse *response, NSError *error))errorBlock;
@end
View
62 GCJSONRequestOperation.m
@@ -6,27 +6,27 @@
// Copyright (c) 2012 Glenn Chiu. All rights reserved.
//
-// This code is distributed under the terms and conditions of the MIT license.
+// This code is distributed under the terms and conditions of the MIT license.
-// Copyright (c) 2012 Glenn Chiu
+// Copyright (c) 2012 Glenn Chiu
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import "GCJSONRequestOperation.h"
@@ -39,7 +39,7 @@ @implementation GCJSONRequestOperation
NSError *_JSONError;
}
-+ (id)JSONRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispatch_queue_t)queue completionHandler:(void(^)(id JSON, NSHTTPURLResponse *response))completionBlock errorHandler:(void(^)(id JSON, NSHTTPURLResponse *response, NSError *error))errorBlock
++ (GCJSONRequestOperation *)JSONRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispatch_queue_t)queue completionHandler:(void(^)(id JSON, NSHTTPURLResponse *response))completionBlock errorHandler:(void(^)(id JSON, NSHTTPURLResponse *response, NSError *error))errorBlock
{
__block GCJSONRequestOperation *operation = nil;
@@ -49,28 +49,32 @@ + (id)JSONRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispatch_que
if (completionBlock)
{
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
+ dispatch_block_t block = ^{
id JSONObject = [operation processJSON:data];
dispatch_async(gc_dispatch_queue(queue), ^{completionBlock(JSONObject, response);});
operation = nil;
- });
+ };
+
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), block);
}
} errorHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
if (errorBlock)
{
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
+ dispatch_block_t block = ^{
id JSONObject = [operation processJSON:data];
dispatch_async(gc_dispatch_queue(queue), ^{errorBlock(JSONObject, response, [operation error]);});
operation = nil;
- });
+ };
+
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), block);
}
}];
return operation;
@@ -85,24 +89,14 @@ - (id)processJSON:(NSData *)data
NSError *error = nil;
JSONObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
- if (!JSONObject)
- {
- [self setError:error];
- }
+ if (!JSONObject) [self setError:error];
}
return JSONObject;
}
- (NSError *)error
{
- if (self->_JSONError)
- {
- return self->_JSONError;
- }
- else
- {
- return [super error];
- }
+ return (self->_JSONError) ?: [super error];
}
@end
View
34 GCNetworkQueue.h
@@ -6,27 +6,27 @@
// Copyright (c) 2012 Glenn Chiu. All rights reserved.
//
-// This code is distributed under the terms and conditions of the MIT license.
+// This code is distributed under the terms and conditions of the MIT license.
-// Copyright (c) 2012 Glenn Chiu
+// Copyright (c) 2012 Glenn Chiu
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import <Foundation/Foundation.h>
View
34 GCNetworkQueue.m
@@ -6,27 +6,27 @@
// Copyright (c) 2012 Glenn Chiu. All rights reserved.
//
-// This code is distributed under the terms and conditions of the MIT license.
+// This code is distributed under the terms and conditions of the MIT license.
-// Copyright (c) 2012 Glenn Chiu
+// Copyright (c) 2012 Glenn Chiu
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import "GCNetworkQueue.h"
#import "GCHTTPRequestOperation.h"
View
50 GCXMLRequestOperation.h
@@ -6,44 +6,44 @@
// Copyright (c) 2012 Glenn Chiu. All rights reserved.
//
-// This code is distributed under the terms and conditions of the MIT license.
+// This code is distributed under the terms and conditions of the MIT license.
-// Copyright (c) 2012 Glenn Chiu
+// Copyright (c) 2012 Glenn Chiu
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import "GCHTTPRequestOperation.h"
@interface GCXMLRequestOperation : GCHTTPRequestOperation
#if TARGET_OS_IPHONE
-+ (id)XMLParserRequest:(GCNetworkRequest *)networkRequest
- callBackQueue:(dispatch_queue_t)queue
- completionHandler:(void(^)(NSXMLParser *parser, NSHTTPURLResponse *response))completionBlock
- errorHandler:(void(^)(NSXMLParser *parser, NSHTTPURLResponse *response, NSError *error))errorBlock;
++ (GCXMLRequestOperation *)XMLParserRequest:(GCNetworkRequest *)networkRequest
+ callBackQueue:(dispatch_queue_t)queue
+ completionHandler:(void(^)(NSXMLParser *parser, NSHTTPURLResponse *response))completionBlock
+ errorHandler:(void(^)(NSXMLParser *parser, NSHTTPURLResponse *response, NSError *error))errorBlock;
#endif
#if __MAC_OS_X_VERSION_MIN_REQUIRED
-+ (id)XMLDocumentRequest:(GCNetworkRequest *)networkRequest
- callBackQueue:(dispatch_queue_t)queue
- completionHandler:(void(^)(NSXMLDocument *document, NSHTTPURLResponse *response))completionBlock
- errorHandler:(void(^)(NSXMLDocument *document, NSHTTPURLResponse *response, NSError *error))errorBlock;
++ (GCXMLRequestOperation *)XMLDocumentRequest:(GCNetworkRequest *)networkRequest
+ callBackQueue:(dispatch_queue_t)queue
+ completionHandler:(void(^)(NSXMLDocument *document, NSHTTPURLResponse *response))completionBlock
+ errorHandler:(void(^)(NSXMLDocument *document, NSHTTPURLResponse *response, NSError *error))errorBlock;
#endif
@end
View
76 GCXMLRequestOperation.m
@@ -6,27 +6,27 @@
// Copyright (c) 2012 Glenn Chiu. All rights reserved.
//
-// This code is distributed under the terms and conditions of the MIT license.
+// This code is distributed under the terms and conditions of the MIT license.
-// Copyright (c) 2012 Glenn Chiu
+// Copyright (c) 2012 Glenn Chiu
//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
#import "GCXMLRequestOperation.h"
@@ -40,7 +40,7 @@ @implementation GCXMLRequestOperation
}
#if TARGET_OS_IPHONE
-+ (id)XMLParserRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispatch_queue_t)queue completionHandler:(void(^)(NSXMLParser *parser, NSHTTPURLResponse *response))completionBlock errorHandler:(void(^)(NSXMLParser *parser, NSHTTPURLResponse *response, NSError *error))errorBlock
++ (GCXMLRequestOperation *)XMLParserRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispatch_queue_t)queue completionHandler:(void(^)(NSXMLParser *parser, NSHTTPURLResponse *response))completionBlock errorHandler:(void(^)(NSXMLParser *parser, NSHTTPURLResponse *response, NSError *error))errorBlock
{
__block GCXMLRequestOperation *operation = nil;
@@ -50,28 +50,32 @@ + (id)XMLParserRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispatc
if (completionBlock)
{
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
+ dispatch_block_t block = ^{
NSXMLParser *parser = [operation XMLParserFromData:data];
dispatch_async(gc_dispatch_queue(queue), ^{completionBlock(parser, response);});
operation = nil;
- });
+ };
+
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), block);
}
} errorHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
if (errorBlock)
{
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
+ dispatch_block_t block = ^{
NSXMLParser *parser = [operation XMLParserFromData:data];
dispatch_async(gc_dispatch_queue(queue), ^{errorBlock(parser, response, error);});
operation = nil;
- });
+ };
+
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), block);
}
}];
return operation;
@@ -84,7 +88,7 @@ - (NSXMLParser *)XMLParserFromData:(NSData *)data
#endif
#if __MAC_OS_X_VERSION_MIN_REQUIRED
-+ (id)XMLDocumentRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispatch_queue_t)queue completionHandler:(void(^)(NSXMLDocument *document, NSHTTPURLResponse *response))completionBlock errorHandler:(void(^)(NSXMLDocument *document, NSHTTPURLResponse *response, NSError *error))errorBlock
++ (GCXMLRequestOperation *)XMLDocumentRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispatch_queue_t)queue completionHandler:(void(^)(NSXMLDocument *document, NSHTTPURLResponse *response))completionBlock errorHandler:(void(^)(NSXMLDocument *document, NSHTTPURLResponse *response, NSError *error))errorBlock
{
__block GCXMLRequestOperation *operation = nil;
@@ -94,28 +98,32 @@ + (id)XMLDocumentRequest:(GCNetworkRequest *)networkRequest callBackQueue:(dispa
if (completionBlock)
{
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
+ dispatch_block_t block = ^{
NSXMLDocument *document = [operation XMLDocumentFromData:data];
dispatch_async(gc_dispatch_queue(queue), ^{completionBlock(document, response);});
operation = nil;
- });
+ };
+
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), block);
}
} errorHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
if (errorBlock)
{
- dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), ^{
+ dispatch_block_t block = ^{
NSXMLDocument *document = [operation XMLDocumentFromData:data];
dispatch_async(gc_dispatch_queue(queue), ^{errorBlock(document, response, [operation error]);});
operation = nil;
- });
+ };
+
+ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0ul), block);
}
}];
return operation;
@@ -130,10 +138,7 @@ - (NSXMLDocument *)XMLDocumentFromData:(NSData *)data
NSError *error = nil;
document = [[NSXMLDocument alloc] initWithData:data options:0 error:&error];
- if (!document)
- {
- [self setError:error];
- }
+ if (!document) [self setError:error];
}
return document;
}
@@ -141,14 +146,7 @@ - (NSXMLDocument *)XMLDocumentFromData:(NSData *)data
- (NSError *)error
{
- if (self->_XMLError)
- {
- return self->_XMLError;
- }
- else
- {
- return [super error];
- }
+ return (self->_XMLError) ?: [super error];
}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.