繁体   English   中英

在后台和主线程ios中执行

[英]execution in background and main thread ios

我有一个函数foo(),它在后台线程foo() {上调用一个函数栏

  [self performSelectorInBackground:@selector(bar:) withObject:nil]; }  bar() { //some initialsations //calling another function bar1();//also in background //after bar1() returns //marking following statement with * [self performSelectorOnMainThread:@selector(stopActivityIndicator:)withObject:nil wailUntilDone:YES]; } 

[self performSelectorInBackground:@selector(bar:) withObject:nil]; } bar() { //some initialsations //calling another function bar1();//also in background //after bar1() returns //marking following statement with * [self performSelectorOnMainThread:@selector(stopActivityIndicator:)withObject:nil wailUntilDone:YES]; }

  [self performSelectorInBackground:@selector(bar:) withObject:nil]; }  bar() { //some initialsations //calling another function bar1();//also in background //after bar1() returns //marking following statement with * [self performSelectorOnMainThread:@selector(stopActivityIndicator:)withObject:nil wailUntilDone:YES]; } 

bar()在调用另一个函数之前做了一些事情。 bar()所做的就是在后台。 同时,我正在展示ActivityIndi​​cator。 一旦我的函数在bar()中返回,我正在调用一个函数,它将在MainThread上运行stopActivityIndi​​cator。

现在,我想在调用stopActivityIndi​​cator()之前调用MainThread中的另一个函数我该怎么办? 我可以把另一个[self performSelectorOnMainThread:@selector(functionIWantToCall:)withObject:nil wailUntilDone:YES]; 之前*?

您可以调度一个块以在主线程上运行,并将您需要的任何代码放入该块中:

[[NSOperationQueue mainQueue] addOperationWithBlock:^ {

   // Code here
   NSLog(@"This is the main thread");

}];

使用您的代码,这将成为:

bar()
{
    bar1();

    [[NSOperationQueue mainQueue] addOperationWithBlock:^ {
        [stopActivityIndidator];
        [functionIWant];
    }];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM