GDTCORTransformer.m 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "GDTCORLibrary/Private/GDTCORTransformer.h"
  17. #import "GDTCORLibrary/Private/GDTCORTransformer_Private.h"
  18. #import <GoogleDataTransport/GDTCORAssert.h>
  19. #import <GoogleDataTransport/GDTCORConsoleLogger.h>
  20. #import <GoogleDataTransport/GDTCOREventTransformer.h>
  21. #import <GoogleDataTransport/GDTCORLifecycle.h>
  22. #import "GDTCORLibrary/Private/GDTCORStorage.h"
  23. @implementation GDTCORTransformer
  24. + (instancetype)sharedInstance {
  25. static GDTCORTransformer *eventTransformer;
  26. static dispatch_once_t onceToken;
  27. dispatch_once(&onceToken, ^{
  28. eventTransformer = [[self alloc] init];
  29. });
  30. return eventTransformer;
  31. }
  32. - (instancetype)init {
  33. self = [super init];
  34. if (self) {
  35. _eventWritingQueue =
  36. dispatch_queue_create("com.google.GDTCORTransformer", DISPATCH_QUEUE_SERIAL);
  37. _storageInstance = [GDTCORStorage sharedInstance];
  38. }
  39. return self;
  40. }
  41. - (void)transformEvent:(GDTCOREvent *)event
  42. withTransformers:(NSArray<id<GDTCOREventTransformer>> *)transformers {
  43. GDTCORAssert(event, @"You can't write a nil event");
  44. __block GDTCORBackgroundIdentifier bgID = GDTCORBackgroundIdentifierInvalid;
  45. bgID = [[GDTCORApplication sharedApplication]
  46. beginBackgroundTaskWithName:@"GDTTransformer"
  47. expirationHandler:^{
  48. [[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
  49. bgID = GDTCORBackgroundIdentifierInvalid;
  50. }];
  51. dispatch_async(_eventWritingQueue, ^{
  52. GDTCOREvent *transformedEvent = event;
  53. for (id<GDTCOREventTransformer> transformer in transformers) {
  54. if ([transformer respondsToSelector:@selector(transform:)]) {
  55. transformedEvent = [transformer transform:transformedEvent];
  56. if (!transformedEvent) {
  57. return;
  58. }
  59. } else {
  60. GDTCORLogError(GDTCORMCETransformerDoesntImplementTransform,
  61. @"Transformer doesn't implement transform: %@", transformer);
  62. return;
  63. }
  64. }
  65. [self.storageInstance storeEvent:transformedEvent];
  66. // The work is done, cancel the background task if it's valid.
  67. [[GDTCORApplication sharedApplication] endBackgroundTask:bgID];
  68. bgID = GDTCORBackgroundIdentifierInvalid;
  69. });
  70. }
  71. #pragma mark - GDTCORLifecycleProtocol
  72. - (void)appWillTerminate:(GDTCORApplication *)application {
  73. // Flush the queue immediately.
  74. dispatch_sync(_eventWritingQueue, ^{
  75. });
  76. }
  77. @end