TWGOperations
是一个针对 iOS 和 Mac OS X 的库。它建立在 NSOperation 之上,提供了一个应用程序的并发性和代码组织。
该库的创建直接受到 2015 年 Apple 开发者大会 (WWDC) 上的演讲的启发。请查看 视频 ,示例项目。对 Apple 团队对以操作为中心的应用程序愿景给予充分的认可。
此库提供了一套用于快速轻松实现结构化异步任务并使用 Apple 的 NSOperation
套件实现良好并发管理的工具。但是,它也指定了实现和使用的方法。我们认为,使用 NSOperation
和 TWGOperation
(以及 TWGGroupOperation
)库有正确和错误的方法。
我们发现,TWGOperation
库提供了一种简单的方法将复杂任务从视图中移除,为单元测试提供良好的接口,以及一种易于学习并发和线程化的途径。
想要快速查看使用 TWGOperation
库编写的 示例项目 吗?使用 CocoaPods 尝试TWGOperations 示例项目
在您的终端中: pod try TWGOperations
Twgoperation
是此库的基础。它提供了两个简单的功能
NSOperation
不同,Twgoperation
的执行方法结束时不会完成。开发人员必须显式调用 - (void)finish
来通知 NSOperationQueue
它已完成执行。TWGOperation
使用 委托模式 来通知调用者完成或失败情况。@interface TWGOperation : NSOperation
@property (nonatomic, weak) id<TWGOperationDelegate> delegate;
@end
@interface TWGOperation (SubclassingHooks)
/*
Override this to implement execution
*/
- (void)execute;
/*
TWGOperation subclasses must call this to complete execution of the operation
*/
- (void)finish;
@end
可以将 TWGOperation
添加到操作队列中并通过
MyOperationSubclass *operation = [[MyOperationSubclass alloc] init];
[[NSOperationQueue mainQueue] addOperation:operation];
TWGOperation
使用委托来通知调用者完成任务或失败。
@protocol TWGOperationDelegate <NSObject>
- (void)operation:(nonnull TWGOperation *)operation didCompleteWithResult:(nullable id)result NS_SWIFT_NAME(operationDidComplete(operation:withResult:));
- (void)operation:(nonnull TWGOperation *)operation didFailWithError:(nullable NSError *)error NS_SWIFT_NAME(operationDidFail(operation:withError:));
@end
通过使用,开发者可以快速访问到 - (void)finish
以及 TWGOperationDelegate
协议方法。
@interface TWGOperation (QuickDelegate)
/*
Convenience completion
Subclasses should use these as short hand for the process:
1. Inform delegate of complete or fail
2. -finish
*/
- (void)finishWithResult:(id _Nullable)result NS_SWIFT_NAME(finish(withResult:));
- (void)finishWithError:(NSError * _Nullable)error NS_SWIFT_NAME(finish(withError:));
@end
这是首选的完成方法,即使不需要错误或结果也应当使用。
[self finishWithResult:nil];
或者
[self finishWithError:nil];
TWGGroupOperation
是 TWGOperation
的子类,能封装操作且不需要实现 - (void)execute
方法。这是通过在内部使用 NSOperation
或其子类的 NSOperationQueue
来实现的。
@interface TWGGroupOperation : TWGOperation
@property (nonatomic, strong, readonly) NSOperationQueue *operationQueue;
/*
initWithOperations: adds operations passed to the operationQueue
subclasses need create sub-operations then [super initWithOperations:]
to have them execute
*/
- (instancetype)initWithOperations:(NSArray<NSOperation *> *)operations;
/*
Subclasses of TWGGroupOperation SHOULD NOT override -execute
See TWGOperation for further subclassing instructions
*/
- (void)execute NS_UNAVAILABLE;
@end
TWGGroupOperation
是为了能够被继承,并响应子操作的委托回调而设计的,请查看 TWGOperation_Example
项目中的示例。
与处理 TWGOperation
的方式相同,TWGGroupOperation
也可以被添加到操作队列中并通过其执行。
MyGroupOperationSubclass *operation = [[MyGroupOperationSubclass alloc] init];
[[NSOperationQueue mainQueue] addOperation:operation];
Flickr REST API 是一个很好的示例,展示了需要通过多个链式API调用才能获取最终结果的REST API。
调用终端的请求
https://api.flickr.com/services/rest/?method=flickr.photos.getRecent
结果是一个与以下类似的对象数组:
{
"id": "30379955494",
"owner": "86866994@N00",
"secret": "5c84551928",
"server": "5731",
"farm": 6,
"title": "Frosty leaves",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0
}
要获取照片的URL,你可以调用
https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&photo_id=30379955494
结果如下:
{
sizes = {
size = (
{
height = 75;
label = Square;
media = photo;
source = "https://farm6.staticflickr.com/5791/30413623884_ae2f143b5a_s.jpg";
url = "https://www.flickr.com/photos/145345660@N03/30413623884/sizes/sq/";
width = 75;
},
{
height = 528;
label = Large;
media = photo;
source = "https://farm6.staticflickr.com/5791/30413623884_ae2f143b5a_b.jpg";
url = "https://www.flickr.com/photos/145345660@N03/30413623884/sizes/l/";
width = 1024;
}
);
};
stat = ok;
}
这给我们提供了必要的URL来获取任意单个图像。
Flickr 的 flickr.photos.getRecent
API 方法返回包含100个项目的第一页。
因此,要获取近期照片列表中的前100张图片,你需要执行以下操作:
flickr.photos.getRecent
flickr.photos.getSizes
https://www.flickr.com/photos/id/sizes/l/
的URL通过提供NSOperation的调度和依赖性,以及处理网络请求所需的错误检查和重试,TWGOperations有助于减轻这种负担。
请参考 FetchFlickrFeedOperation
以了解更多关于如何实现解决方案的详细信息。
测试套件是作为可以通过 CocoaPods 获取的 TWGOperation_Example
项目的一部分提供的。
自豪地,Nicholas Kuhne,[[email protected]]
TWGOperations 代码库下提供 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。