使用dispatch_async中的变量 [英] working with variables from dispatch_async

查看:120
本文介绍了使用dispatch_async中的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种从服务器下载一些图片的方法.我有一个用异步块(NSMutableArray * imgtmp)定义的用于下载数据的缓冲区,但还没有弄清楚如何将数组从那里取出.访问imgtmp以返回其内容或从中设置实例变量的最佳方法是什么?

I have a method which downloads some pics from a server. I had the buffer for the downloaded data defined withing async block (NSMutableArray *imgtmp) but haven't figured out how to get the array back out of there. What's the best way to access imgtmp in order to return it's contents, or set an instance variable from it?

我一直在寻找 Apple阻止文档,但我一定不在正确的部分.我是否需要使用__block关键字声明imgtmp?我试过了,但是imgtmp在块外仍然是空的.谢谢!

I've been looking in the Apple Block docs but I must not be in the right section. Do I need to declare imgtmp using the __block keyword somehow? I tried that but imgtmp was still empty outside the block. Thanks!

编辑:使用工作模型更新的代码

EDIT: code updated with working model

- (void) loadImages
{
   // temp array for downloaded images. If all downloads complete, load into the actual image data array for tablerows
   __block NSMutableArray *imgtmp = [[NSMutableArray alloc] init];

   dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0),
   ^{
      int error = 0;
      int totalitems = 0;
      NSMutableArray *picbuf = [[NSMutableArray alloc] init];

      for (int i=0; i < _imageURLS.count;i++)
      {
         NSLog(@"loading image for main image holder at index %i",i);
         NSURL *mynsurl = [[NSURL alloc] initWithString:[_imageURLS objectAtIndex:i]];
         NSData *imgData = [NSData dataWithContentsOfURL:mynsurl];
         UIImage *img = [UIImage imageWithData:imgData];


         if (img)
         {
            [picbuf addObject:img];
            totalitems++;
         }
         else
         {
            NSLog(@"error loading img from %@", [_imageURLS objectAtIndex:i]);
            error++;
         }
      }// for int i...


      dispatch_async(dispatch_get_main_queue(),
      ^{
         NSLog(@"_loadedImages download COMPLETE");
         imgtmp = picbuf;
         [_tvStatus setText: [NSString stringWithFormat:@"%d objects have been retrieved", totalitems]];
         NSLog (@"imgtmp contains %u images", [imgtmp count]);
      });// get_main_queue


   });// get_global_queue


}

推荐答案

您遇到的是,在执行任何块代码之前,最终" NSLog调用 .您所有的图像加载内容都包装在dispatch_async中.它被异步执行,而NSLog被立即调用.

You're hitting that "final" NSLog call before any of your block code has executed. All your image loading stuff is wrapped in a dispatch_async. It is executed asynchronously whereas the NSLog is called right away.

我认为您最好的做法是将imgtmp传递给某些持久对象.也许您的视图控制器可以具有以下属性:

I think the best thing for you to do is to pass imgtmp along to some persistent object. Perhaps your view controller can have a property like:

@property (nonatomic, copy) NSArray *images;

,您可以在将文本分配给_tvStatus的同一块中进行分配.

and you can assign that in the same block where you assign the text to _tvStatus.

这篇关于使用dispatch_async中的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