2017-08-03 6 views
0
I am trying to update the `UILabel` i.e downloaded data and remeaning data to be downloaded estimated time and total size of the downloading files via `NSnotificationCenter`, but not being updated `UILabel` text Please help me on this. 

Also tried putting the `NSnotificationCenter` block in the main thread but no result found. 

I have tried like this: 

    - (AFHTTPRequestOperation)downloadMediaOperation:(ILSCDowloadMedia)media success:(void (^)(ILSCDowloadMedia *media))success {  
     if (media.mediaUrl.length == 0) nil; 

     __block NSString *mediaKey = [[NSUserDefaults standardUserDefaults] objectForKey:media.mediaUrl]; 

     NSURL *url = [NSURL URLWithString:media.mediaUrl]; 

     if (mediaKey.length == 0) { 
      mediaKey = [NSString stringWithFormat:@"%@.%@", [ILSCUtility createUUID], [[[url path] lastPathComponent] pathExtension]]; 
     } 

     NSFileManager *fileManager= [NSFileManager defaultManager]; 

     NSString *mediaFilePath = NIPathForDocumentsResource(mediaKey); 
     media.mediaFilePath = mediaFilePath; if (![fileManager fileExistsAtPath:mediaFilePath]) { 


      __weak ILSCSyncManager *weakSelf = self; 
      NSURLRequest *request = [self.HTTPClient requestWithMethod:@"GET" path:[url path] parameters:nil]; 

      AFHTTPRequestOperation *downLoadOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
      downLoadOperation.downloadSpeedMeasure.active = YES;  [downLoadOperation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ 
       // Clean up anything that needs to be handled if the request times out 
       // It may be useful to initially check whether the operation finished or was cancelled 
      }]; 
      downLoadOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:mediaFilePath append:NO]; 


      [downLoadOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

       [[NSUserDefaults standardUserDefaults] setObject:mediaKey forKey:media.mediaUrl]; 
       [[NSUserDefaults standardUserDefaults] synchronize]; 

       if (success) { 

       success(media); 
       }    
      } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
       NIDERROR(@"y error %@", [error localizedDescription]); 
       __strong ILSCSyncManager *strongSelf = weakSelf; 
       strongSelf.numberOfDownloadErrors++; 

      }]; 


      [downLoadOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) 
      { 

       NSLog(@"vvv Byted total expected to read %f",totalImagesBytesExpectedToRead); 


       totalImagesBytesRead += bytesRead; 

       humanReadableSpeed = downLoadOperation.downloadSpeedMeasure.humanReadableSpeed; 

       humanReadableRemaingTime = [downLoadOperation.downloadSpeedMeasure humanReadableRemainingTimeOfTotalSize:totalImagesBytesExpectedToRead numberOfCompletedBytes:totalImagesBytesRead]; 


       NSLog(@"Speed Human %@",humanReadableSpeed); 
       NSLog(@"Time is human read %@",humanReadableRemaingTime); 

       dispatch_async(dispatch_get_main_queue(), ^{ 
        [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdateProgressBar" object:[NSString stringWithFormat:@"%@-%f-%f-%@", humanReadableSpeed,totalImagesBytesRead,totalImagesBytesExpectedToRead,humanReadableRemaingTime]]; 


       }); 

      }];   

      return downLoadOperation; 

     } else { 

      if (success) { 
       success(media); 
      } 
     } 
    return nil; 
    } 

Please help me on this. 

This is the listener of the NSnotification please check and please let me know. 

一度ダウンロードを開始している間に、このクラスをLoaderとして追加します。 NSOperationキューの情報がバックグラウンドスレッドで実行されているので、私はいくつかのサイトを通過しました。私はこれで私を助けてくださいでは分かりません。進行中のファイルのダウンロード時にUIlabelのテキストが更新されない

_observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"UpdateProgressBar" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) { 

      NSString *str =[note object];   NSArray *arrayTotalOperationsIn = [str componentsSeparatedByString:@"-"]; 
      NSLog(@"%@",arrayTotalOperationsIn);   self.lblSpeedMeasure.text =[NSString stringWithFormat:@"Internet Speed - %@" ,[arrayTotalOperationsIn objectAtIndex:0]]; 

      float bytesRead = [[arrayTotalOperationsIn objectAtIndex:1] floatValue]; 
      float bytesExpectedToRead = [[arrayTotalOperationsIn objectAtIndex:2] floatValue]; 
      NSString *timeExpectedToRead = [arrayTotalOperationsIn objectAtIndex:3]; 

      self.progressCountTextLabel.text=[NSString stringWithFormat:@"%.2f MB/%.2f MB - %@ Left",bytesRead/1000000,bytesExpectedToRead/1000000,timeExpectedToRead];    
     }]; 


The above is the listener of the NSnotification please check and please let me know. 

一度ダウンロードを開始している間にこのクラスをローダとして追加します。 NSOperationキューの情報がバックグラウンドスレッドで実行されているので、私はいくつかのサイトを通過しました。私はこれで私を助けてくださいでは分かりません。

+1

を設定した後、あなたのUILabelにsetNeedsDisplayメソッドを呼び出してみ?コードのテキストを設定している行はどこですか? –

+1

'Notification Object'に' NSDictionary'を送るべきです。 – iPeter

+0

そして 'UpdateProgressBar'をリスニングするコードは何ですか?それは何をするためのものか?それはどこですか、どのクラスですか?彼はその通知名を聞いたのですか?それはinstanciatedですか? – Larme

答えて

0

は、あなたのコード内のラベルであるテキスト

[self.progressCountTextLabel setNeedsDisplay]; 
関連する問題