我正在解析一个 XML 网络服务,在解析完成后我想调用另一个方法。但是我的代码在解析过程中调用了该方法。我想要的是等到解析过程结束。这是我的代码:
ArsivNoCheck *arsivNoCheck = [ArsivNoCheck alloc];
[arsivNoCheck checkArsivNo:_txtArsivNo.text]; //Here I call parsing method in another class
//Here I call the method
[self performSelectorOnMainThread:@selector(sampleMethod) withObject:nil waitUntilDone:YES];
-(void) sampleMethod
{
//some code
}
最佳答案
你应该考虑NSOperation
, 及其方法completionBlock
。
然后,您就可以执行解析,并在解析结束时执行一些代码。
注意:如果您计划更新 UI,请小心,因为 completionBlock
不一定在主线程上运行!
来自 NSOperation's Doc reference :
completionBlock
Returns the block to execute when the operation’s main task is complete.
-(void (^)(void))completionBlock
Return Value
The block to execute after the operation’s main task is completed. This block takes no parameters and has no return value.
Discussion
The completion block you provide is executed when the value returned by the isFinished method changes to YES. Thus, this block is executed by the operation object after the operation’s primary task is finished or cancelled.
示例:
[filterOp setCompletionBlock: ^{
NSLog(@"Finished filtering an image.");
}];
参见 this tutorial在 Ray Wenderlich 的网站上进行实现。
关于ios - 在调用另一个方法之前等待 XML 解析过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18785060/