//
// PDFSingleton.h
// Muine
//
// Created by bjlee on 11. 1. 11..
// Copyright 2011 buggymind.com. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface PDFSingleton : NSObject {
}
+ (NSUInteger) do:(NSUInteger(^)(CGPDFDocumentRef))blk;
@end
//
// PDFSingleton.m
// Muine
//
// Created by bjlee on 11. 1. 11..
// Copyright 2011 buggymind.com. All rights reserved.
//
#import "PDFSingleton.h"
@implementation PDFSingleton
static CGPDFDocumentRef _pdf = NULL;
static int requestCount = 0;
+ (NSUInteger) do:(NSUInteger(^)(CGPDFDocumentRef))blk {
@synchronized ( self ) {
if ( ++requestCount == 6 ) {
CGPDFDocumentRelease(_pdf);
_pdf = NULL;
requestCount = 1;
}
if ( _pdf == NULL ) {
CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("xxx.pdf"), NULL, NULL);
_pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
}
return blk(_pdf);
}
}
@end
[PDFSingleton do:^(CGPDFDocumentRef pdf) {
CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index);
// ...
CGContextDrawPDFPage(ctx, page);
return (NSUInteger)0;
}];
네. 어떤 한 쓰레드에서 해당 PDF 파일을 사용해 뭔가 심각하고 오래 걸리는 일을 하고 있으면, 다른쪽에서는 그 일이 끝날 때 까지 무작정 기다려야 합니다.
그러니 동시성을 활용해 캐싱이라던가 프리패칭(pre-fetching)같은 것을 하고 싶다면, 이렇게 하면 곤란하겠죠. 연습문제 삼아, 각자 이 문제를 고민해보시기 바랍니다. :-)
댓글0