Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 同时执行多个方法的最快方式_Iphone_Objective C_Ios_Asynchronous - Fatal编程技术网

Iphone 同时执行多个方法的最快方式

Iphone 同时执行多个方法的最快方式,iphone,objective-c,ios,asynchronous,Iphone,Objective C,Ios,Asynchronous,我有4个方法,每个方法都需要一两秒钟才能返回,这些方法返回UIImage,我需要这些图像以最快的方式显示 -(NSMutableArray*)prepareImages{ UIImage *imageToModifyUsingUIImageCategory; NSMutableArray *imageArray = [[NSMutableArray alloc]initWithObjects: [imageToModifyUsingUIImageCategory doSomethi

我有4个方法,每个方法都需要一两秒钟才能返回,这些方法返回UIImage,我需要这些图像以最快的方式显示

-(NSMutableArray*)prepareImages{
  UIImage *imageToModifyUsingUIImageCategory;

  NSMutableArray *imageArray = [[NSMutableArray alloc]initWithObjects:
  [imageToModifyUsingUIImageCategory doSomethingAndReturn1],
  [imageToModifyUsingUIImageCategory doSomethingAndReturn2],
  [imageToModifyUsingUIImageCategory doSomethingAndReturn3],
  [imageToModifyUsingUIImageCategory doSomethingAndReturn4],nil];    
  return imageArray;
}
在上述方法的末尾,我将从该数组中获得4个图像。每个“doSomethingAndReturn”方法都需要一两秒钟,这意味着我的
prepareImages
方法将在大约5秒钟内完成执行。太长了,嗯

我的问题是,还有什么其他方法可以更快地完成这些任务?GCD是我的选择吗?怎么做


任何帮助都将不胜感激。谢谢

我有一个解决方法,但还没有测试过。我正在从我遇到并解决的其他几个问题中得出解决方案:

  • NSNull
    插入到
    NSMutableArray
    作为占位符。
  • dispatch\u async
    完成繁重的工作并替换
    NSMutableArray
    中的相应条目(使用
    @synchronized
    关键字锁定
    NSMutableArray

  • 同样在
    dispatch async
    函数中,将函数分派回主队列以更新视图。
    • 您可以使用:

      NSOperationQueue类调节一组NSOperation对象的执行。将操作添加到队列后,操作将保留在该队列中,直到显式取消或完成其任务。队列中的操作(但尚未执行)本身根据优先级和操作间对象依赖性进行组织,并相应地执行。一个应用程序可以创建多个操作队列,并向其中任何一个提交操作

      或:

      Grand Central Dispatch(GCD)调度队列是执行任务的强大工具。调度队列允许您异步或同步地执行与调用方相关的任意代码块。您可以使用调度队列执行几乎所有在单独线程上执行的任务。调度队列的优点是,与相应的线程代码相比,它们使用起来更简单,执行这些任务的效率也更高


      假设您不介意在后台顺序加载图像(而不是并行加载),只需将图像分派到后台队列以运行
      prepareImages
      即可:

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
          NSArray *images = [self prepareImages];
      
          // now send the result back to the main thread so we can do
          // UIKit stuff
          dispatch_async(dispatch_get_main_queue(), ^{
              // set the images on your UIImageViews here...
          });
      });
      
      这将在低优先级后台队列中加载图像,然后在完成后将其分派回主线程

      如果您想保持更低的优先级,可以使用
      调度队列\u优先级\u后台
      ,尽管这仅在iOS 4.3及更高版本中可用。在这种情况下,您应该检查
      dispatch\u get\u global\u queue
      中的返回值,如果返回0,您可以使用其他优先级之一


      如果要并行加载每个映像,可能应该将
      dosomething和return
      方法转换为
      NSOperation
      子类,并使用
      NSOperationQueue
      来运行它们。这将需要更多的努力来实现。如果同时处理多个大图像,还应注意内存使用。

      您可以在处理器的另一个核心上同时执行,以便尝试:

      NSThread detachNewThreadSelector:@selector(YOUR_METHODS_HERE:) toTarget:self withObject:nil];
      NSThread detachNewThreadSelector:@selector(YOUR_METHODS_HERE:) toTarget:self withObject:nil];
      NSThread detachNewThreadSelector:@selector(YOUR_METHODS_HERE:) toTarget:self withObject:nil];
      NSThread detachNewThreadSelector:@selector(YOUR_METHODS_HERE:) toTarget:self withObject:nil];
      

      如果您的处理器有四个内核,那么每个方法都将在不同的内核上执行。

      我只是做了一个练习:对每个dosomething和return使用GCD concurrent Q,并使用串行Q来监视回调的数量。当回调的数量等于doSomethingAndReturn的数量时,返回prepareImages数组

      我创建了代码来测试这个概念

      -(NSString *)doSomethingAndReturn1
      {
          for (int i=0; i<30; i++) 
          {
              NSLog(@"soSomethingAndReturn1 i: %i", i);
          }
          return @"soSomethingAndReturn1";
      }
      
      -(NSString *)doSomethingAndReturn2
      {
          for (int i=0; i<10; i++) 
          {
              NSLog(@"soSomethingAndReturn2 i: %i", i);
          }
          return @"soSomethingAndReturn2";
      }
      
      -(NSString *)doSomethingAndReturn3
      {
          for (int i=0; i<20; i++) 
          {
              NSLog(@"soSomethingAndReturn3 i: %i", i);
          }
          return @"soSomethingAndReturn3";
      }
      
      -(void)addToArray:(NSString *)str
      {
          [asyncArray addObject:str];
          NSLog(@"asyncArray: %@", asyncArray);
      }
      
      - (IBAction)buttonMultitasksPressed:(id)sender 
      {
          dispatch_queue_t serialdQueue;
          serialdQueue = dispatch_queue_create("com.mydomain.testbed.multimethods", NULL);
              dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                  [self addToArray:[self doSomethingAndReturn1]];
              });
              dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                  [self addToArray:[self doSomethingAndReturn2]];
              });
              dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                  [self addToArray:[self doSomethingAndReturn3]];
              });
              dispatch_sync(serialdQueue, ^{
                  while (!([asyncArray count] == 3)) 
                  {
                      NSLog(@"not there yet count: %i", [asyncArray count]);
                  }
              });
      
          NSLog(@"end of dispatch_sync serialQueue");
      //    return asyncArray;
      }
      
      -(NSString*)doSomethingAndReturn1
      {
      
      对于(int i=0;ithanks for the response!我一接触我的mac电脑就会尝试解决这个问题。你可以将每个对DoSomething的调用包装在自己的dispatch_async中,以便它们都并行运行。要等待它们全部完成,你可以创建一个dispatch group。请参阅此处的文档:非常感谢..帮助了我很多。不推荐低甚至是后台优先级。后台优先级队列限制了磁盘I/O,因此加载图像不会很快。他想要最快的方式。谢谢!我有一个for循环决定任务,我将我的打印任务包装在dispatch_async中…如上所述,同时得到了异步打印。我还使用了位于d上方的@autorelease poolispatch和下面的循环。向上投票:)@MikeWeller你能建议与NSO操作相同的场景吗??