Permalink
Browse files

Removed all NSOperationQueue logic in favor of NSURLConnections async…

…hronous methods. Deprecated decreate/imageImageLoadPriority. EGOImageView/EGOImageButton have new cancelImageLoad method, in their place. Updated the EGOCache submodule to use the newest code
  • Loading branch information...
1 parent fb8ae87 commit 8f1e921ace48833da3ddfd3f8a534289bcd9123e @shnhrrsn shnhrrsn committed Dec 2, 2009
@@ -29,7 +29,7 @@ - (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if(!newSuperview) {
- [imageView decreaseImageLoadPriority];
+ [imageView cancelImageLoad];
}
}
- (void)dealloc {
@@ -21,10 +21,10 @@
2826AB8E108D5160007FBD1D /* ExampleCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 2826AB8D108D5160007FBD1D /* ExampleCell.m */; };
2826AB90108D5258007FBD1D /* placeholder.png in Resources */ = {isa = PBXBuildFile; fileRef = 2826AB8F108D5258007FBD1D /* placeholder.png */; };
2826ABDB108D56AE007FBD1D /* icon.png in Resources */ = {isa = PBXBuildFile; fileRef = 2826ABDA108D56AE007FBD1D /* icon.png */; };
- 28661A7B109E3D6800975186 /* EGOImageLoadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 28661A7A109E3D6800975186 /* EGOImageLoadOperation.m */; };
28661A83109E3DAC00975186 /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 28661A82109E3DAC00975186 /* Reachability.m */; };
2892E4100DC94CBA00A64D0F /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2892E40F0DC94CBA00A64D0F /* CoreGraphics.framework */; };
28AD73600D9D9599002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD735F0D9D9599002E5188 /* MainWindow.xib */; };
+ 28BBA36B10C62E3300081AB5 /* EGOImageLoadConnection.m in Sources */ = {isa = PBXBuildFile; fileRef = 28BBA36A10C62E3300081AB5 /* EGOImageLoadConnection.m */; };
28C286E10D94DF7D0034E888 /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28C286E00D94DF7D0034E888 /* RootViewController.m */; };
28F335F11007B36200424DE2 /* RootViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28F335F01007B36200424DE2 /* RootViewController.xib */; };
/* End PBXBuildFile section */
@@ -50,13 +50,13 @@
2826AB8D108D5160007FBD1D /* ExampleCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ExampleCell.m; sourceTree = "<group>"; };
2826AB8F108D5258007FBD1D /* placeholder.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = placeholder.png; sourceTree = "<group>"; };
2826ABDA108D56AE007FBD1D /* icon.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = icon.png; sourceTree = "<group>"; };
- 28661A79109E3D6800975186 /* EGOImageLoadOperation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EGOImageLoadOperation.h; sourceTree = "<group>"; };
- 28661A7A109E3D6800975186 /* EGOImageLoadOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EGOImageLoadOperation.m; sourceTree = "<group>"; };
28661A81109E3DAC00975186 /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Reachability.h; sourceTree = "<group>"; };
28661A82109E3DAC00975186 /* Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Reachability.m; sourceTree = "<group>"; };
2892E40F0DC94CBA00A64D0F /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
28A0AAE50D9B0CCF005BE974 /* EGOImageLoadingDemo_Prefix.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EGOImageLoadingDemo_Prefix.pch; sourceTree = "<group>"; };
28AD735F0D9D9599002E5188 /* MainWindow.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = MainWindow.xib; sourceTree = "<group>"; };
+ 28BBA36910C62E3300081AB5 /* EGOImageLoadConnection.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EGOImageLoadConnection.h; sourceTree = "<group>"; };
+ 28BBA36A10C62E3300081AB5 /* EGOImageLoadConnection.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EGOImageLoadConnection.m; sourceTree = "<group>"; };
28C286DF0D94DF7D0034E888 /* RootViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RootViewController.h; sourceTree = "<group>"; };
28C286E00D94DF7D0034E888 /* RootViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RootViewController.m; sourceTree = "<group>"; };
28F335F01007B36200424DE2 /* RootViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = RootViewController.xib; sourceTree = "<group>"; };
@@ -140,8 +140,8 @@
children = (
2826AB14108D4295007FBD1D /* EGOImageLoader.h */,
2826AB15108D4295007FBD1D /* EGOImageLoader.m */,
- 28661A79109E3D6800975186 /* EGOImageLoadOperation.h */,
- 28661A7A109E3D6800975186 /* EGOImageLoadOperation.m */,
+ 28BBA36910C62E3300081AB5 /* EGOImageLoadConnection.h */,
+ 28BBA36A10C62E3300081AB5 /* EGOImageLoadConnection.m */,
);
name = EGOImageLoader;
path = ../EGOImageLoader;
@@ -275,8 +275,8 @@
2826AB4F108D4295007FBD1D /* EGOImageLoader.m in Sources */,
2826AB50108D4295007FBD1D /* EGOImageView.m in Sources */,
2826AB8E108D5160007FBD1D /* ExampleCell.m in Sources */,
- 28661A7B109E3D6800975186 /* EGOImageLoadOperation.m in Sources */,
28661A83109E3DAC00975186 /* Reachability.m in Sources */,
+ 28BBA36B10C62E3300081AB5 /* EGOImageLoadConnection.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Submodule EGOCache updated 2 files
+14 −5 EGOCache.h
+107 −17 EGOCache.m
@@ -24,8 +24,7 @@
- (id)initWithPlaceholderImage:(UIImage*)anImage; // delegate:nil
- (id)initWithPlaceholderImage:(UIImage*)anImage delegate:(id<EGOImageButtonDelegate>)aDelegate;
-- (void)increaseImageLoadPriority;
-- (void)decreaseImageLoadPriority;
+- (void)cancelImageLoad;
@property(nonatomic,retain) NSURL* imageURL;
@property(nonatomic,retain) UIImage* placeholderImage;
@@ -54,12 +54,8 @@ - (void)setImageURL:(NSURL *)aURL {
#pragma mark -
#pragma mark Image loading
-- (void)increaseImageLoadPriority {
- [[EGOImageLoader sharedImageLoader] increaseImageLoadPriorityForURL:self.imageURL];
-}
-
-- (void)decreaseImageLoadPriority {
- [[EGOImageLoader sharedImageLoader] decreaseImageLoadPriorityForURL:self.imageURL];
+- (void)cancelImageLoad {
+ [[EGOImageLoader sharedImageLoader] cancelLoadForURL:self.imageURL];
}
- (void)imageLoaderDidLoad:(NSNotification*)notification {
@@ -1,29 +1,46 @@
//
-// EGOImageLoadOperation.h
+// EGOImageLoadConnection.h
// EGOImageLoading
//
-// Created by Shaun Harrison on 10/28/09.
+// Created by Shaun Harrison on 12/1/09.
// Copyright 2009 enormego. All rights reserved.
//
+// This work is licensed under the Creative Commons GNU General Public License License.
+// To view a copy of this license, visit http://creativecommons.org/licenses/GPL/2.0/
+// or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
+//
#import <Foundation/Foundation.h>
-@protocol EGOImageLoadOperationDelegate;
-@interface EGOImageLoadOperation : NSOperation {
+@protocol EGOImageLoadConnectionDelegate;
+
+@interface EGOImageLoadConnection : NSObject {
@private
- NSURL* imageURL;
- NSTimeInterval timeoutInterval;
- id<EGOImageLoadOperationDelegate> delegate;
+ NSURL* _imageURL;
+ NSURLResponse* _response;
+ NSMutableData* _responseData;
+ NSURLConnection* _connection;
+ NSTimeInterval _timeoutInterval;
+
+ id<EGOImageLoadConnectionDelegate> _delegate;
}
-- (id)initWithImageURL:(NSURL*)aURL;
+- (id)initWithImageURL:(NSURL*)aURL delegate:(id)delegate;
+
+- (void)start;
+- (void)cancel;
+
+@property(nonatomic,readonly) NSData* responseData;
+@property(nonatomic,readonly,getter=imageURL) NSURL* imageURL;
+
+@property(nonatomic,retain) NSURLResponse* response;
+@property(nonatomic,assign) id<EGOImageLoadConnectionDelegate> delegate;
+
+@property(nonatomic,assign) NSTimeInterval timeoutInterval; // Default is 30 seconds
-@property(nonatomic,assign) id<EGOImageLoadOperationDelegate> delegate;
-@property(nonatomic,assign) NSTimeInterval timeoutInterval;
-@property(nonatomic,readonly) NSURL* imageURL;
@end
-@protocol EGOImageLoadOperationDelegate<NSObject>
-- (void)imageLoadOperation:(EGOImageLoadOperation*)operation didFinishWithData:(NSData*)imageData;
-- (void)imageLoadOperationDidFail:(EGOImageLoadOperation*)operation;
+@protocol EGOImageLoadConnectionDelegate<NSObject>
+- (void)connectionDidFinishLoading:(EGOImageLoadConnection *)connection;
+- (void)connection:(EGOImageLoadConnection *)connection didFailWithError:(NSError *)error;
@end
@@ -1,53 +1,84 @@
//
-// EGOImageLoadOperation.m
+// EGOImageLoadConnection.m
// EGOImageLoading
//
-// Created by Shaun Harrison on 10/28/09.
+// Created by Shaun Harrison on 12/1/09.
// Copyright 2009 enormego. All rights reserved.
//
+// This work is licensed under the Creative Commons GNU General Public License License.
+// To view a copy of this license, visit http://creativecommons.org/licenses/GPL/2.0/
+// or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
+//
-#import "EGOImageLoadOperation.h"
+#import "EGOImageLoadConnection.h"
-@implementation EGOImageLoadOperation
-@synthesize imageURL, delegate, timeoutInterval;
+@implementation EGOImageLoadConnection
+@synthesize imageURL=_imageURL, response=_response, delegate=_delegate, timeoutInterval=_timeoutInterval;
-- (id)initWithImageURL:(NSURL*)aURL {
+- (id)initWithImageURL:(NSURL*)aURL delegate:(id)delegate {
if((self = [super init])) {
- imageURL = [aURL retain];
+ _imageURL = [aURL retain];
+ self.delegate = delegate;
+ _responseData = [[NSMutableData alloc] init];
+ self.timeoutInterval = 30;
}
-
+
return self;
}
-- (void)main {
- NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
- [delegate retain];
+- (void)start {
+ NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:self.imageURL
+ cachePolicy:NSURLRequestReturnCacheDataElseLoad
+ timeoutInterval:self.timeoutInterval];
+ [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
- NSURLRequest* request = [[NSURLRequest alloc] initWithURL:self.imageURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:self.timeoutInterval];
- NSData* imageData = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
+ _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
+ NSLog(@"Starting: %@", _connection);
[request release];
-
-
- if(![self isCancelled] && imageData.length > 0) {
- [delegate imageLoadOperation:self didFinishWithData:imageData];
- } else {
- [delegate imageLoadOperationDidFail:self];
+}
+
+- (void)cancel {
+ NSLog(@"Cancelling: %@", _connection);
+ [_connection cancel];
+}
+
+- (NSData*)responseData {
+ return _responseData;
+}
+
+- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
+ if(connection != _connection) return;
+ [_responseData appendData:data];
+}
+
+- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
+ if(connection != _connection) return;
+ self.response = response;
+}
+
+- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
+ if(connection != _connection) return;
+ NSLog(@"%@", NSStringFromSelector(_cmd));
+ if([self.delegate respondsToSelector:_cmd]) {
+ [self.delegate connectionDidFinishLoading:self];
}
-
- [delegate release];
- delegate = nil;
- [pool release];
}
-- (void)setQueuePriority:(NSOperationQueuePriority)p {
- if(![self isFinished] && ![self isCancelled] && ![self isExecuting]) {
- [super setQueuePriority:p];
+- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
+ if(connection != _connection) return;
+ NSLog(@"%@", NSStringFromSelector(_cmd));
+ if([self.delegate respondsToSelector:_cmd]) {
+ [self.delegate connection:self didFailWithError:error];
}
}
+
- (void)dealloc {
- [imageURL release];
+ self.response = nil;
+ self.delegate = nil;
+ [_connection release];
+ [_imageURL release];
[super dealloc];
}
@@ -13,25 +13,23 @@
#import <Foundation/Foundation.h>
@protocol EGOImageLoaderObserver;
-@interface EGOImageLoader : NSObject {
+@interface EGOImageLoader : NSObject/*<NSURLConnectionDelegate>*/ {
@private
- NSDictionary* _currentOperations;
- NSMutableDictionary* currentOperations;
- NSOperationQueue* operationQueue;
+ NSDictionary* _currentConnections;
+ NSMutableDictionary* currentConnections;
+
+ NSLock* connectionsLock;
}
+ (EGOImageLoader*)sharedImageLoader;
- (BOOL)isLoadingImageURL:(NSURL*)aURL;
-- (void)cancelLoadForURL:(NSURL*)aURL;
- (void)loadImageForURL:(NSURL*)aURL observer:(id<EGOImageLoaderObserver>)observer;
- (UIImage*)imageForURL:(NSURL*)aURL shouldLoadWithObserver:(id<EGOImageLoaderObserver>)observer;
-- (void)increaseImageLoadPriorityForURL:(NSURL*)aURL;
-- (void)decreaseImageLoadPriorityForURL:(NSURL*)aURL;
+- (void)cancelLoadForURL:(NSURL*)aURL;
-@property(nonatomic,retain) NSDictionary* currentOperations;
-@property(nonatomic,assign,getter=isSuspended) BOOL suspended;
+@property(nonatomic,retain) NSDictionary* currentConnections;
@end
@protocol EGOImageLoaderObserver<NSObject>
Oops, something went wrong.

0 comments on commit 8f1e921

Please sign in to comment.