Skip to content

Commit

Permalink
Merge pull request AFNetworking#22 from steipete/master
Browse files Browse the repository at this point in the history
Fixes warnings, Improves UIImageView loading helper
  • Loading branch information
Mattt Thompson committed Sep 13, 2011
2 parents 714ccc1 + 1ec54e2 commit 8d9b7ac
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
43 changes: 24 additions & 19 deletions AFNetworking/AFHTTPRequestOperation.m
Expand Up @@ -23,6 +23,8 @@
#import "AFHTTPRequestOperation.h"
#import "AFNetworkActivityIndicatorManager.h"

#define AFHTTPMinContentLength 1024 * 1024 * 8

typedef enum {
AFHTTPOperationReadyState = 1,
AFHTTPOperationExecutingState = 2,
Expand Down Expand Up @@ -107,6 +109,14 @@ @implementation AFHTTPRequestOperation

static NSThread *_networkRequestThread = nil;

+ (void)networkRequestThreadEntryPoint:(id)object {
do {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NSRunLoop currentRunLoop] run];
[pool drain];
} while (YES);
}

+ (NSThread *)networkRequestThread {
static dispatch_once_t oncePredicate;

Expand All @@ -118,14 +128,6 @@ + (NSThread *)networkRequestThread {
return _networkRequestThread;
}

+ (void)networkRequestThreadEntryPoint:(id)object {
do {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[[NSRunLoop currentRunLoop] run];
[pool drain];
} while (YES);
}

+ (id)operationWithRequest:(NSURLRequest *)urlRequest
completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error))completion
{
Expand Down Expand Up @@ -246,16 +248,6 @@ - (BOOL)isConcurrent {
return YES;
}

- (void)start {
if (self.isFinished) {
return;
}

self.state = AFHTTPOperationExecutingState;

[self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:YES modes:[self.runLoopModes allObjects]];
}

- (void)operationDidStart {
self.connection = [[[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO] autorelease];

Expand All @@ -269,6 +261,16 @@ - (void)operationDidStart {

}

- (void)start {
if (self.isFinished) {
return;
}

self.state = AFHTTPOperationExecutingState;

[self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:YES modes:[self.runLoopModes allObjects]];
}

- (void)cancel {
self.isCancelled = YES;

Expand Down Expand Up @@ -297,7 +299,10 @@ - (void)connection:(NSURLConnection *)connection
if (self.outputStream) {
[self.outputStream open];
} else {
NSUInteger contentLength = MIN(MAX(abs(response.expectedContentLength), 1024), 1024 * 1024 * 8);
NSUInteger contentLength = MAX(abs(response.expectedContentLength), 1024);
if (contentLength < AFHTTPMinContentLength) {
contentLength = AFHTTPMinContentLength;
}
self.dataAccumulator = [NSMutableData dataWithCapacity:contentLength];
}
}
Expand Down
2 changes: 1 addition & 1 deletion AFNetworking/UIImageView+AFNetworking.h
Expand Up @@ -39,6 +39,6 @@
placeholderImage:(UIImage *)placeholderImage
imageSize:(CGSize)imageSize
options:(AFImageRequestOptions)options
block:(void (^)(UIImage *image))block;
block:(void (^)(UIImage *image, BOOL cacheUsed))block;

@end
25 changes: 15 additions & 10 deletions AFNetworking/UIImageView+AFNetworking.m
Expand Up @@ -84,9 +84,12 @@ - (void)setImageWithURL:(NSURL *)url
placeholderImage:(UIImage *)placeholderImage
imageSize:(CGSize)imageSize
options:(AFImageRequestOptions)options
block:(void (^)(UIImage *image))block
block:(void (^)(UIImage *image, BOOL cacheUsed))block
{
if (!url) {
// stop loading image
[self.imageRequestOperation cancel];
self.imageRequestOperation = nil;
return;
}

Expand All @@ -99,20 +102,22 @@ - (void)setImageWithURL:(NSURL *)url
self.image = cachedImage;

if (block) {
block(cachedImage);
block(cachedImage, YES);
}
} else {
self.image = placeholderImage;

self.imageRequestOperation = [AFImageRequestOperation operationWithRequest:request imageSize:imageSize options:options success:^(UIImage *image) {
if ([[request URL] isEqual:[[self.imageRequestOperation request] URL]]) {
self.image = image;
} else {
self.image = placeholderImage;
}

if (block) {
block(image);
if (self.imageRequestOperation && ![self.imageRequestOperation isCancelled]) {
if (block) {
block(image, NO);
}

if ([[request URL] isEqual:[[self.imageRequestOperation request] URL]]) {
self.image = image;
} else {
self.image = placeholderImage;
}
}
}];

Expand Down

0 comments on commit 8d9b7ac

Please sign in to comment.