私のサーバーからの進捗状況を伴うファイルのダウンロードを実装したいと考えています。 私はNSURLConnectionとNSURLConnectionDataDelegate:接続コールバックが起動されない
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://example.com"]];
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
DownloadCallback *dc = [[DownloadCallback alloc] initWithCallbackProgress:^(long long res){
NSLog(@"%lld", res);
} withCallbackReady:^(long long res){
NSLog(@"READY %lld", res);
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
}];
} withCallbackError:^(NSError * error) {
NSLog(@"READY %@", error.domain);
}];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:dc];
// [connection setDelegateQueue:[[NSOperationQueue alloc] init]];
[connection start];
ヘッダから委任されたカスタムクラス使用していますI私のコード:
@interface DownloadCallback: NSObject<NSURLConnectionDataDelegate>{
@private void (^_progressHandler)(long long someParameter);
@private void (^_readyHandler)(long long someParameter);
@private void (^_errorHandler)(NSError *someParameter);
}
-(id) initWithCallbackProgress:(void(^)(long long))handler withCallbackReady:(void(^)(long long))handlerReady withCallbackError:(void(^)(NSError*))handlerError;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
@end
体:
@implementation DownloadCallback
-(id) initWithCallbackProgress:(void(^)(long long))handler withCallbackReady:(void(^)(long long))handlerReady withCallbackError:(void(^)(NSError*))handlerError{
self = [super init];
if (self) {
_progressHandler = [handler copy];
_readyHandler = [handlerReady copy];
_errorHandler = [handlerError copy];
}
return self;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// self.expectedTotalSize = response.expectedContentLength;
// Call completion handler.
if (_readyHandler != nil)
_readyHandler(response.expectedContentLength);
// Clean up.
// [_completionHandler release];
_readyHandler = nil;
_progressHandler = nil;
_errorHandler = nil;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// self.recievedData += data.length;
if (_progressHandler != nil)
_progressHandler(data.length);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (_errorHandler != nil)
_errorHandler(error);
}
@end
をしかし、コールバック・イベントが発生しませんが!まったく!
簡単な同期コードの作業prefectly:
// Send a synchronous request
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
NSURLResponse * response = nil;
NSError * error = nil;
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
if (error == nil) {
// Parse data here
}
しかし、私は、コールバックを必要とします!それを解決するには?私は解決策stackoverflowで見つかりませんでした。 さらに、私がDownloadCallback
の代わりにメジャークラスへの単純なデリゲートを使用している場合、接続コールバックも起動されません。
悲しいことに、これは理由ではありません:( – Vyacheslav
あなたの 'NSURLConnection * connection'はどうですか?あなたが表示するコードの最初のスニペットの囲み範囲を表示できますか?あなたの接続 – Joride
メインスレッドの実行に関する問題が見つかりました - メインスレッドが終了するまでNSURLConnectionを実行することはできません – Vyacheslav