如何知道NSOperation操作是否完成?

时间:2016-06-17 00:23:15

标签: ios objective-c nsoperation

我需要从我的NSOperation中设置一个字符串,但是如何知道操作是否已完成?因为它没有达到我设定的断点,或者我没有正确地调用我的NSOperation?如果是,我应该如何调用我的操作?

这是我的代码

- (void)operationDidFinish:(MJOperation *)operation
{
NSString *strng  = [MJUtilities decodedStringFromXMLData:operation.receivedData];

mainString = [NSString stringWithFormat:mainString, strng];

}

现在我需要获取mainString的值

1 个答案:

答案 0 :(得分:3)

completionBlock上设置NSOperation属性。操作完成后,将调用completionBlock

有关详细信息,请参阅NSOperation文档:https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSOperation_class/#//apple_ref/occ/instp/NSOperation/completionBlock

尝试这样的事情:

__block NSString *mainString;

...

myOperation.completionBlock = ^{
    NSString *strng  = [MJUtilities decodedStringFromXMLData:myOperation.receivedData];
    mainString = [NSString stringWithFormat:mainString, strng];
    // here you should do something with mainString
};