Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Update for MasOS 10.6 #1

Merged
merged 5 commits into from

2 participants

Anton Sidelnikov Justin Palmer
Anton Sidelnikov

Hello.

Saw your example while roaming the web, decided ti upgrade it a bit, cause it doesn't working for maxos 10.6, since they have changed concurrent NSOperation model. Previously concurrent operations were run from main thread, but now they also use threads from threadpool, so NSURLConnections fails to return any result, 'cause it expects to be run from main thread.

Maybe it shouldn't be pulled to master (but techs go on), it's up to you to decide.

And sorry for project, it's for XCode 4 && iOS 4.3, don't have ability to invoke older SDK, just finished company-wide upgrade :)

meredian added some commits
Anton Sidelnikov meredian Modified project to run with XCode 4 and macOS 10.6 or iOS 4.3 0ccf397
Anton Sidelnikov meredian Important fix for example to work properly under 10.6/4.3
Until 10.6 non-concurrent operations were run from background threads,
and concurrent - always from main. It has changed in 10.6, and both
types use background threads now, but NSURLConnection expects to be
run from main thread. So this important check is required for now.
c72da34
Anton Sidelnikov meredian Remove trailing whitespaces f7c5656
Anton Sidelnikov meredian Some memory management updates to remove leaks
Surprised object were not retained. Feature of iOS 3.0?
61435e0
Anton Sidelnikov meredian Add reaction to isCancelled 09e75bc
Justin Palmer Caged merged commit 8361de0 into from
Justin Palmer
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 30, 2011
  1. Anton Sidelnikov
  2. Anton Sidelnikov

    Important fix for example to work properly under 10.6/4.3

    meredian authored
    Until 10.6 non-concurrent operations were run from background threads,
    and concurrent - always from main. It has changed in 10.6, and both
    types use background threads now, but NSURLConnection expects to be
    run from main thread. So this important check is required for now.
  3. Anton Sidelnikov

    Remove trailing whitespaces

    meredian authored
  4. Anton Sidelnikov

    Some memory management updates to remove leaks

    meredian authored
    Surprised object were not retained. Feature of iOS 3.0?
  5. Anton Sidelnikov

    Add reaction to isCancelled

    meredian authored
