Advertisement
Badal_hs_shah

past item operation

Jan 31st, 2023
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  RW3PastPriceItemsOperation.m
  3. //  Pecos
  4. //
  5. //  Created by Vitaly Dyachkov on 27.05.15.
  6. //  Copyright (c) 2015 Vitaly Dyachkov. All rights reserved.
  7. //
  8.  
  9. #import "RW3PastPriceItemsOperation.h"
  10. #import "RW3PastPriceItem.h"
  11.  
  12. #define BUFFER_SIZE 500
  13.  
  14. @interface RW3PastPriceItemsOperation ()
  15.  
  16. @property (nonatomic, readonly) NSManagedObjectContext *context;
  17. @property (nonatomic, readonly) NSMutableArray *items;
  18.  
  19. @property (nonatomic, readonly) NSMutableDictionary *buffer;
  20.  
  21. @property (nonatomic, strong) NSArray *alternatviePriceReasons;
  22.  
  23. @end
  24.  
  25. @implementation RW3PastPriceItemsOperation
  26.  
  27. - (instancetype)initWithRequest:(NSURLRequest *)request context:(NSManagedObjectContext *)context
  28. {
  29.    self = [super initWithRequest:request];
  30.    if (self) {
  31.       RW3LogNormal();
  32.       _context = context;
  33.      
  34.       _items = [[NSMutableArray alloc] init];
  35.       _buffer = [[NSMutableDictionary alloc] initWithCapacity:BUFFER_SIZE];
  36.      
  37.       __weak __typeof(self) weakSelf = self;
  38.       [self setProcessingBlock:^(NSDictionary *JSONObject) {
  39.          __strong __typeof(self) strongSelf = weakSelf;
  40.          
  41.          if (!strongSelf.alternatviePriceReasons) {
  42.             [strongSelf.context performBlockAndWait:^{
  43.                NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"AlternativePriceReason"];
  44.                
  45.                NSError *error = nil;
  46.                strongSelf.alternatviePriceReasons = [context executeFetchRequest:request error:&error];
  47.                if (!strongSelf.alternatviePriceReasons) {
  48.                   RW3LogError(@"Error occurred while executing fetch request: %@", error);
  49.                }
  50.             }];
  51.          }
  52.          
  53.          NSString *upc = JSONObject[@"Upc"];
  54.          strongSelf.buffer[upc] = JSONObject;
  55.          
  56.          if ([strongSelf.buffer count] == BUFFER_SIZE) {
  57.             [strongSelf processBufferedItems];
  58.          }
  59.       }];
  60.    }
  61.    return self;
  62. }
  63.  
  64. - (void)processBufferedItems
  65. {
  66.    NSManagedObjectContext *context = self.context;
  67.    
  68.    [context performBlockAndWait:^{
  69.       for (NSString *upc in [self.buffer allKeys]) {
  70.          RW3PastPriceItem *item = [NSEntityDescription insertNewObjectForEntityForName:@"PastPriceItem" inManagedObjectContext:context];
  71.          NSDictionary *JSONObject = self.buffer[upc];
  72.          [item populateWithJSON:JSONObject alternativePriceReasons:self.alternatviePriceReasons];
  73.          [self.items addObject:item];
  74.       }
  75.    }];
  76.    
  77.    [self.buffer removeAllObjects];
  78. }
  79.  
  80. - (void)setCompletionBlockWithSuccess:(void (^)(NSArray *items))success failure:(void (^)(NSError *error))failure
  81. {
  82.    [super setCompletionBlockWithSuccess:^(RW3JSONRequestOperation *operation) {
  83.       RW3PastPriceItemsOperation *pastPriceItemsOperation = (RW3PastPriceItemsOperation *)operation;
  84.      
  85.       [pastPriceItemsOperation processBufferedItems];
  86.       RW3LogNormal(@"Processed %lu past price items.", (unsigned long)pastPriceItemsOperation.items.count);
  87.      
  88.       success(pastPriceItemsOperation.items);
  89.    } failure:^(RW3JSONRequestOperation *operation, NSError *error) {
  90.       failure(error);
  91.    }];
  92. }
  93.  
  94. @end
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement