Reputation: 2676
I am parsing an XML web service and after that parse finishes I want to call another method. But my code calls the method during the parse process. What I want is to wait till the parse process end. Here is my code:
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
}
Upvotes: 1
Views: 349
Reputation: 10698
You should consider NSOperation
, and its method completionBlock
.
Then, you would be able to perform your parsing, and at the end of it, execute some code.
Note : If you plan to update the UI, take care, because the completionBlock
is not necessarily running on the main thread!
From 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.
Example :
[filterOp setCompletionBlock: ^{
NSLog(@"Finished filtering an image.");
}];
See this tutorial on Ray Wenderlich's site for implementation.
Upvotes: 1