2013-05-04 50 views
1

我是新来的iOS开发线程,我有问题在这里考虑下面的代码的iOS在一个线程中运行多个操作

[activityIndicator startAnimating]; 
    [self startAnewOperationInSepThread]; 
    [self startAnotherOperationInSepThread]; 

这里我的理解是指在执行第一线的时候,活动的指标将被启动在主线程中。所以现在主线程负责运行活动指示器。随后的操作将如何被调用。

+0

你想从其他线程访问activityIndi​​cator吗? – 2013-05-04 05:57:41

+0

上面的代码工作正常,活动指标是动画和其他后续操作被调用,但我不清楚如何调用这两种方法,因为主线程现在正在处理活动指标,没有办法调用其他两种方法。 – Karthick 2013-05-04 06:19:02

回答

0

号当第一线执行活动的指标未启动。只要主循环调用的主线程中的方法结束,它就会启动。当你的方法在主线程中运行时,UI将不会改变。

1

使用performSelectorInBackground

[self performSelectorInBackground:@selector(yourmethod:) withObject:nil]; 
0

是的,你调用[activityIndi​​cator startAnimating];从主线程,这只是一个调用来启动动画。实际的动画是在一个单独的线程上完成的。

即使活动指示器仍然是动画,这并不意味着您无法对主线程执行任何操作。因为他的职责(称为startAnimating方法)已经结束。

在activityIndi​​cator上调用startAnimating后,它跳到下一行并执行[self myMethod1];然后[self myMethod2];为下面的代码。

不要让他们复杂的,这就是这个简单的

[activityIndicator startAnimating]; 
    [self myMethod1]; 
    [self myMethod2]; 
0

你必须选择它

1>您可以运行在不同的两种两种方法 - 不同的线程喜欢

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

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

OR

2>如果你想上线后,已完成其他将开始然后首先调用startAnewOperationInSepThread方法,它的定义调用内部startAnotherOperationInSepThread喜欢

-(void)startAnotherOperationInSepThread 
{ 
    [self startAnotherOperationInSepThread]; 
} 

请让我知道如果仍然ü面临任何问题。谢谢