Forked from zwaldowski/MRSimpleXMLRequestOperation.h
Last active
December 11, 2015 17:29
-
-
Save adamotte/4634987 to your computer and use it in GitHub Desktop.
Fix wrong return object (according to SMXMLDocument tag 1.0.1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MRSimpleXMLRequestOperation.h | |
// Marked | |
// | |
// Created by Zachary Waldowski on 10/22/11. | |
// Copyright (c) 2011 Dizzy Technology. All rights reserved. | |
// | |
#import "AFHTTPRequestOperation.h" | |
#import "SMXMLDocument.h" | |
@interface MRSimpleXMLRequestOperation : AFHTTPRequestOperation | |
@property (readonly, nonatomic, strong) SMXMLDocument *responseXML; | |
+ (MRSimpleXMLRequestOperation *)XMLRequestOperationWithRequest:(NSURLRequest *)urlRequest | |
success:(void (^)(NSHTTPURLResponse *response, SMXMLDocument *XML))success | |
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MRSimpleXMLRequestOperation.m | |
// Marked | |
// | |
// Created by Donald Waldowski on 10/22/11. | |
// Copyright (c) 2011 Dizzy Technology. All rights reserved. | |
// | |
#import "MRSimpleXMLRequestOperation.h" | |
static dispatch_queue_t MRSimpleXMLRequestOperationQueue; | |
static dispatch_queue_t MRSimpleXMLRequestOperationProcessingQueue() { | |
if (MRSimpleXMLRequestOperationQueue == NULL) { | |
MRSimpleXMLRequestOperationQueue = dispatch_queue_create("com.dizzytechology.marked.simple-xml-request", 0); | |
} | |
return MRSimpleXMLRequestOperationQueue; | |
} | |
@interface MRSimpleXMLRequestOperation () | |
@property (readwrite, nonatomic, strong) id responseXML; | |
@property (readwrite, nonatomic, strong) NSError *error; | |
+ (NSSet *)defaultAcceptableContentTypes; | |
@end | |
@implementation MRSimpleXMLRequestOperation | |
@synthesize responseXML, error; | |
+ (MRSimpleXMLRequestOperation *)XMLRequestOperationWithRequest:(NSURLRequest *)urlRequest | |
success:(void (^)(NSHTTPURLResponse *response, SMXMLDocument *XML))success | |
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure { | |
MRSimpleXMLRequestOperation *operation = [[self alloc] initWithRequest:urlRequest]; | |
__block MRSimpleXMLRequestOperation *safeOperation = operation; | |
operation.completionBlock = ^{ | |
if (safeOperation.isCancelled) | |
return; | |
if (safeOperation.error && failure) { | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
failure(safeOperation.response, safeOperation.error); | |
}); | |
} else if (!safeOperation.error && success) { | |
dispatch_async(MRSimpleXMLRequestOperationProcessingQueue(), ^{ | |
id XML = safeOperation.responseXML; | |
dispatch_async(dispatch_get_main_queue(), ^(void) { | |
if (safeOperation.error && failure) { | |
failure(safeOperation.response, safeOperation.error); | |
} else if (!safeOperation.error && success) { | |
success(safeOperation.response, XML); | |
} | |
}); | |
}); | |
} | |
}; | |
return operation; | |
} | |
- (id)responseXML { | |
if (!responseXML && [self isFinished]) { | |
NSError *parseError = nil; | |
self.responseXML = [SMXMLDocument documentWithData:self.responseData error:&parseError]; | |
self.error = parseError; | |
} | |
return responseXML; | |
} | |
+ (NSSet *)defaultAcceptableContentTypes { | |
return [NSSet setWithObjects:@"application/xml", @"text/xml", nil]; | |
} | |
- (id)initWithRequest:(NSURLRequest *)urlRequest { | |
if ((self = [super initWithRequest:urlRequest])) { | |
self.acceptableContentTypes = [[self class] defaultAcceptableContentTypes]; | |
} | |
return self; | |
} | |
+ (BOOL)canProcessRequest:(NSURLRequest *)request { | |
return [[self defaultAcceptableContentTypes] containsObject:[request valueForHTTPHeaderField:@"Accept"]]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment