ReaderThumbQueue.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // ReaderThumbQueue.m
  3. // Reader v2.8.6
  4. //
  5. // Created by Julius Oklamcak on 2011-09-01.
  6. // Copyright © 2011-2015 Julius Oklamcak. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights to
  11. // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. // of the Software, and to permit persons to whom the Software is furnished to
  13. // do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  23. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. //
  25. #import "ReaderThumbQueue.h"
  26. @implementation ReaderThumbQueue
  27. {
  28. NSOperationQueue *loadQueue;
  29. NSOperationQueue *workQueue;
  30. }
  31. #pragma mark - ReaderThumbQueue class methods
  32. + (ReaderThumbQueue *)sharedInstance
  33. {
  34. static dispatch_once_t predicate = 0;
  35. static ReaderThumbQueue *object = nil; // Object
  36. dispatch_once(&predicate, ^{ object = [self new]; });
  37. return object; // ReaderThumbQueue singleton
  38. }
  39. #pragma mark - ReaderThumbQueue instance methods
  40. - (instancetype)init
  41. {
  42. if ((self = [super init])) // Initialize
  43. {
  44. loadQueue = [NSOperationQueue new];
  45. [loadQueue setName:@"ReaderThumbLoadQueue"];
  46. [loadQueue setMaxConcurrentOperationCount:1];
  47. workQueue = [NSOperationQueue new];
  48. [workQueue setName:@"ReaderThumbWorkQueue"];
  49. [workQueue setMaxConcurrentOperationCount:1];
  50. }
  51. return self;
  52. }
  53. - (void)addLoadOperation:(NSOperation *)operation
  54. {
  55. if ([operation isKindOfClass:[ReaderThumbOperation class]])
  56. {
  57. [loadQueue addOperation:operation]; // Add to load queue
  58. }
  59. }
  60. - (void)addWorkOperation:(NSOperation *)operation
  61. {
  62. if ([operation isKindOfClass:[ReaderThumbOperation class]])
  63. {
  64. [workQueue addOperation:operation]; // Add to work queue
  65. }
  66. }
  67. - (void)cancelOperationsWithGUID:(NSString *)guid
  68. {
  69. [loadQueue setSuspended:YES]; [workQueue setSuspended:YES];
  70. for (ReaderThumbOperation *operation in loadQueue.operations)
  71. {
  72. if ([operation isKindOfClass:[ReaderThumbOperation class]])
  73. {
  74. if ([operation.guid isEqualToString:guid]) [operation cancel];
  75. }
  76. }
  77. for (ReaderThumbOperation *operation in workQueue.operations)
  78. {
  79. if ([operation isKindOfClass:[ReaderThumbOperation class]])
  80. {
  81. if ([operation.guid isEqualToString:guid]) [operation cancel];
  82. }
  83. }
  84. [workQueue setSuspended:NO]; [loadQueue setSuspended:NO];
  85. }
  86. - (void)cancelAllOperations
  87. {
  88. [loadQueue cancelAllOperations]; [workQueue cancelAllOperations];
  89. }
  90. @end
  91. #pragma mark -
  92. //
  93. // ReaderThumbOperation class implementation
  94. //
  95. @implementation ReaderThumbOperation
  96. {
  97. NSString *_guid;
  98. }
  99. @synthesize guid = _guid;
  100. #pragma mark - ReaderThumbOperation instance methods
  101. - (instancetype)initWithGUID:(NSString *)guid
  102. {
  103. if ((self = [super init]))
  104. {
  105. _guid = guid;
  106. }
  107. return self;
  108. }
  109. @end