项目作者: p4checo

项目描述 :
µFramework consisting of `NSOperationQueue` subclasses (Swift & Obj-C) which allow scheduling operations in serial subgroups inside a concurrent queue
高级语言: Swift
项目地址: git://github.com/p4checo/APNSubGroupOperationQueue.git
创建时间: 2016-04-09T19:19:36Z
项目社区:https://github.com/p4checo/APNSubGroupOperationQueue

开源协议:MIT License

下载


APNSubGroupOperationQueue

license
release
platforms
Build Status
codecov.io
Docs
Carthage
CocoaPods
Swift 4.2

Swift & Obj-C µFramework consisting of NSOperationQueue subclasses which allow scheduling operations in serial subgroups inside concurrent queues.

In some scenarios, operations scheduled in an NSOperationQueue depend on a subset of other operations and should be processed in order, but don’t depend on the remaining operations in the queue.

So far, this could be solved by:

  • using separate queues for each subset of dependent operations (can become unmanageable, wasteful)
  • defining the queue as serial (sub-optimal performance)

With this µFramework, a single operation queue can be used to schedule all operations and obtain the best of both worlds.

Dependent operations are grouped into “subgroups” which are guaranteed to be processed in a serial fashion inside each subgroup, while operations from other subgroups are processed concurrently (while serial inside their own subgroup) and regular operations (i.e. without defined subgroup) are processed concurrently with all others. This is done by leveraging NSOperations dependencies and using an auxiliary data structure to store all subgroups’ operations.

Use

Swift

Single SubGroup Key type

  1. @import APNSubGroupOperationQueue
  2. let subGroupQueue = SubGroupOperationQueue<String>()
  3. // schedule operations in subgroups "A", "B" and "C"
  4. // these will run serially inside each subgroup, but concurrently with other subgroups' operations
  5. subGroupQueue.addOperation(opA1, withKey: "A")
  6. subGroupQueue.addOperation(opA2, withKey: "A")
  7. subGroupQueue.addOperation(opA3, withKey: "A")
  8. subGroupQueue.addOperations([opB1, opB2, opB3], withKey: "B")
  9. subGroupQueue.addOperation({ /* opC1 */ }, withKey: "C")
  10. subGroupQueue.addOperation({ /* opC2 */ }, withKey: "C")
  11. subGroupQueue.addOperation({ /* opC3 */ }, withKey: "C")
  12. // query current subgroup's operations (a snapshot)
  13. let aOps = subGroupQueue["A"]
  14. let bOps = subGroupQueue["B"]
  15. let cOps = subGroupQueue.subGroupOperations(forKey: "C")

Multiple SubGroup Key types (via AnyHashable)

  1. @import APNSubGroupOperationQueue
  2. let dynamicSubGroupQueue = SubGroupQueue<AnyHashable> // or simply a `DynamicSubGroupOperationQueue`
  3. dynamicSubGroupQueue.addOperation(opX1, withKey: "X")
  4. dynamicSubGroupQueue.addOperation(opX2, withKey: "X")
  5. dynamicSubGroupQueue.addOperation(opX3, withKey: "X")
  6. dynamicSubGroupQueue.addOperations([opN1, opN2, opN3], withKey: 1337)
  7. let date = Date()
  8. dynamicSubGroupQueue.addOperation({ /* opD1 */ }, withKey: date)
  9. dynamicSubGroupQueue.addOperation({ /* opD2 */ }, withKey: date)
  10. dynamicSubGroupQueue.addOperation({ /* opD3 */ }, withKey: date)
  11. // query current subgroup's operations (a snapshot)
  12. let xOps = subGroupQueue["X"]
  13. let nOps = subGroupQueue[1337]
  14. let dOps = subGroupQueue.subGroupOperations(forKey: date)

Objective-C

  1. @import APNSubGroupOperationQueue;
  2. APNSubGroupOperationQueue *subGroupQueue = [APNSubGroupOperationQueue new];
  3. // schedule operations in subgroups "A", "B" and "C"
  4. // these will run serially inside each subgroup, but concurrently with other subgroups' operations
  5. [subGroupQueue addOperation:opA1 withKey:@"A"];
  6. [subGroupQueue addOperation:opA2 withKey:@"A"];
  7. [subGroupQueue addOperation:opA2 withKey:@"A"];
  8. [subGroupQueue addOperations::@[opB1, opB2, opB3] withKey:@"B" waitUntilFinished:false];
  9. [subGroupQueue addOperationWithBlock:^{ /* opC1 */ } andKey:@"C"];
  10. [subGroupQueue addOperationWithBlock:^{ /* opC2 */ } andKey:@"C"];
  11. [subGroupQueue addOperationWithBlock:^{ /* opC3 */ } andKey:@"C"];
  12. // query current subgroup's operations (a snapshot)
  13. NSArray<NSOperation*> *aOps = [subGroupQueue subGroupOperationsForKey:@"A"];
  14. NSArray<NSOperation*> *bOps = [subGroupQueue subGroupOperationsForKey:@"B"];
  15. NSArray<NSOperation*> *cOps = [subGroupQueue subGroupOperationsForKey:@"C"];
  16. // Objective-C uses a `DynamicSubGroupOperationQueue` which allows a more flexible usage, since keys only need to be `NSObject`'s (`AnyHashable`)
  17. [subGroupQueue addOperations:@[opN1, opN2, opN3] withKey:@1337 waitUntilFinished:false];
  18. NSDate *date = [NSDate date];
  19. [subGroupQueue addOperationWithBlock:^{ /* opD1 */ } andKey:date];
  20. [subGroupQueue addOperationWithBlock:^{ /* opD2 */ } andKey:date];

Compatibility

4.x (current)

  • iOS 10.0+, macOS 10.12, tvOS 10.0+, watchOS 3.0+
  • Xcode 10.2+
  • Swift 5.0

3.x

  • iOS 10.0+, macOS 10.12, tvOS 10.0+, watchOS 3.0+
  • Xcode 10
  • Swift 4.2

2.x

  • iOS 8.0+, macOS 10.9+, tvOS 9.0+, watchOS 2.0+
  • 2.3.0
    • Xcode 9.4
    • Swift 4.1
  • 2.2.0
    • Xcode 9
    • Swift 4.0
  • 2.1.0
    • Xcode 8
    • Swift 3

Integration

CocoaPods

Add APNSubGroupOperationQueue to your Podfile and run pod install:

  1. # CocoaPods
  2. pod 'APNSubGroupOperationQueue', '~> 4.0'

Carthage

Add APNSubGroupOperationQueue to your Cartfile (package dependency) or Cartfile.private
(development dependency):

  1. github "p4checo/APNSubGroupOperationQueue" ~> 4.0

Swift Package Manager

Add APNSubGroupOperationQueue to your Package.swift:

  1. import PackageDescription
  2. let package = Package(
  3. name: "HelloWorld",
  4. dependencies: [
  5. .package(url: "https://github.com/p4checo/APNSubGroupOperationQueue.git", from: "4.0.0"),
  6. ]
  7. )

git Submodule

  1. Add this repository as a submodule.
  2. Drag APNSubGroupOperationQueue.xcodeproj into your project or workspace.
  3. Link your target against APNSubGroupOperationQueue.framework of your platform.
  4. If linking againts an Application target, ensure the framework gets copied into the bundle. If linking against a Framework target, the application linking to it should also include APNSubGroupOperationQueue.

Contributing

See CONTRIBUTING.