ios - 在调用另一个方法之前等待 XML 解析过程

标签 ios objective-c

我正在解析一个 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/

相关文章:

objective-c - 为什么 Swift 中 UIScrollView 的自定义子类必须是 UIGestureRecognizerDelegate?

objective-c - TabBarController 和 SplitViewController

iOS:有没有一种安全的方法可以在代码中包含 API key ?

ios - 此处可能误用逗号运算符

ios - 第二个 TableView 没有显示在屏幕上

objective-c - CCMenuItemFont 颜色变化

ios - 暂时禁用点击手势

ios - UIView animateWithDuration 在 ios8 中没有动画

ios textFieldDidEndEditing 触发,但 textFieldShouldReturn 不触发

ios - 将 View Controller 连接到委托(delegate)类 - CLLocationManager/iOS8