Ben Copsey

Release blocks on main thread (hopefully closes gh-91)

... ... @@ -24,7 +24,7 @@
#import "ASIDataCompressor.h"
// Automatically set on build
NSString *ASIHTTPRequestVersion = @"v1.7-131 2010-11-07";
NSString *ASIHTTPRequestVersion = @"v1.7-132 2010-11-10";
NSString* const NetworkRequestErrorDomain = @"ASIHTTPRequestErrorDomain";
... ... @@ -178,6 +178,7 @@ static NSOperationQueue *sharedQueue = nil;
#if NS_BLOCKS_AVAILABLE
- (void)performBlockOnMainThread:(ASIBasicBlock)block;
- (void)releaseBlocksOnMainThread;
#endif
@property (assign) BOOL complete;
... ... @@ -362,21 +363,59 @@ static NSOperationQueue *sharedQueue = nil;
[dataDecompressor release];
#if NS_BLOCKS_AVAILABLE
[completionBlock release];
[failureBlock release];
[startedBlock release];
[headersReceivedBlock release];
[bytesReceivedBlock release];
[bytesSentBlock release];
[downloadSizeIncrementedBlock release];
[uploadSizeIncrementedBlock release];
[dataReceivedBlock release];
[proxyAuthenticationNeededBlock release];
[authenticationNeededBlock release];
[self releaseBlocksOnMainThread];
#endif
[super dealloc];
}
#if NS_BLOCKS_AVAILABLE
- (void)releaseBlocksOnMainThread
{
NSMutableArray *blocks = [NSMutableArray array];
if (completionBlock) {
[blocks addObject:completionBlock];
}
if (failureBlock) {
[blocks addObject:failureBlock];
}
if (startedBlock) {
[blocks addObject:startedBlock];
}
if (headersReceivedBlock) {
[blocks addObject:headersReceivedBlock];
}
if (bytesReceivedBlock) {
[blocks addObject:bytesReceivedBlock];
}
if (bytesSentBlock) {
[blocks addObject:bytesSentBlock];
}
if (downloadSizeIncrementedBlock) {
[blocks addObject:downloadSizeIncrementedBlock];
}
if (uploadSizeIncrementedBlock) {
[blocks addObject:uploadSizeIncrementedBlock];
}
if (dataReceivedBlock) {
[blocks addObject:dataReceivedBlock];
}
if (proxyAuthenticationNeededBlock) {
[blocks addObject:proxyAuthenticationNeededBlock];
}
if (authenticationNeededBlock) {
[blocks addObject:authenticationNeededBlock];
}
[[self class] performSelectorOnMainThread:@selector(releaseBlocks:) withObject:blocks waitUntilDone:[NSThread isMainThread]];
}
// Always called on main thread
+ (void)releaseBlocks:(NSArray *)blocks
{
// Blocks will be released when this method exits
}
#endif
#pragma mark setup request
- (void)addRequestHeader:(NSString *)header value:(NSString *)value
... ...
... ... @@ -17,6 +17,18 @@
- (BOOL)shouldRunOnMainThread { return YES; }
#if NS_BLOCKS_AVAILABLE
#if TARGET_OS_IPHONE
// It isn't safe to allow the view to deallocate on a thread other than the main thread / web thread, so this test is designed to cause a crash semi-reliably
- (void)testBlockMainThreadSafety
{
NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0,0,200,200)] autorelease];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^(ASIHTTPRequest *request) {[webView loadHTMLString:[request responseString] baseURL:url]; }];
[request startAsynchronous];
}
#endif
- (void)testBlocks
{
NSData *dataToSend = [@"This is my post body" dataUsingEncoding:NSUTF8StringEncoding];
... ...
This diff was suppressed by a .gitattributes entry.