NSURLConnectionãå©ç¨ãã¦ã®éåæã並è¡ãã¦ã³ãã¼ã
iPhoneã¢ããªã®éçºã§ãè¤æ°ã®ç»åãéåæã«ãã¦ã³ãã¼ããããã£ãã®ã§ãNSURLConnectionãå©ç¨ãã¦ã®éåæã並è¡ãã¦ã³ãã¼ãã¯ã©ã¹ãå®è£ ãã¦ã¿ã¾ããã
åºæ¬çãªæ¹æ³
ãã¦ã³ãã¼ã対象ã®URLã¯Queueã®ããã¡ã«(å®éã¯NSMutableArray)ã§ç»é²ããããããªããã¡ã¨ãã.
Queueã«è¿½å ããããã¦ã³ãã¼ã対象URLã®è¦ç´ ãç£è¦ããæ°è¦ã¹ã¬ãããèµ·åããã
NSURLConnectionã«ããéåæãã¦ã³ãã¼ãå¦çã®èµ·åãè¡ã£ã¦ãã.
NSURLConnectionãããã¦ã³ãã¼ãå®äºãªã©ã¯ãNSURLConnectionã®Delegateã§ããã
åãã¦ã³ãã¼ãè¦ç´ 管çã®ã¯ã©ã¹(QueuedURLDownloaderElem)ã®ã¤ã³ã¹ã¿ã³ã¹ã«éç¥ãããããã«
ãã®ãã¦ã³ãã¼ãã¼(QueuedURLDownloader)ã«è¨å®ããã¦ãDelegate(
QueuedURLDownloaderDelegateãããã³ã«ã®å®è£
)ã®ã¡ã½ããã«è»¢éãããã¨ã«ããããã¦ã³ãã¼ã
ãããçµæãå¾ããã¨ãã§ããã
æ§æè¦ç´ - 以ä¸ã®ã¯ã©ã¹ããããã³ã«ã§æ§æãã
QueuedURLDownloader - ãã¦ã³ãã¼ãã¼ã®æ¬ä½ãå®è¡å¾ ã¡ãã¥ã¼ã管çãé çªã«å®è¡ãã¦ãã.
QueuedURLDownloaderElem - ãã¦ã³ãã¼ãè¦ç´ (1ã¤ã®URLã«å¯¾å¿)
QueuedURLDownloaderDelegate - ãã¦ã³ãã¼ãã®éç¥ãåããDelegateã®ãããã³ã«
Interfaceé¨
#import <Foundation/Foundation.h> /*! @class QueuedURLDownloader NSURLConnectionãå©ç¨ãã¦éåæã«ãã¡ã¤ã«ã®ãã¦ã³ãã¼ããè¡ã. åæåæã«åæã«ãã¦ã³ãã¼ãããæ大ãæå®, addURL:withUserInfoã§ãã¦ã³ãã¼ã対象ã®URLãQueueã«ç»é²ãã¦ãã¦ã³ãã¼ããè¡ã£ã¦ããã ãã®ç»é²ãè¡ããã¨ã«ãããéåæã«ãã¦ã³ãã¼ããè¡ããã. QueuedURLDownloaderDelegateãããã³ã«ãå®è£ ããDelegateã¯ã©ã¹ã® ã¡ã½ãã(didFinishLoading:withUserInfo)ã«ãããã¦ã³ãã¼ãããå 容ãåå¾ããã 以ä¸ã®æé ã§ããã¦ã³ãã¼ãã®å¦çãèµ·åãçµäºããããã ãã¦ã³ãã¼ãã®èµ·åå¦çã¯ãæ°è¦ã¹ã¬ããã§éåæã«è¡ãããã 1.ãã®ã¯ã©ã¹ã¤ã³ã¹ã¿ã³ã¹ã®åæå. 2.delegateã«QueuedURLDownloaderDelegateãããã³ã«ãå®è£ ããã¤ã³ã¹ã¿ã³ã¹ãè¨å®. 3.startã¡ã½ããã§ãã¦ã³ãã¼ãéå§(URLã追å ãããããã¦ã³ãã¼ããã¯ãããç¶æ ã«ãã). 4.ãã¦ã³ãã¼ã対象ã®URLã追å ãã¦ãã(addURL:withUnserInfo ã¡ã½ãã). 5.finishã¡ã½ããã§ãã以ä¸ããã¦ã³ãã¼ããããã®ããªããã¨ãéç¥. ä¾. // åæå QueuedURLDownloader *downloader = [[QueuedURLDownloader alloc] initWithMaxAtSameTime:3]; downloader.delegate = self; // QueuedURLDownloaderDelegateãããã³ã«ãå®è£ ãããã® // éå§ [downloader start]; // NSDictionary *dict; dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1, @"key1", nil] ; [downloader addURL:[NSURL URLWithString:urlString1 ] withUserInfo:dict]; dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value2, @"key2", nil] ; [downloader addURL:[NSURL URLWithString:urlString2 ] withUserInfo:dict]; dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value3, @"key3", nil] ; [downloader addURL:[NSURL URLWithString:urlString3 ] withUserInfo:dict]; ... // ãã以ä¸ããã¦ã³ãã¼ããããã®ããªããã¨ãéç¥ [downloader finishQueuing]; ======== å®è£ ã«ã¤ã㦠æ°è¦ã¹ã¬ããã«ãããQueueã«è¿½å ããããã¦ã³ãã¼ã対象URLã®è¦ç´ ãç£è¦ãã¦ãé çªã« NSURLConnectionã«ããéåæãã¦ã³ãã¼ãå¦çã®èµ·åãè¡ã£ã¦ãã. NSURLConnectionãããã¦ã³ãã¼ãå®äºãªã©ã¯ãNSURLConnectionã®Delegateã§ããã åãã¦ã³ãã¼ãè¦ç´ 管çã®ã¯ã©ã¹(QueuedURLDownloaderElem)ã®ã¤ã³ã¹ã¿ã³ã¹ã«éç¥ãããããã« ãã®ãã¦ã³ãã¼ãã¼(QueuedURLDownloader)ã«è¨å®ããã¦ãDelegate( QueuedURLDownloaderDelegateãããã³ã«ã®å®è£ )ã®ã¡ã½ããã«è»¢éãããã¨ã«ããããã¦ã³ãã¼ã ãããçµæãå¾ããã¨ãã§ããã */ @protocol QueuedURLDownloaderDelegate; @interface QueuedURLDownloader : NSObject { NSInteger maxAtSameTime; NSObject<QueuedURLDownloaderDelegate> *delegate; @private NSMutableArray *waitingQueue; NSMutableDictionary *runningDict; BOOL queuingFinished; BOOL completed; // å¦çãéå§ããã¦ãã? BOOL started; // åæ¢ãè¦æ±ããã¦ãã? BOOL stoppingRequired; NSInteger completedCount; NSLock *lock; NSTimeInterval timeoutInterval; } @property (nonatomic, retain) NSObject<QueuedURLDownloaderDelegate> *delegate; @property (readonly) NSInteger completedCount; /*! Timeoutæé,åä½:ç§,Default 10.0ç§ */ @property (nonatomic) NSTimeInterval timeoutInterval; /*! åæã«ãã¦ã³ãã¼ãããæ大æ°ãæå®ãã¦ã®åæå */ - (id) initWithMaxAtSameTime:(NSInteger)count; /*! ãã¦ã³ãã¼ãããURLã追å */ - (void) addURL:(NSURL *)URL withUserInfo:(NSDictionary *)info; /*! ãã¦ã³ãã¼ããéå§ */ - (void) start; /*! @method requireStopping @discussion ãã¦ã³ãã¼ãå¦çãåæ¢ãè¦æ±,å®éã«åæ¢ããããã¯ãisCompletedã§ç¢ºèªããå¿ è¦ããã */ - (void) requireStopping; /*! @method isCompleted @discussion Downloadå¦çãå®äºãã¦ãããã®å¤å® */ - (BOOL) isCompleted; /*! @method waitCompleted @discussion Downloadå¦çãå®äºããã¾ã§å¾ ã¤,ã¾ã éå§ããã¦ããªãå ´åã¯ãããã«è¿ãã */ - (void) waitCompleted; /*! ãã以ä¸ããã¦ã³ãã¼ãURLãããã®ããªããã¨ãéç¥. ãã®éç¥å¾ããã¦ã³ãã¼ãè¦ç´ ã®è¿½å ã¯åãä»ãããããç¾å¨å®è¡å¾ ã¡ãå®è¡ä¸ã®ãã¦ã³ãã¼ãã®å¦çãå®äºããã°ã ãã¦ã³ãã¼ãå¦çã®ã¹ã¬ãããçµäºããã */ - (void) finishQueuing; /*! ãã¦ã³ãã¼ãå®è¡å¾ ã¡è¦ç´ æ° */ - (NSInteger)waitingCount; /*! ãã¦ã³ãã¼ãå®è¡ä¸ã®è¦ç´ æ° */ - (NSInteger)runningCount; /*! åæã«ãã¦ã³ãã¼ãå¦çãè¡ãæå¤§æ° */ @property (nonatomic) NSInteger maxAtSameTime; /*! ç¾å¨ã®Queueã®è¦ç´ æ° */ //@property (readonly) NSInteger count; @end /*! ãã¦ã³ãã¼ãã¼(QueuedURLDownloader)ã®Delegate didFinishLoading:withUserInfoã¡ã½ããã«ãããQueuedURLDownloaderã®addURL:withUserInfo ã§æå®ããURLããã®ãã¦ã³ãã¼ãéç¥ãåãããã¡ã¤ã«ã®å 容ãå¾ãã */ @protocol QueuedURLDownloaderDelegate /*! @method didFinishLoading:withUserInfo: @discussion Downloadå®äºã®éç¥. @param data ãã¦ã³ãã¼ããããã¼ã¿ @param info QueuedURLDownloaderã®addURL:withUserInfoã§æ¸¡ãã userInfo */ - (void)didFinishLoading:(NSData *)data withUserInfo:(NSDictionary *)info; /*! @method downloadDidFailWithError:withUserInfo: @discussion Downloadæã®ã¨ã©ã¼çºçéç¥. @param error @param info QueuedURLDownloaderã®addURL:withUserInfoã§æ¸¡ãã userInfo */ - (void)downloadDidFailWithError:(NSError *)error withUserInfo:(NSDictionary *)info; @optional /*! @method didReceiveResponse:withUserInfo: @discussin æå®URLå ããã®ã¬ã¹ãã³ã¹ã®éç¥ @param response @param info QueuedURLDownloaderã®addURL:withUserInfoã§æ¸¡ãã userInfo */ - (void)didReceiveResponse:(NSURLResponse *)response withUserInfo:(NSDictionary *)info; /*! @method didReceiveResponse:withUserInfo: @discussin æå®URLå ããã®ãã¼ã¿ã¼ã®éç¥(é¨åçã«ãã¼ã¿åä¿¡ããå ´åãéç¥ããããã¨ããã) @param data @param info QueuedURLDownloaderã®addURL:withUserInfoã§æ¸¡ãã userInfo */ - (void)didReceiveData:(NSData *)data withUserInfo:(NSDictionary *)info; /*! @method didAllCompleted @discussion ãã¹ã¦ã®ãã¦ã³ãã¼ããå®äºããã¨ãã®éç¥ */ - (void)didAllCompleted; /*! @method dowloadCanceled @discussion ãã¦ã³ãã¼ãããã£ã³ã»ã«ãããã¨ãã®éç¥ */ - (void)dowloadCanceled; @end
å®è£ é¨
#import "QueuedURLDownloader.h" /*! @class QueuedURLDownloaderElem @discussion ãã¦ã³ãã¼ãè¦ç´ .ãã¦ã³ãã¼ãå URLãã¨ã«çæãã. URLã³ãã¯ã·ã§ã³ã®ç®¡çããã¼ã¿åå¾æã®éç¥ã¡ã½ããã®èµ·åãªã©ãè¡ã. */ @interface QueuedURLDownloaderElem : NSObject { @private NSDictionary *userInfo; NSURL *URL; NSObject<QueuedURLDownloaderDelegate> *delegate; NSURLConnection *con; QueuedURLDownloader *queuedDownloader; NSMutableData *data; } /*! ãã¦ã³ãã¼ãå URL */ @property (nonatomic, readonly) NSURL *URL; /*! ãã¼ã¿åå¾ãæ¥ç¶ãªã©ã®éç¥å Delegate */ @property (nonatomic, readonly) NSObject<QueuedURLDownloaderDelegate> *delegate; /* ãã¦ã³ãã¼ãå URLã¸ã®ã³ãã¯ã·ã§ã³ */ @property (nonatomic, retain) NSURLConnection *con; /*! ãããè¦ç´ ã¨ãã¦å«ãDownloader */ @property (nonatomic, readonly) QueuedURLDownloader *queuedDownloader; /*! ä»å æ å ± */ @property (nonatomic, readonly) NSDictionary *userInfo; - (id) initURL:(NSURL *)URL WithUserInfo:(NSDictionary *)info withDelegate:(NSObject<QueuedURLDownloaderDelegate> *) myDelegate withQueuedDownloader: (QueuedURLDownloader *)downloader; // NSURLConnection delegate - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)fragment; - (void)connectionDidFinishLoading:(NSURLConnection *)connection; @end @interface QueuedURLDownloader(Private) /*! @method run @discussion æªå®è¡Queueãããã¦ã³ãã¼ã対象æ å ±ãåãåºãããã¦ã³ãã¼ï¾ï¾ãèµ·åãèµ·å. Queueã空ã«ãªãã¾ã§ç¹°ãè¿ã. */ - (void) run; /*! @method finishDownload: @discussion ï¼è¦ç´ ã®ãã¦ã³ãã¼ããå®äºããã¨ãã®å¦ç. QueuedURLDownloaderElemããéç¥ããã. å¾ããããããã. */ - (void) finishDownload:(QueuedURLDownloaderElem *)elem; @end @implementation QueuedURLDownloaderElem @synthesize userInfo; @synthesize delegate; @synthesize URL; @synthesize con; @synthesize queuedDownloader; - (id) initURL:(NSURL *)RequestURL WithUserInfo:(NSDictionary *)info withDelegate:(NSObject<QueuedURLDownloaderDelegate> *) myDelegate withQueuedDownloader: (QueuedURLDownloader *)downloader { self = [super init]; URL = RequestURL; [URL retain]; userInfo = info; [userInfo retain]; delegate = myDelegate; [delegate retain]; con = nil; queuedDownloader = downloader; [queuedDownloader retain]; data = nil; return self; } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)fragment { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSLog(@"connection:didReceiveData"); if(fragment) { //NSLog(@"data length = %d", [fragment length]); } if ([delegate respondsToSelector:@selector(didReceiveData:withUserInfo:)]) [delegate didReceiveData:fragment withUserInfo:userInfo]; if(!data) { data = [[NSMutableData alloc] init]; } [data appendData:fragment]; [pool drain]; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSLog(@"connection:didFailWithError - %@", error); if ([delegate respondsToSelector:@selector(downloadDidFailWithError:withUserInfo:)]) [delegate downloadDidFailWithError:error withUserInfo:userInfo]; [self.queuedDownloader finishDownload:self]; [pool drain]; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSLog(@"connection:didReceiveResponse "); if ([delegate respondsToSelector:@selector(didReceiveResponse:withUserInfo:)]) [delegate didReceiveResponse:response withUserInfo:userInfo]; [pool drain]; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSLog(@"connection:connectionDidFinishLoading "); if(data) { NSLog(@"data length = %d", [data length]); } if(self.userInfo) { /* NSEnumerator *enumerator = [self.userInfo keyEnumerator]; id key; while ((key = [enumerator nextObject])) { // NSLog(@"key = %@, value = %@", key, [self.userInfo valueForKey:key]); } */ } if ([delegate respondsToSelector:@selector(didFinishLoading:withUserInfo:)]) [delegate didFinishLoading:data withUserInfo:userInfo]; [self.queuedDownloader finishDownload:self]; [pool drain]; NSLog(@"drain"); } - (void)dealloc { if(userInfo) [userInfo release]; if(delegate) [delegate release]; if(con) [con release]; if(URL) [URL release]; if(queuedDownloader) [queuedDownloader release]; if(data) [data release]; [super dealloc]; } @end @implementation QueuedURLDownloader @synthesize maxAtSameTime; @synthesize delegate; @synthesize completedCount; @synthesize timeoutInterval; - (id) init { [self initWithMaxAtSameTime:1]; return self; } - (id) initWithMaxAtSameTime:(NSInteger)count { self = [super init]; if(self == nil) return self; maxAtSameTime = count; waitingQueue = [[NSMutableArray alloc] init]; runningDict = [[NSMutableDictionary alloc] init]; queuingFinished = NO; completed = NO; completedCount = 0; timeoutInterval = 10.0; lock = [[NSLock alloc] init]; return self; } /* - (NSInteger) count { return [queue count]; } */ - (void) addURL:(NSURL *)URL withUserInfo:(NSDictionary *)info { QueuedURLDownloaderElem *elem = [[QueuedURLDownloaderElem alloc] initURL:URL WithUserInfo:info withDelegate:self.delegate withQueuedDownloader:self]; [lock lock]; if(!queuingFinished) { // ããURLã追å ããªãããéç¥ããã¦ããã [waitingQueue addObject:elem]; } [lock unlock]; } - (void)finishQueuing { queuingFinished = YES; } - (void) start { [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil]; } - (void) requireStopping { [lock lock]; stoppingRequired = YES; [lock unlock]; } - (BOOL) isCompleted { BOOL ret; [lock lock]; ret = completed; [lock unlock]; return ret; } - (void) waitCompleted { BOOL ret = NO; while (YES) { [lock lock]; ret = completed || !started; [lock unlock]; if(ret == YES) { if(stoppingRequired && started && [delegate respondsToSelector:@selector(dowloadCanceled)] ) { [delegate dowloadCanceled]; } break; } [NSThread sleepForTimeInterval:0.01f]; } } - (void) run { [lock lock]; started = YES; [lock unlock]; while (1) { BOOL downloading = NO; [lock lock]; if([waitingQueue count] == 0 && [runningDict count] == 0 && queuingFinished || stoppingRequired == YES) { [lock unlock]; break; } if([waitingQueue count] > 0 && [runningDict count] < maxAtSameTime) { // æªå®è¡ã®ãã®ã®ãã¡ã§ä¸çªå ã«Queueã«ç»é²ããããã®ãå¾ã // autoreleaseã®ObjectãLeakããã®ã§ AutoReleasePool NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; QueuedURLDownloaderElem *elem = [waitingQueue objectAtIndex:0]; [waitingQueue removeObjectAtIndex:0]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:elem.URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:timeoutInterval]; // DownLoadãéå§ elem.con = [[NSURLConnection alloc] initWithRequest:request delegate:elem]; [request release]; [runningDict setObject:elem forKey:elem.URL]; [elem.con start]; downloading = YES; [lock unlock]; [[NSRunLoop currentRunLoop] run]; [pool drain]; } else { [lock unlock]; } if(downloading == NO) { // Loopã§CPUçãããããªãããã«å°ãsleep [NSThread sleepForTimeInterval:0.01f]; } } // å®äºã®éç¥ if(delegate == nil) return; if([delegate respondsToSelector:@selector(didAllCompleted)] ) { [lock lock]; if(stoppingRequired) { [lock unlock]; } else { [lock unlock]; [delegate didAllCompleted]; } } [lock lock]; completed = YES; [lock unlock]; } - (void) finishDownload:(QueuedURLDownloaderElem *)elem { NSURL *URL = elem.URL; [lock lock]; if(elem) { [runningDict removeObjectForKey:URL]; [elem release]; completedCount += 1; } [lock unlock]; } - (NSInteger)waitingCount { NSInteger result; [lock lock]; result = [waitingQueue count]; [lock unlock]; return result; } - (NSInteger)runningCount { NSInteger result; [lock lock]; result = [runningDict count]; [lock unlock]; return result; } - (void) dealloc { if(delegate) { [delegate release]; delegate = nil; } [waitingQueue release]; [runningDict release]; [lock release]; [super dealloc]; } @end
ã¡ã¢ãªãªã¼ã¯å¯¾å¿
NSURLConnectionã®å¦çã®ä¸ã§ãautoReleaseãªãã¸ã§ã¯ãã®Memory Leak ããããããªã®ã§ãrunã¡ã½ããå ã«AutoReleaseãã¼ã«ã追å (2010.4.28)
åæå¦çã®åé¡
ãã¦ã³ãã¼ãä¸ã«ãViewãéãããªã©ãããå ´åã¯ãå¦çã®ä¸æã®è¦æ±ããããã®ã§ããã®ããã®requireStoppingã¡ã½ããã追å ã
ããã¨ãä¸æããããªããã¡ã«ãªãã¸ã§ã¯ãã®ç ´æ£ããããã®ãé²ããããä¸æorå®äºãå¾
ã£ã¦ãããã¯ããwaitCompletedã¡ã½ããã追å ã(2010.6.2)
使ç¨æé (ãã¦ã³ãã¼ããèµ·åããå´ï¼
åæåæã«åæã«ãã¦ã³ãã¼ãããæ大ãæå®,
addURL:withUserInfoã§ãã¦ã³ãã¼ã対象ã®URLãQueueã«ç»é²ãã¦ãã¦ã³ãã¼ããè¡ã£ã¦ããã
ãã®ç»é²ãè¡ããã¨ã«ãããéåæã«ãã¦ã³ãã¼ããè¡ããã.
QueuedURLDownloaderDelegateãããã³ã«ãå®è£
ããDelegateã¯ã©ã¹ã®
ã¡ã½ãã(didFinishLoading:withUserInfo)ã«ãããã¦ã³ãã¼ãããå
容ãåå¾ããã
以ä¸ã®æé ã§ããã¦ã³ãã¼ãã®å¦çãèµ·åãçµäºããããã
-
- ãã®ã¯ã©ã¹ã¤ã³ã¹ã¿ã³ã¹ã®åæå.
- delegateã«QueuedURLDownloaderDelegateãããã³ã«ãå®è£ ããã¤ã³ã¹ã¿ã³ã¹ãè¨å®.
- startã¡ã½ããã§ãã¦ã³ãã¼ãéå§(URLã追å ãããããã¦ã³ãã¼ããã¯ãããç¶æ ã«ãã).
- ãã¦ã³ãã¼ã対象ã®URLã追å ãã¦ãã(addURL:withUnserInfo ã¡ã½ãã).
- finishã¡ã½ããã§ãã以ä¸ããã¦ã³ãã¼ããããã®ããªããã¨ãéç¥.
ä¾ã¯ãããªæã
// åæå QueuedURLDownloader *downloader = [[QueuedURLDownloader alloc] initWithMaxAtSameTime:3]; downloader.delegate = self; // QueuedURLDownloaderDelegateãããã³ã«ãå®è£ ãããã® // éå§ [downloader start]; // NSDictionary *dict; dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1, @"key1", nil] ; [downloader addURL:[NSURL URLWithString:urlString1 ] withUserInfo:dict]; dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value2, @"key2", nil] ; [downloader addURL:[NSURL URLWithString:urlString2 ] withUserInfo:dict]; dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value3, @"key3", nil] ; [downloader addURL:[NSURL URLWithString:urlString3 ] withUserInfo:dict]; ... // ãã以ä¸ããã¦ã³ãã¼ããããã®ããªããã¨ãéç¥ [downloader finishQueuing];
使ç¨æé (ãã¦ã³ãã¼ããªã©ã®éç¥ãåããããï¼
Delegateã¯ãï¼è¦ç´ (URL - ãã¡ã¤ã«)ã®ãã¦ã³ãã¼ããå®äºããããã®éç¥ã¡ã½ãããã¨ã©ã¼å®äºããå ´åã®éç¥ã¡ã½ãããå¿ é ã¨ãªãã
/*! ãã¦ã³ãã¼ãå®äºæã®éç¥ */ - (void)didFinishLoading:(NSData *)data withUserInfo:(NSDictionary *)info { // å¼æ°dataããã¦ã³ãã¼ãããããã¼ã¿ãªã®ã§ã表示ãããªãããã¡ã¤ã«ä¿åãããªã... } /*! ãã¦ã³ãã¼ãã¨ã©ã¼æã®éç¥ */ - (void)downloadDidFailWithError:(NSError *)error withUserInfo:(NSDictionary *)info { }
ãã¨ãUIViewControllerãªã©ãããã®ãã¦ã³ãã¼ãæ©è½ã使ã£ã¦ããå ´åããã¦ã³ãã¼ãä¸ã«Viewãç ´æ£ãããã¨ãã¦ã³ãã¼ããªãã¸ã§ã¯ããç ´æ£ããã¦ä¸æ£ã¡ã¢ãªã¢ã¯ã»ã¹ãèµ·ãã£ããããã
ããã®åé¿ã®ããã«ã¯ãViewãé表示ã«ãªãã¿ã¤ãã³ã°ãªã©(viewDidDisappear:)ã«ãä¸æã®è¦æ±ã¨å®å¾
ã¡ãªã©ãããã
- (void)viewDidDisappear:(BOOL)animated { // Downloadå®è¡ä¸ã®å ´å,åæ¢ãè¦æ±ãå®äºããã¾ã§å¾ 㤠if(downloader) { [downloader requireStopping]; [downloader waitCompleted]; } }