Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // VFDFirebaseManager+Stickers.h
- // VK-Feed
- //
- // Created by Дмитрий Кучин on 05/10/2018.
- // Copyright © 2018 VFeed. All rights reserved.
- //
- #import "VFDFirebaseManager.h"
- #import "VFDFirebasePack.h"
- #import "VFDFirebaseSticker.h"
- NS_ASSUME_NONNULL_BEGIN
- @interface VFDFirebaseManager (Stickers)
- - (void)saveStickersInPack:(VFDFirebasePack *)pack stickers:(NSArray<NSString *> *)stickersIds;
- - (void)saveStickers:(NSArray<VFDFirebaseSticker *> *)stickers withPack:(VFDFirebasePack *)pack;
- @end
- NS_ASSUME_NONNULL_END
- /////////////////////IMPLEMENTATION////////////////////////////
- //
- // VFDFirebaseManager+Stickers.m
- // VK-Feed
- //
- // Created by Дмитрий Кучин on 05/10/2018.
- // Copyright © 2018 VFeed. All rights reserved.
- //
- #import "VFDFirebaseManager+Stickers.h"
- #import "VFDFirebaseManager+StickerPacks.h"
- NSString * _Nonnull const VFDStickersKey = @"stickers";
- @implementation VFDFirebaseManager (Stickers)
- - (void)saveStickersInPack:(VFDFirebasePack *)pack stickers:(NSArray<NSString *> *)stickersIds {
- FIRDocumentReference *packRef = [[self.dataBase collectionWithPath:VFDPacksKey] documentWithPath:pack.identifier];
- [self.dataBase runTransactionWithBlock:^id _Nullable(FIRTransaction * _Nonnull snapshot, NSError * _Nullable __autoreleasing * _Nullable errorPointer) {
- FIRDocumentSnapshot *snapshotPack = [snapshot getDocument:packRef error:errorPointer];
- guardvar(NSMutableArray, temp, snapshotPack.data[VFDStickersKey], else { temp = NSMutableArray.array; })
- [snapshot updateData:@{ VFDStickersKey : stickersIds } forDocument:packRef];
- return nil;
- } completion:^(id _Nullable result, NSError * _Nullable error) {}];
- }
- - (void)saveStickers:(NSArray<VFDFirebaseSticker *> *)stickers withPack:(VFDFirebasePack *)pack {
- FIRWriteBatch *batch = self.dataBase.batch;
- NSMutableArray *stickerIds = NSMutableArray.array;
- for (VFDFirebaseSticker *sticker in stickers) {
- NSMutableDictionary *temp = NSMutableDictionary.dictionary;
- [temp addEntriesFromDictionary:@{@"packID" : pack.identifier}];
- [temp addEntriesFromDictionary:sticker.dictionary];
- FIRDocumentReference *stickersRef = [[self.dataBase collectionWithPath:VFDStickersKey] addDocumentWithData:temp];
- [stickerIds addObject:stickersRef.documentID];
- [batch setData:temp forDocument:stickersRef];
- }
- [batch commitWithCompletion:^(NSError * _Nullable error) {
- [self saveStickersInPack:pack stickers:stickerIds];
- }];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement