Advertisement
Guest User

VFDFirebaseManager+Stickers

a guest
Nov 12th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  VFDFirebaseManager+Stickers.h
  3. //  VK-Feed
  4. //
  5. //  Created by Дмитрий Кучин on 05/10/2018.
  6. //  Copyright © 2018 VFeed. All rights reserved.
  7. //
  8.  
  9. #import "VFDFirebaseManager.h"
  10. #import "VFDFirebasePack.h"
  11. #import "VFDFirebaseSticker.h"
  12.  
  13. NS_ASSUME_NONNULL_BEGIN
  14.  
  15. @interface VFDFirebaseManager (Stickers)
  16. - (void)saveStickersInPack:(VFDFirebasePack *)pack stickers:(NSArray<NSString *> *)stickersIds;
  17. - (void)saveStickers:(NSArray<VFDFirebaseSticker *> *)stickers withPack:(VFDFirebasePack *)pack;
  18. @end
  19.  
  20. NS_ASSUME_NONNULL_END
  21.  
  22.  
  23. /////////////////////IMPLEMENTATION////////////////////////////
  24.  
  25. //
  26. //  VFDFirebaseManager+Stickers.m
  27. //  VK-Feed
  28. //
  29. //  Created by Дмитрий Кучин on 05/10/2018.
  30. //  Copyright © 2018 VFeed. All rights reserved.
  31. //
  32.  
  33. #import "VFDFirebaseManager+Stickers.h"
  34. #import "VFDFirebaseManager+StickerPacks.h"
  35.  
  36. NSString * _Nonnull const VFDStickersKey = @"stickers";
  37.  
  38. @implementation VFDFirebaseManager (Stickers)
  39.  
  40. - (void)saveStickersInPack:(VFDFirebasePack *)pack stickers:(NSArray<NSString *> *)stickersIds {
  41.     FIRDocumentReference *packRef = [[self.dataBase collectionWithPath:VFDPacksKey] documentWithPath:pack.identifier];
  42.     [self.dataBase runTransactionWithBlock:^id _Nullable(FIRTransaction * _Nonnull snapshot, NSError * _Nullable __autoreleasing * _Nullable errorPointer) {
  43.         FIRDocumentSnapshot *snapshotPack = [snapshot getDocument:packRef error:errorPointer];
  44.         guardvar(NSMutableArray, temp, snapshotPack.data[VFDStickersKey], else { temp = NSMutableArray.array; })
  45.         [snapshot updateData:@{ VFDStickersKey : stickersIds } forDocument:packRef];
  46.         return nil;
  47.     } completion:^(id  _Nullable result, NSError * _Nullable error) {}];
  48. }
  49.  
  50. - (void)saveStickers:(NSArray<VFDFirebaseSticker *> *)stickers withPack:(VFDFirebasePack *)pack {
  51.     FIRWriteBatch *batch = self.dataBase.batch;
  52.     NSMutableArray *stickerIds = NSMutableArray.array;
  53.     for (VFDFirebaseSticker *sticker in stickers) {
  54.         NSMutableDictionary *temp = NSMutableDictionary.dictionary;
  55.         [temp addEntriesFromDictionary:@{@"packID" : pack.identifier}];
  56.         [temp addEntriesFromDictionary:sticker.dictionary];
  57.         FIRDocumentReference *stickersRef = [[self.dataBase collectionWithPath:VFDStickersKey] addDocumentWithData:temp];
  58.         [stickerIds addObject:stickersRef.documentID];
  59.         [batch setData:temp forDocument:stickersRef];
  60.     }
  61.     [batch commitWithCompletion:^(NSError * _Nullable error) {
  62.         [self saveStickersInPack:pack stickers:stickerIds];
  63.     }];
  64. }
  65.  
  66. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement