|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Parse, LLC. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | + |
| 10 | +#import "PFMemoryEventuallyQueue.h" |
| 11 | +#import "PFEventuallyQueue_Private.h" |
| 12 | + |
| 13 | +#import <Bolts/BFTask.h> |
| 14 | +#import <Bolts/BFExecutor.h> |
| 15 | + |
| 16 | +@interface PFMemoryEventuallyQueue () <PFEventuallyQueueSubclass> { |
| 17 | + dispatch_queue_t _dataAccessQueue; |
| 18 | + BFExecutor *_dataAccessExecutor; |
| 19 | + |
| 20 | + NSMutableArray PF_GENERIC(NSString *)*_pendingCommandIdentifiers; |
| 21 | + NSMutableDictionary PF_GENERIC(NSString *, id<PFNetworkCommand>)*_commandsDictionary; |
| 22 | +} |
| 23 | + |
| 24 | +@end |
| 25 | + |
| 26 | +@implementation PFMemoryEventuallyQueue |
| 27 | + |
| 28 | +///-------------------------------------- |
| 29 | +#pragma mark - Init |
| 30 | +///-------------------------------------- |
| 31 | + |
| 32 | ++ (instancetype)newDefaultMemoryEventuallyQueueWithCommandRunner:(id<PFCommandRunning>)commandRunner { |
| 33 | + PFMemoryEventuallyQueue *queue = [[self alloc] initWithCommandRunner:commandRunner |
| 34 | + maxAttemptsCount:PFEventuallyQueueDefaultMaxAttemptsCount |
| 35 | + retryInterval:PFEventuallyQueueDefaultTimeoutRetryInterval]; |
| 36 | + [queue start]; |
| 37 | + return queue; |
| 38 | +} |
| 39 | + |
| 40 | +- (instancetype)initWithCommandRunner:(id<PFCommandRunning>)commandRunner |
| 41 | + maxAttemptsCount:(NSUInteger)attemptsCount |
| 42 | + retryInterval:(NSTimeInterval)retryInterval { |
| 43 | + self = [super initWithCommandRunner:commandRunner maxAttemptsCount:attemptsCount retryInterval:retryInterval]; |
| 44 | + if (!self) return nil; |
| 45 | + |
| 46 | + _dataAccessQueue = dispatch_queue_create("com.parse.eventuallyQueue.memory", DISPATCH_QUEUE_SERIAL); |
| 47 | + _dataAccessExecutor = [BFExecutor executorWithDispatchQueue:_dataAccessQueue]; |
| 48 | + |
| 49 | + _pendingCommandIdentifiers = [NSMutableArray array]; |
| 50 | + _commandsDictionary = [NSMutableDictionary dictionary]; |
| 51 | + |
| 52 | + return self; |
| 53 | +} |
| 54 | + |
| 55 | +///-------------------------------------- |
| 56 | +#pragma mark - Controlling Queue |
| 57 | +///-------------------------------------- |
| 58 | + |
| 59 | +- (void)removeAllCommands { |
| 60 | + [super removeAllCommands]; |
| 61 | + |
| 62 | + dispatch_sync(_dataAccessQueue, ^{ |
| 63 | + [_pendingCommandIdentifiers removeAllObjects]; |
| 64 | + [_commandsDictionary removeAllObjects]; |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +///-------------------------------------- |
| 69 | +#pragma mark - PFEventuallyQueueSubclass |
| 70 | +///-------------------------------------- |
| 71 | + |
| 72 | +- (NSString *)_newIdentifierForCommand:(id<PFNetworkCommand>)command { |
| 73 | + return [NSUUID UUID].UUIDString; |
| 74 | +} |
| 75 | + |
| 76 | +- (NSArray PF_GENERIC(NSString *)*)_pendingCommandIdentifiers { |
| 77 | + __block NSArray *array = nil; |
| 78 | + dispatch_sync(_dataAccessQueue, ^{ |
| 79 | + array = [_pendingCommandIdentifiers copy]; |
| 80 | + }); |
| 81 | + return array; |
| 82 | +} |
| 83 | + |
| 84 | +- (id<PFNetworkCommand>)_commandWithIdentifier:(NSString *)identifier error:(NSError **)error { |
| 85 | + __block id<PFNetworkCommand> command = nil; |
| 86 | + dispatch_sync(_dataAccessQueue, ^{ |
| 87 | + command = _commandsDictionary[identifier]; |
| 88 | + }); |
| 89 | + return command; |
| 90 | +} |
| 91 | + |
| 92 | +- (BFTask *)_enqueueCommandInBackground:(id<PFNetworkCommand>)command object:(PFObject *)object identifier:(NSString *)identifier { |
| 93 | + return [BFTask taskFromExecutor:_dataAccessExecutor withBlock:^id{ |
| 94 | + [_pendingCommandIdentifiers addObject:identifier]; |
| 95 | + _commandsDictionary[identifier] = command; |
| 96 | + return nil; |
| 97 | + }]; |
| 98 | +} |
| 99 | + |
| 100 | +- (BFTask *)_didFinishRunningCommand:(id<PFNetworkCommand>)command withIdentifier:(NSString *)identifier resultTask:(BFTask *)resultTask { |
| 101 | + return [BFTask taskFromExecutor:_dataAccessExecutor withBlock:^id{ |
| 102 | + [_pendingCommandIdentifiers removeObject:identifier]; |
| 103 | + [_commandsDictionary removeObjectForKey:identifier]; |
| 104 | + return [super _didFinishRunningCommand:command withIdentifier:identifier resultTask:resultTask]; |
| 105 | + }]; |
| 106 | +} |
| 107 | + |
| 108 | +- (BFTask *)_waitForOperationSet:(PFOperationSet *)operationSet eventuallyPin:(PFEventuallyPin *)eventuallyPin { |
| 109 | + return [BFTask taskWithResult:nil]; |
| 110 | +} |
| 111 | + |
| 112 | +@end |
0 commit comments