Ios UIlabel文本在下载文件时未获得更新

Ios UIlabel文本在下载文件时未获得更新,ios,objective-c,uilabel,Ios,Objective C,Uilabel,当下载开始时,我添加这个类作为加载程序。 我已经浏览了一些站点,因为我得到了一些信息,操作队列是在后台线程中运行的。这件事我不确定,请帮帮我 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

当下载开始时,我添加这个类作为加载程序。 我已经浏览了一些站点,因为我得到了一些信息,操作队列是在后台线程中运行的。这件事我不确定,请帮帮我

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.
当下载开始时,我添加这个类作为加载程序。
我已经浏览了一些站点,因为我得到了一些信息,操作队列是在后台线程中运行的。我对此不确定,请帮助我。

设置文本后,尝试调用UILabel上的setNeedsDisplay方法

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.
_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.

代码中的标签在哪里?设置代码文本的代码行在哪里?您应该在
通知对象中发送
NSDictionary
。侦听
UpdateProgressBar
的代码是什么?它有什么作用?它在哪里,在哪一班等。?他听过那个通知吗?它是实例化的吗?这是一种通知方法,我将这些值作为对象发送给另一个类,在该类中更新UIlabel文本。dispatch_async(dispatch_get_main_queue(),^{[[NSNotificationCenter defaultCenter]postNotificationName:@“UpdateProgressBar”对象:[NSString stringWithFormat:@“%@-%f-%f-%@”、humanReadableSpeed、TotalImagesBytes读取、TotalImagesBytes预期读取、humanReadableRemaingTime];);这是我将这些值作为对象分配给另一个类的通知方法,在该类中我更新UIlabel文本。dispatch_async(dispatch_get_main_queue(),^{[[NSNotificationCenter defaultCenter]postNotificationName:@“UpdateProgressBar”对象:[NSString stringWithFormat:@“%@-%f-%f-%@”、Human可读速度、TotalImagesBytes读取、TotalImagesBytes读取‌​ctedToRead,humanRead‌​ableRemaingTime]];})