Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
17
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer.h"
18
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORTransformer_Private.h"
19
 
20
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORAssert.h"
21
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORLifecycle.h"
22
#import "GoogleDataTransport/GDTCORLibrary/Internal/GDTCORStorageProtocol.h"
23
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCORConsoleLogger.h"
24
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREvent.h"
25
#import "GoogleDataTransport/GDTCORLibrary/Public/GoogleDataTransport/GDTCOREventTransformer.h"
26
 
27
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCOREvent_Private.h"
28
#import "GoogleDataTransport/GDTCORLibrary/Private/GDTCORRegistrar_Private.h"
29
 
30
@implementation GDTCORTransformer
31
 
32
+ (instancetype)sharedInstance {
33
  static GDTCORTransformer *eventTransformer;
34
  static dispatch_once_t onceToken;
35
  dispatch_once(&onceToken, ^{
36
    eventTransformer = [[self alloc] init];
37
  });
38
  return eventTransformer;
39
}
40
 
41
- (instancetype)init {
42
  return [self initWithApplication:[GDTCORApplication sharedApplication]];
43
}
44
 
45
- (instancetype)initWithApplication:(id<GDTCORApplicationProtocol>)application {
46
  self = [super init];
47
  if (self) {
48
    _eventWritingQueue =
49
        dispatch_queue_create("com.google.GDTCORTransformer", DISPATCH_QUEUE_SERIAL);
50
    _application = application;
51
  }
52
  return self;
53
}
54
 
55
- (void)transformEvent:(GDTCOREvent *)event
56
      withTransformers:(NSArray<id<GDTCOREventTransformer>> *)transformers
57
            onComplete:(void (^_Nullable)(BOOL wasWritten, NSError *_Nullable error))completion {
58
  GDTCORAssert(event, @"You can't write a nil event");
59
 
60
  __block GDTCORBackgroundIdentifier bgID = GDTCORBackgroundIdentifierInvalid;
61
  __auto_type __weak weakApplication = self.application;
62
  bgID = [self.application beginBackgroundTaskWithName:@"GDTTransformer"
63
                                     expirationHandler:^{
64
                                       [weakApplication endBackgroundTask:bgID];
65
                                       bgID = GDTCORBackgroundIdentifierInvalid;
66
                                     }];
67
 
68
  __auto_type completionWrapper = ^(BOOL wasWritten, NSError *_Nullable error) {
69
    if (completion) {
70
      completion(wasWritten, error);
71
    }
72
 
73
    // The work is done, cancel the background task if it's valid.
74
    [weakApplication endBackgroundTask:bgID];
75
    bgID = GDTCORBackgroundIdentifierInvalid;
76
  };
77
 
78
  dispatch_async(_eventWritingQueue, ^{
79
    GDTCOREvent *transformedEvent = event;
80
    for (id<GDTCOREventTransformer> transformer in transformers) {
81
      if ([transformer respondsToSelector:@selector(transformGDTEvent:)]) {
82
        GDTCORLogDebug(@"Applying a transformer to event %@", event);
83
        transformedEvent = [transformer transformGDTEvent:event];
84
        if (!transformedEvent) {
85
          completionWrapper(NO, nil);
86
          return;
87
        }
88
      } else {
89
        GDTCORLogError(GDTCORMCETransformerDoesntImplementTransform,
90
                       @"Transformer doesn't implement transformGDTEvent: %@", transformer);
91
        completionWrapper(NO, nil);
92
        return;
93
      }
94
    }
95
 
96
    id<GDTCORStorageProtocol> storage =
97
        [GDTCORRegistrar sharedInstance].targetToStorage[@(event.target)];
98
 
99
    [storage storeEvent:transformedEvent onComplete:completionWrapper];
100
  });
101
}
102
 
103
#pragma mark - GDTCORLifecycleProtocol
104
 
105
- (void)appWillTerminate:(GDTCORApplication *)application {
106
  // Flush the queue immediately.
107
  dispatch_sync(_eventWritingQueue, ^{
108
                });
109
}
110
 
111
@end