Foundation framework提供了兩個(gè)內(nèi)置的NSOperation的子類,但是這兩個(gè)內(nèi)置的operation不一定能夠滿足我們的實(shí)際的需要。比如我們需要一個(gè)完成一個(gè)網(wǎng)絡(luò)請(qǐng)求的operation,里面可能會(huì)有許多自定義的邏輯在里面,為了完成這些特有的邏輯,往往需要自定義一個(gè)NSOperation的子類來(lái)。
創(chuàng)新互聯(lián)于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元泗水做網(wǎng)站,已為上家服務(wù),為泗水各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:13518219792NSOperation 類本身實(shí)現(xiàn)了許多與自定義有關(guān)的東西,我們只需要做相對(duì)較少的工作就可以了,自定義一個(gè)非并發(fā)的operation相對(duì)簡(jiǎn)單,只需要處理需要做的邏輯,并且處理cancel 操作就可以,其他的都不需要做了。但是自定義一個(gè)并發(fā)的operation相對(duì)難度較大,需要做的東西也比較多。
If the block operation and invocation operation objects do not quite meet the needs of your application, you can subclass NSOperation directly and add whatever behavior you need. The NSOperation class provides a general subclass point for all operation objects. The class also provides a significant amount of infrastructure to handle most of the work needed for dependencies and KVO notifications. However, there may still be times when you need to supplement the existing infrastructure to ensure that your operations behave correctly. The amount of extra work you have to do depends on whether you are implementing a nonconcurrent or a concurrent operation.
Defining a nonconcurrent operation is much simpler than defining a concurrent operation. For a nonconcurrent operation, all you have to do is perform your main task and respond appropriately to cancellation events; the existing class infrastructure does all of the other work for you. For a concurrent operation, you must replace some of the existing infrastructure with your custom code. The following sections show you how to implement both types of object.
Performing the Main Task
不管是什么類型的operation,必須要做的一件事情就是執(zhí)行需要的task,這是operation存在的最基本的意義。
At a minimum, every operation object should implement at least the following methods:
1、A custom initialization method
2、main
You need a custom initialization method to put your operation object into a known state and a custom main method to perform your task. You can implement additional methods as needed, of course, such as the following:
1、Custom methods that you plan to call from the implementation of your main method.
2、Accessor methods for setting data values and accessing the results of the operation.
3、Methods of the NSCoding protocol to allow you to archive and unarchive the operation object.
The following code shows a starting template for a custom NSOperation subclass. (This listing does not show how to handle cancellation but does show the methods you would typically have. For information about handling cancellation, see "Responding to Cancellation Events") The initialization method for this class takes a single object as a data parameter and stores a reference to it inside the operation object. The main method would ostensibly work on that data object before returning the results back to your application.
Defining a simple operation object.
@interface MyNonConcurrentOperation : NSOperation
@property id (strong) myData;
- (id)initWithData:(id)data;
@end
@implementation MyNonConcurrentOperation
- (id)initWithData:(id)data {
if(self = [super init]) {
myData = data;
}
return self;
}
- (void)main {
@try {
// Do some work on myData and report the results.
@catch(...) {
// Do not rethrow exceptions.
}
}
@end
For a detailed example of how to implement an NSOperation subclass, see NSOperationSample.
除了添加完成必須工作的main方法之外,還必須要處理cancel相關(guān)的邏輯,支持cancel是NSOperation很重要的一部分能力。
Responding to Cancellation Events
After an operation begins executing, it continues performing its task until it is finished or until your code explicitly cancels the operation. Cancellation can occur at any time, even before an operation begins executing. Although the NSOperation class provides a way for clients to cancel an operation, recognizing the cancellation event is voluntary by necessary. If an operation were terminated outright, there might not be a way to reclaim resources that had been allocated. As a result, operation objects are expected to check for cancellation events and to exit gracefully when they occur in the middle of the operation.
To support cancellation in an operation object, all you have to do is call the object's isCancelled method periodically from your custom code and return immediately if it ever returns YES. Supporting cancellation is important regardless of the duration of your operation or whether you subclass NSOperation directly or use one of its concrete subclasses. The isCancelled method itself is very lightweight and can be called frequently without any significant performance penalty. When designing your operation objects, you should consider calling the isCalled method at the following places in your code:
1、Immediately before you perform any actual work.
2、At least once during each iteration of a loop, or more frequently if each iteration is relatively long.
3、At any points in your code where it woul be relatively easy to abort the operation.
The following provides a very simple example of how to respond to cancellation events in the main method of an operation object. In this case, the isCancelled method is called each time through a while loop, allowing for a quick exit before work begins and again at regular intervals.
Responding to a cancellation request
- (void)main {
@try {
BOOL isDone = NO;
while(![self isCancelled] && !isDone) {
// Do some work and set isDone to YES when finished
}
}@catch(...) {
// Do not rethrow exceptions.
}
}
Although the preceding example contains no cleanup code, your own code should be sure to free up any resources that were allocated by your custom code.
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。