Skip to content

Instantly share code, notes, and snippets.

@zwaldowski
Created October 30, 2011 03:53
Show Gist options
  • Save zwaldowski/1325446 to your computer and use it in GitHub Desktop.
Save zwaldowski/1325446 to your computer and use it in GitHub Desktop.
AFHTTPRequestOperation subclass for simple XML parsing
//
// 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) SMXMLElement *responseXML;
+ (MRSimpleXMLRequestOperation *)XMLRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(NSHTTPURLResponse *response, SMXMLElement *XML))success
failure:(void (^)(NSHTTPURLResponse *response, NSError *error))failure;
@end
//
// 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, SMXMLElement *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 = [SMXMLElement rootElementForData: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