2015-02-23 111 views
0

我正在使用NSNotificationcentre从for循环更新UI。在执行超出循环之前,UI不会更新。有办法处理这种情况? 这里是我下面的代码:在for循环中处理UI更新 - iOS

- (void)uploadContent{ 
    NSURLResponse *res = nil; 
    NSError *err = nil; 



    for (int i = 0; i < self.requestArray.count; i++) { 

     [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
      [[NSNotificationCenter defaultCenter] postNotificationName:kUpdatePreviewImageView object:nil userInfo:@{@"image":  [self.imageArray objectAtIndex:i],@"count":[NSNumber numberWithInt:i],@"progress":[NSNumber numberWithFloat:0.5f]}]; 
     }]; 
     ImageUploadRequest *request = [self.requestArray objectAtIndex:i]; 


     NSData *data = [NSURLConnection sendSynchronousRequest:request.urlRequest returningResponse:&res error:&err]; 
     if (err) { 

      NSLog(@"error:%@", err.localizedDescription); 
     } 
     NSError *jsonError; 
     NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&jsonError]; 

     NSLog(@"current thread %@",[NSThread currentThread]); 

     [[NSOperationQueue mainQueue] addOperationWithBlock:^{ 
      [[NSNotificationCenter defaultCenter] postNotificationName:kUpdatePreviewImageView object:nil userInfo:@{@"image":[self.imageArray objectAtIndex:i],@"count":[NSNumber numberWithInt:i],@"progress":[NSNumber numberWithFloat:1.0f]}]; 
     }]; 
    } 


    [[NSNotificationCenter defaultCenter] postNotificationName:kImageUploaded object:nil]; 


} 

在我viewcontroller.m文件我有()viewDidLoad中下宣布观察者

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updatePreviewView:) name:kUpdatePreviewImageView object:nil]; 

的updatepreview:

-(void)updatePreviewView:(NSNotification *)notify{ 


    NSDictionary *previewImageDetails = [notify userInfo]; 

    self.previewImageView.image = previewImageDetails[@"image"]; 



    hud.labelText = [NSString stringWithFormat:@"Uploading media %@ of %lu",previewImageDetails[@"count"],(long unsigned)self.mediaDetail.count]; 


    hud.progress = [previewImageDetails[@"progress"] floatValue]; 

} 

回答

2
:类定义如下

由于for循环正在运行主线程,所以此线程将被阻塞,直到for外观完成。由于主要的威胁也是UI线程,所以你的UI更新不会完成,直到循环结束。

您应该在后台线程上运行该循环,UI将在主线程上运行异步时更改。

并在您的updatePreviewView:请确保代码将在主线程上运行。

1

这样做:

-(void)updatePreviewView:(NSNotification *)notify{ 

    dispatch_async(dispatch_get_main_queue(), ^{ 
     NSDictionary *previewImageDetails = [notify userInfo]; 
     self.previewImageView.image = previewImageDetails[@"image"]; 
     hud.labelText = [NSString stringWithFormat:@"Uploading media %@ of %lu",previewImageDetails[@"count"],(long unsigned)self.mediaDetail.count]; 
     hud.progress = [previewImageDetails[@"progress"] floatValue]; 
    }); 
} 
1

你应该把它在主线程。但NSOperationQueue可能不会发送全部循环。你可以在异步队列中进行操作并发送它而不需要NSOperationQueue

 dispatch_async(dispatch_get_main_queue(), ^{ 
    NSDictionary *previewImageDetails = [notify userInfo]; 
    self.previewImageView.image = previewImageDetails[@"image"]; 
    hud.labelText = [NSString stringWithFormat:@"Uploading media %@ of %lu",previewImageDetails[@"count"],(long unsigned)self.mediaDetail.count]; 

    hud.progress = [previewImageDetails[@"progress"] floatValue]; 
});