Skip to content

Commit

Permalink
Less NSOperations use
Browse files Browse the repository at this point in the history
  • Loading branch information
djphoenix committed May 14, 2015
1 parent e2d3abc commit c93eefe
Showing 1 changed file with 44 additions and 60 deletions.
104 changes: 44 additions & 60 deletions pxSVG/pxSVGLayer.m
Expand Up @@ -64,37 +64,29 @@ - (void)loadURL:(NSURL *)url
[self clean];
__weak pxSVGLayer *weakself = self;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
__block NSURLResponse *resp;
__block NSError *err;
NSURLResponse *resp;
NSError *err;
NSData *data;
data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:30] returningResponse:&resp error:&err];
if (!err && [resp isKindOfClass:[NSHTTPURLResponse class]] && (((NSHTTPURLResponse*)resp).statusCode != 200))
err = [NSError errorWithDomain:@"pxSVGLoader.httpStatus" code:((NSHTTPURLResponse*)resp).statusCode userInfo:nil];
if (!err && !data)
err = [NSError errorWithDomain:@"pxSVGLoader.emptyData" code:0 userInfo:nil];
NSString *str = data?[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]:nil;
data = nil, resp = nil;
if ([op isCancelled]) {
op = nil, data = nil, resp = nil, err = nil;
op = nil, str = nil, err = nil;
return;
}
__block NSString *str = data?[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]:nil;
data = nil;
__block NSBlockOperation *sync = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
sync = nil;
if ([op isCancelled] || !weakself) {
op = nil, resp = nil, err = nil, str = nil;
return;
}
weakself.loadOperation = op = nil;
if (!err && [resp isKindOfClass:[NSHTTPURLResponse class]] && (((NSHTTPURLResponse*)resp).statusCode != 200))
err = [NSError errorWithDomain:@"pxSVGLoader.httpStatus" code:((NSHTTPURLResponse*)resp).statusCode userInfo:nil];
if (!err && !str)
err = [NSError errorWithDomain:@"pxSVGLoader.emptyData" code:0 userInfo:nil];
if (err) {
resp = nil, str = nil;
[weakself loadError:err];
err = nil;
return;
}
[weakself loadString:str];
resp = nil, err = nil, str = nil;
} }];
[[NSOperationQueue mainQueue] addOperations:@[sync] waitUntilFinished:YES];
op = nil;
if (err) {
str = nil;
[weakself performSelectorOnMainThread:@selector(loadError:) withObject:err waitUntilDone:NO modes:@[NSRunLoopCommonModes]];
err = nil;
return;
}
[weakself performSelectorOnMainThread:@selector(loadString:) withObject:str waitUntilDone:NO modes:@[NSRunLoopCommonModes]];
str = nil, err = nil;
} }];
op.name = url.absoluteString;
op.threadPriority = 0.1f;
Expand All @@ -111,26 +103,19 @@ - (void)loadString:(NSString *)string
[self clean];
__weak pxSVGLayer *weakself = self;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
__block pxSVGImage *img = [pxSVGImage svgImageWithXML:string];
pxSVGImage *img = [pxSVGImage svgImageWithXML:string];
if ([op isCancelled]) {
op = nil, img = nil;
return;
}
__block NSBlockOperation *sync = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
sync = nil;
if ([op isCancelled] || !weakself) {
op = nil, img = nil;
return;
}
weakself.parseOperation = op = nil;
if (!img) {
img = nil;
return [weakself loadError:[NSError errorWithDomain:@"pxSVGParser.parseError" code:0 userInfo:nil]];
}
[weakself loadImage:img];
op = nil;
if (!img) {
img = nil;
} }];
[[NSOperationQueue mainQueue] addOperations:@[sync] waitUntilFinished:YES];
[weakself performSelectorOnMainThread:@selector(loadError:) withObject:[NSError errorWithDomain:@"pxSVGParser.parseError" code:0 userInfo:nil] waitUntilDone:NO modes:@[NSDefaultRunLoopMode]];
return;
}
[weakself performSelectorOnMainThread:@selector(loadImage:) withObject:img waitUntilDone:NO modes:@[NSRunLoopCommonModes]];
img = nil;
} }];
op.threadPriority = 0.1f;
[[self.class parseQueue] addOperation:self.parseOperation=op];
Expand All @@ -142,31 +127,30 @@ - (void)loadImage:(pxSVGImage*)image
CGRect bounds = image.bounds;
__weak pxSVGLayer *weakself = self;
__block NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
__block CALayer *img = [image makeLayer];
CALayer *img = [image makeLayer];
img.bounds = bounds;
if ([op isCancelled]) {
op = nil, img = nil;
return;
}
__block NSBlockOperation *sync = [NSBlockOperation blockOperationWithBlock:^{ @autoreleasepool {
sync = nil;
if ([op isCancelled] || !weakself) {
img = nil, op = nil;
return;
}
weakself.layerOperation = op = nil;
[weakself clean];
[weakself addSublayer:img];
img = nil;
weakself.contentRect = bounds;
if ([weakself.svgDelegate respondsToSelector:@selector(svgLayerDidLoadImage:)])
[weakself.svgDelegate svgLayerDidLoadImage:weakself];
} }];
[[NSOperationQueue mainQueue] addOperations:@[sync] waitUntilFinished:YES];
op = nil;
[weakself performSelectorOnMainThread:@selector(loadLayer:) withObject:img waitUntilDone:NO modes:@[NSRunLoopCommonModes]];
img = nil;
} }];
op.threadPriority = 0.2f;
[[self.class layererQueue] addOperation:self.layerOperation=op];
}

- (void)loadLayer:(CALayer*)layer
{
[self clean];
self.contentRect = layer.bounds;
layer.bounds = (CGRect){CGPointZero,layer.frame.size};
[self addSublayer:layer];
if ([self.svgDelegate respondsToSelector:@selector(svgLayerDidLoadImage:)])
[self.svgDelegate svgLayerDidLoadImage:self];
}

- (void)loadError:(NSError *)error
{
[self clean];
Expand All @@ -177,9 +161,9 @@ - (void)loadError:(NSError *)error
- (void)clean
{
@autoreleasepool {
if (self.loadOperation) [self.loadOperation cancel];
if (self.parseOperation) [self.parseOperation cancel];
if (self.layerOperation) [self.layerOperation cancel];
if (self.loadOperation) {[self.loadOperation cancel];self.loadOperation = nil;}
if (self.parseOperation) {[self.parseOperation cancel];self.parseOperation = nil;}
if (self.layerOperation) {[self.layerOperation cancel];self.layerOperation = nil;}
while (self.sublayers.count) [self.sublayers.firstObject removeFromSuperlayer];
self.contentRect = CGRectZero;
}
Expand Down

0 comments on commit c93eefe

Please sign in to comment.