【问题标题】:using NSOperationQueue and blocks使用 NSOperationQueue 和块
【发布时间】:2012-02-24 22:49:47
【问题描述】:

我在 Cocoa 中使用 addoperationwithblock 时遇到了一点麻烦。假设我有一个主函数

-(IBAction) callthisone {

  // Call another function "slave" here and store returned value in result

    result = return value from slave
    NSLog(@" result is %@",result);
 }];

}

-(NSArray *) slave {

 [operationQueue addOperationWithBlock: ^{   

  NSString * result = @"5" ;
  }];

 return result;
}

我永远无法得到主服务器中返回的结果值。我该怎么做呢 ?我的方法正确吗?谢谢

【问题讨论】:

  • 那些不是函数,它们是方法。大不同。

标签: cocoa nsoperationqueue


【解决方案1】:

你可以试试这样的:

-(IBAction) callthisone {
    [self slave: ^(NSString* result) {
            NSLog(@" result is %@",result);
        }
    ];
}


-(void)slave: (void(^)(NSString*)) callback {
    [operationQueue addOperationWithBlock: ^{
            NSString* str = [NSString stringWithFormat: @"5]";
            callback(str);
        }
    ];
}

【讨论】:

    【解决方案2】:

    Apple's documentation for addOperationWithBlock 说:

    参数

    从操作对象执行的块。该块应该采取 没有参数,也没有返回值。

    这些用于自包含块操作。

    您能否尝试一些不同的方法,在让内容进出队列/线程方面具有更大的灵活性?也许是 Grand Central Dispatch(我只是 looking at this thread)。

    【讨论】:

      猜你喜欢
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多