Using NSOperation is curious slowly

I am dealing with a problem here. This is the view that I use to view document thumbnails in my application. Since loading thumbnails slowed the main thread, I looked for workarounds and ended up executing NSOperation for the thumbnail creation task.

I am showing a view with empty thumbnail frames and a corresponding activity indicator to say that the user is "holding on, they are on their way." But it takes so much time that I think about putting some elevator music to make the wait more pleasant x_X.

I have this NSOperationQueue with 10 simultaneous actions. A setting that helped a bit with the download part, but a little small. Downloading one thumb, which still takes 6 seconds, and what a strange thing is that loading 10 accepts the same thing. The following code is the operation itself

@class ThumbnailView;

@protocol LoadThumbnailOperationDelegate;
@interface LoadThumbnailOperation : NSOperation {
    NSString *key;
    id<LoadThumbnailOperationDelegate> delegate;
    @private
    CGSize _size;
    CGPDFDocumentRef _docRef;
    UIImage * _image;
    NSInteger _page;


}
@property (nonatomic,retain) NSString * key;
@property (nonatomic,assign) id<LoadThumbnailOperationDelegate>delegate;
-(id)initWithPage:(NSInteger)page  operationKey:(NSString *)opKey fromDocRef:(CGPDFDocumentRef)docRef size:(CGSize)size delegate:(id<LoadThumbnailOperationDelegate>)aDelegate;
-(NSInteger)getPage ;
@protocol LoadThumbnailOperationDelegate <NSObject>

-(void)operation:(LoadThumbnailOperation*)operation finishedLoadingThumbnail:(UIImage*)image;
@end


@interface LoadThumbnailOperation (private)
-(UIImage*)makeThumbnailForPage:(NSInteger)page;

@end

@implementation LoadThumbnailOperation
@synthesize key;
@synthesize delegate;

-(id)initWithPage:(NSInteger)page  operationKey:(NSString *)opKey fromDocRef:(CGPDFDocumentRef)docRef size:(CGSize)size delegate:(id<LoadThumbnailOperationDelegate>)aDelegate{

    self = [super init];
    if (self) {

        self.key = opKey;
        _docRef = docRef;
        CGPDFDocumentRetain(_docRef);
        _size = size;
        _page = page;
        self.delegate = delegate;

    }
    return self;
}

-(void)main {
#if DEBUG
    NSLog( @"LoadThumbnailOperaiton.m -> main key:%@",key);
#endif
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    if (![self isCancelled]) {
        _image = [self makeThumbnailForPage:_page];
        [_image retain];
    }
    if(![self isCancelled]){
        if ([delegate respondsToSelector:@selector(operation:finishedLoadingThumbnail:)]) {
            [delegate operation:self finishedLoadingThumbnail:_image];
        }
    }

    [pool release];
}


-(void)dealloc {


    [key release];
    CGPDFDocumentRelease(_docRef);
    [_image release];
    [super dealloc];

}
#pragma mark - 
#pragma mark graphics 


-(UIImage*) makeThumbnailForPage :(NSInteger) page  {
#if DEBUG
    NSLog( @"LoadThumbnailOperaiton.m -> makeThumbnailForPage:%d",page);
#endif
    CGPDFPageRef pdfPage = CGPDFDocumentGetPage(_docRef, page);
    if (pdfPage !=NULL){
        CGPDFPageRetain(pdfPage);

    }else {
        NSAssert (pdfPage==NULL,@"pdf page NULL");
    }

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, 
                                                 _size.width, 
                                                 _size.height, 
                                                 8,                      /* bits per component*/
                                                 _size.width * 4,   /* bytes per row */
                                                 colorSpace, 
                                                 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextClipToRect(context, CGRectMake(0, 0, _size.width,_size.height));


    CGRect pdfPageRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
    CGRect contextRect = CGContextGetClipBoundingBox(context);
    CGAffineTransform transform =aspectFit(pdfPageRect, contextRect);

    CGContextConcatCTM(context, transform);
    CGContextDrawPDFPage(context, pdfPage);

    /* 
     create bitmap context
     */
    CGImageRef image = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    UIImage *uiImage = [[UIImage alloc]initWithCGImage:image];

    // clean up
    [uiImage autorelease];
    CGImageRelease(image);
    CGPDFPageRelease(pdfPage);
    return uiImage;
}

-(NSInteger)getPage {

    return _page;
}
@end

This is the delegate method on the view controller, which adds uploaded images

NOTE. This application is for iPad only.

Thank you in advance for your help.

+3
source share
2 answers

, . , , . , , , . , , - [NSObject performSelectorOnMainThread:].

. PDF . , ipad.

+3

. iPad.

iPad .

concurrency 11, , - , - .

, , - NSOperation; concurrency, , , , .

+1

Source: https://habr.com/ru/post/1789271/


All Articles