This page is out of date. Refresh to see the latest.
2  Classes/ConcurrentOperationTestAppDelegate.h
View
@@ -14,6 +14,8 @@
@interface ConcurrentOperationTestAppDelegate : NSObject <SomeNetworkOperationDelegate, UIApplicationDelegate> {
UIWindow *window;
ConcurrentOperationTestViewController *viewController;
+
+ NSOperationQueue *queue;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
18 Classes/ConcurrentOperationTestAppDelegate.m
View
@@ -22,30 +22,29 @@ @implementation ConcurrentOperationTestAppDelegate
@synthesize viewController;
-- (void)applicationDidFinishLaunching:(UIApplication *)application {
-
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
+- (void)applicationDidFinishLaunching:(UIApplication *)application {
+
+ queue = [[[NSOperationQueue alloc] init] retain];
[queue setMaxConcurrentOperationCount:3];
-
+
for(NSURL *url in [self urlFixtures]) {
- SomeNetworkOperation *op = [[SomeNetworkOperation alloc] init];
+ SomeNetworkOperation *op = [[[SomeNetworkOperation alloc] init] autorelease];
op.delegate = self;
op.urlToLoad = url;
[queue addOperation:op];
- [op release];
}
-
+
viewController.countLabel.text = @"Images Loaded: 0";
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
-- (void)didFinishLoad:(NSDictionary *)info
+- (void)didFinishLoad:(NSDictionary *)info
{
NSURL *url = [info valueForKey:@"url"];
totalImagesLoaded += 1;
viewController.countLabel.text = [NSString stringWithFormat:@"Images Loaded: %i/%i", totalImagesLoaded, [[self urlFixtures] count]];
-
+
NSLog(@"FINISHED LOADING URL:%@", url);
}
@@ -73,6 +72,7 @@ - (NSArray *)urlFixtures
- (void)dealloc {
+ [queue release];
[viewController release];
[window release];
[super dealloc];
10 Classes/ConcurrentOperationTestViewController.m
View
@@ -48,15 +48,15 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
*/
- (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
+ // Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
-
- // Release any cached data, images, etc that aren't in use.
+
+ // Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
+ // Release any retained subviews of the main view.
+ // e.g. self.myOutlet = nil;
}
4 Classes/SomeNetworkOperation.h
View
@@ -12,10 +12,10 @@
@interface SomeNetworkOperation : NSOperation {
id <SomeNetworkOperationDelegate>_delegate;
NSURL *_urlToLoad;
-
+
NSURLConnection *_connection;
NSMutableData *_responseData;
-
+
BOOL _isFinished;
BOOL _isExecuting;
}
38 Classes/SomeNetworkOperation.m
View
@@ -17,11 +17,11 @@ @implementation SomeNetworkOperation
- (id)init
{
- if(self = [super init]) {
+ if((self = [super init])) {
_isExecuting = NO;
_isFinished = NO;
}
-
+
return self;
}
@@ -37,24 +37,37 @@ - (BOOL)isConcurrent {
- (void)start
{
+ if (![NSThread isMainThread])
+ {
+ [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
+ return;
+ }
+
+ if ([self isCancelled]) {
+ [self willChangeValueForKey:@"isFinished"];
+ _isFinished = YES;
+ [self didChangeValueForKey:@"isFinished"];
+ return;
+ }
+
[self willChangeValueForKey:@"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:@"isExecuting"];
-
+
NSURLRequest *request = [NSURLRequest requestWithURL:self.urlToLoad cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
-
+
if(_connection) {
- _responseData = [[NSMutableData alloc] init];
+ _responseData = [[[NSMutableData alloc] init] retain];
} else {
[self finish];
- }
+ }
}
- (void)finish {
[_connection release];
_connection = nil;
-
+
[_responseData release];
_responseData = nil;
@@ -85,23 +98,22 @@ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
-
+
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
-
+
[self finish];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
- UIImage *img = [[UIImage alloc] initWithData:_responseData];
+ UIImage *img = [[[UIImage alloc] initWithData:_responseData] autorelease];
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:img, @"img", _urlToLoad, @"url", nil];
-
+
if([_delegate respondsToSelector:@selector(didFinishLoad:)]) {
[_delegate performSelector:@selector(didFinishLoad:) withObject:info];
}
-
- [img release];
+
[self finish];
}
@end
14 ConcurrentOperationTest.xcodeproj/project.pbxproj
View
@@ -141,7 +141,11 @@
isa = PBXProject;
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "ConcurrentOperationTest" */;
compatibilityVersion = "Xcode 3.1";
+ developmentRegion = English;
hasScannedForEncodings = 1;
+ knownRegions = (
+ en,
+ );
mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */;
projectDirPath = "";
projectRoot = "";
@@ -188,7 +192,9 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = ConcurrentOperationTest_Prefix.pch;
INFOPLIST_FILE = "ConcurrentOperationTest-Info.plist";
+ IPHONEOS_DEPLOYMENT_TARGET = 4.3;
PRODUCT_NAME = ConcurrentOperationTest;
+ TARGETED_DEVICE_FAMILY = 1;
};
name = Debug;
};
@@ -200,7 +206,9 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = ConcurrentOperationTest_Prefix.pch;
INFOPLIST_FILE = "ConcurrentOperationTest-Info.plist";
+ IPHONEOS_DEPLOYMENT_TARGET = 4.3;
PRODUCT_NAME = ConcurrentOperationTest;
+ TARGETED_DEVICE_FAMILY = 1;
};
name = Release;
};
@@ -212,8 +220,9 @@
GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
+ IPHONEOS_DEPLOYMENT_TARGET = 4.2;
PREBINDING = NO;
- SDKROOT = iphoneos3.0;
+ SDKROOT = iphoneos4.3;
};
name = Debug;
};
@@ -225,8 +234,9 @@
GCC_C_LANGUAGE_STANDARD = c99;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
+ IPHONEOS_DEPLOYMENT_TARGET = 4.2;
PREBINDING = NO;
- SDKROOT = iphoneos3.0;
+ SDKROOT = iphoneos4.3;
};
name = Release;
};
2  main.m
View
@@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>
int main(int argc, char *argv[]) {
-
+
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
Something went wrong with that request. Please try again.