下载正在进行的文件时,UIlabel文本未获得更新

时间:2017-08-03 07:22:21

标签: 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 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队列在后台线程中运行。我不确定,请帮助我。

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

我在加载启动时将此类添加为Loader。 我已经浏览了一些网站,因为我得到了一些信息NSOperation队列在后台线程中运行。我不确定,请帮助我。

1 个答案:

答案 0 :(得分:1)

设置文本

后,尝试在UILabel上调用setNeedsDisplay方法
[self.progressCountTextLabel setNeedsDisplay];