2016-04-10 15 views
0

私のサーバーからの進捗状況を伴うファイルのダウンロードを実装したいと考えています。 私は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の代わりにメジャークラスへの単純なデリゲートを使用している場合、接続コールバックも起動されません。

答えて

0

コールバッククラスにdeallocメソッドを追加し、その中にブレークポイントまたはログステートメントを出力します。コールバックが呼び出される前に解放されているかどうかを確認してください。

この場合、コールバッククラスインスタンスがすぐに破棄されます。リクエストよりも長く生きるクラスのプロパティにします。

また、あなたは必ずこのコードことを確認する必要があります

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:dc]; 
[connection start]; 

が接続をoutlivesして実行ループを持っているスレッドで呼び出されます。これを達成する最も簡単な方法は、そのコードをメインキューで呼び出すことです。あなたのコード例は、呼び出されたキューを表示しません。それが動作していない場合、私はあなたがバックグラウンドキューでそれを呼び出すためだと仮定します。 必要な/必要なデリゲートコールバックからバックグラウンドキューにディスパッチできます。

傍注として、新しいものを構築する場合は、NSURLConnectionの代わりにNSURLSessionを試してみてください。 NSURLSessionは、より安全で使いやすく、推奨されなくなりました。 NSURLConnectionは非推奨です。

+0

悲しいことに、これは理由ではありません:( – Vyacheslav

+0

あなたの 'NSURLConnection * connection'はどうですか?あなたが表示するコードの最初のスニペットの囲み範囲を表示できますか?あなたの接続 – Joride

+0

メインスレッドの実行に関する問題が見つかりました - メインスレッドが終了するまでNSURLConnectionを実行することはできません – Vyacheslav

関連する問題