Skip to content

Commit 4baeb82

Browse files
authored
Address floc review comments (#6691)
* Address floc review comments * Fix style * fix bad synthesize * Remove segmentation constants from core * fix style
1 parent 13beb58 commit 4baeb82

File tree

6 files changed

+76
-84
lines changed

6 files changed

+76
-84
lines changed

FirebaseSegmentation/Sources/Public/FIRSegmentation.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ NS_ASSUME_NONNULL_BEGIN
2020

2121
@class FIRApp;
2222
/**
23-
* The Firebase Segmentation SDK is used to associate a custom, non-Firebase custom installation
23+
* The Firebase Segmentation SDK is used to associate a custom, non-Firebase installation
2424
* identifier to Firebase. Once this custom installation identifier is set, developers can use the
2525
* current app installation for segmentation purposes. If the custom installation identifier is
2626
* explicitely set to nil, any existing custom installation identifier data will be removed.
@@ -56,7 +56,7 @@ typedef NS_ENUM(NSInteger, FIRSegmentationErrorCode) {
5656
/// Returns the FIRSegmentation instance for your Firebase application. This singleton class
5757
/// instance lets you set your own custom identifier to be used for targeting purposes within
5858
/// Firebase.
59-
+ (instancetype)segmentationWithApp:(nonnull FIRApp *)app;
59+
+ (instancetype)segmentationWithApp:(FIRApp *)app;
6060

6161
/**
6262
* :nodoc:

FirebaseSegmentation/Sources/SEGContentManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ NS_ASSUME_NONNULL_BEGIN
2323
@interface SEGContentManager : NSObject
2424

2525
/// Shared Singleton Instance
26-
+ (instancetype)sharedInstanceWithOptions:(FIROptions*)options;
26+
+ (instancetype)sharedInstanceWithOptions:(FIROptions *)options;
2727

28-
- (void)associateCustomInstallationIdentiferNamed:(NSString*)customInstallationID
29-
firebaseApp:(NSString*)appName
28+
- (void)associateCustomInstallationIdentiferNamed:(NSString *)customInstallationID
29+
firebaseApp:(NSString *)appName
3030
completion:(SEGRequestCompletion)completionHandler;
3131

3232
@end

FirebaseSegmentation/Sources/SEGContentManager.m

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,14 @@
1919
#import "FirebaseSegmentation/Sources/Public/FIRSegmentation.h"
2020
#import "FirebaseSegmentation/Sources/SEGDatabaseManager.h"
2121
#import "FirebaseSegmentation/Sources/SEGNetworkManager.h"
22-
#import "FirebaseSegmentation/Sources/SEGSegmentationConstants.h"
2322

24-
NSString *const kErrorDescription = @"ErrorDescription";
25-
26-
@interface SEGContentManager () {
23+
@implementation SEGContentManager {
2724
NSMutableDictionary<NSString *, id> *_associationData;
2825
NSString *_installationIdentifier;
2926
NSString *_installationIdentifierToken;
3027
SEGDatabaseManager *_databaseManager;
3128
SEGNetworkManager *_networkManager;
3229
}
33-
@end
34-
35-
@implementation SEGContentManager
3630

3731
+ (instancetype)sharedInstanceWithOptions:(FIROptions *)options {
3832
static dispatch_once_t onceToken;
@@ -111,19 +105,13 @@ - (void)associateInstallationWithLatestIdentifier:(NSString *_Nullable)identifie
111105

112106
_installationIdentifier = identifier;
113107

114-
__weak SEGContentManager *weakSelf = self;
115108
[installation authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
116109
NSError *_Nullable error) {
117-
SEGContentManager *strongSelf = weakSelf;
118-
if (!strongSelf) {
119-
completionHandler(NO, @{kErrorDescription : @"Internal Error getting installation token."});
120-
return;
121-
}
122-
[strongSelf associateInstallationWithToken:tokenResult
123-
customizedIdentifier:customInstallationID
124-
firebaseApp:firebaseApp
125-
error:error
126-
completion:completionHandler];
110+
[self associateInstallationWithToken:tokenResult
111+
customizedIdentifier:customInstallationID
112+
firebaseApp:firebaseApp
113+
error:error
114+
completion:completionHandler];
127115
}];
128116
}
129117

FirebaseSegmentation/Sources/SEGDatabaseManager.m

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,16 @@ static BOOL SEGAddSkipBackupAttributeToItemAtPath(NSString *filePathString) {
3838
NSError *error = nil;
3939
BOOL success = [URL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:&error];
4040
if (!success) {
41-
// TODO(dmandar): log error.
42-
NSLog(@"Error excluding %@ from backup %@.", [URL lastPathComponent], error);
41+
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000001", @"Error excluding %@ from backup %@.",
42+
[URL lastPathComponent], error);
4343
}
4444
return success;
4545
}
4646

4747
static BOOL SEGCreateFilePathIfNotExist(NSString *filePath) {
48-
if (!filePath || !filePath.length) {
49-
// TODO(dmandar) log error.
50-
NSLog(@"Failed to create subdirectory for an empty file path.");
48+
if (!filePath.length) {
49+
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000002",
50+
@"Failed to create subdirectory for an empty file path.");
5151
return NO;
5252
}
5353
NSFileManager *fileManager = [NSFileManager defaultManager];
@@ -58,23 +58,20 @@ static BOOL SEGCreateFilePathIfNotExist(NSString *filePath) {
5858
attributes:nil
5959
error:&error];
6060
if (error) {
61-
// TODO(dmandar) log error.
62-
NSLog(@"Failed to create subdirectory for database file: %@.", error);
61+
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000003",
62+
@"Failed to create subdirectory for database file: %@.", error);
6363
return NO;
6464
}
6565
}
6666
return YES;
6767
}
6868

69-
@interface SEGDatabaseManager () {
69+
@implementation SEGDatabaseManager {
7070
/// Database storing all the config information.
7171
sqlite3 *_database;
7272
/// Serial queue for database read/write operations.
7373
dispatch_queue_t _databaseOperationQueue;
7474
}
75-
@end
76-
77-
@implementation SEGDatabaseManager
7875

7976
+ (instancetype)sharedInstance {
8077
static dispatch_once_t onceToken;
@@ -97,41 +94,35 @@ - (instancetype)init {
9794
#pragma mark - Public Methods
9895

9996
- (void)loadMainTableWithCompletion:(SEGRequestCompletion)completionHandler {
100-
__weak SEGDatabaseManager *weakSelf = self;
10197
dispatch_async(_databaseOperationQueue, ^{
102-
SEGDatabaseManager *strongSelf = weakSelf;
103-
if (!strongSelf) {
104-
completionHandler(NO, @{@"Database Error" : @"Internal database error"});
105-
}
106-
10798
// Read the database into memory.
10899
NSDictionary<NSString *, NSDictionary<NSString *, NSString *> *> *associations =
109100
[self loadMainTable];
110-
completionHandler(YES, associations);
101+
if (associations != nil) {
102+
completionHandler(YES, associations);
103+
} else {
104+
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000004", @"Failed to load main table.");
105+
completionHandler(NO, @{});
106+
}
111107
});
112108
return;
113109
}
114110

115111
- (void)createOrOpenDatabaseWithCompletion:(SEGRequestCompletion)completionHandler {
116-
__weak SEGDatabaseManager *weakSelf = self;
117112
dispatch_async(_databaseOperationQueue, ^{
118-
SEGDatabaseManager *strongSelf = weakSelf;
119-
if (!strongSelf) {
120-
completionHandler(NO, @{@"ErrorDescription" : @"Internal database error"});
121-
}
122113
NSString *dbPath = [SEGDatabaseManager pathForSegmentationDatabase];
123-
// TODO(dmandar) log.
124-
NSLog(@"Loading segmentation database at path %@", dbPath);
114+
FIRLogDebug(kFIRLoggerSegmentation, @"I-SEG000005", @"Loading segmentation database at path %@",
115+
dbPath);
125116
const char *databasePath = dbPath.UTF8String;
126117
// Create or open database path.
127118
if (!SEGCreateFilePathIfNotExist(dbPath)) {
128-
completionHandler(NO, @{@"ErrorDescription" : @"Could not create database file at path"});
119+
completionHandler(NO, @{kSEGErrorDescription : @"Could not create database file at path"});
129120
}
130121
int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_FILEPROTECTION_COMPLETE |
131122
SQLITE_OPEN_FULLMUTEX;
132-
if (sqlite3_open_v2(databasePath, &strongSelf->_database, flags, NULL) == SQLITE_OK) {
123+
if (sqlite3_open_v2(databasePath, &self->_database, flags, NULL) == SQLITE_OK) {
133124
// Create table if does not exist already.
134-
if ([strongSelf createTableSchema]) {
125+
if ([self createTableSchema]) {
135126
// DB file created or already exists.
136127
// Exclude the app data used from iCloud backup.
137128
SEGAddSkipBackupAttributeToItemAtPath(dbPath);
@@ -142,42 +133,44 @@ - (void)createOrOpenDatabaseWithCompletion:(SEGRequestCompletion)completionHandl
142133

143134
} else {
144135
// Remove database before fail.
145-
[strongSelf removeDatabase:dbPath];
146-
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000010", @"Failed to create table.");
136+
[self removeDatabase:dbPath];
137+
FIRLogError(kFIRLoggerSegmentation, @"I-SEG00006", @"Failed to create table.");
147138
// Create a new database if existing database file is corrupted.
148139
if (!SEGCreateFilePathIfNotExist(dbPath)) {
149-
completionHandler(NO,
150-
@{@"ErrorDescription" : @"Could not recreate database file at path"});
140+
completionHandler(
141+
NO,
142+
@{kSEGErrorDescription : @"Could not recreate database file at path: %@", dbpath});
143+
return;
151144
}
152-
if (sqlite3_open_v2(databasePath, &strongSelf->_database, flags, NULL) == SQLITE_OK) {
153-
if (![strongSelf createTableSchema]) {
145+
// Try to open the database with the new file.
146+
if (sqlite3_open_v2(databasePath, &self->_database, flags, NULL) == SQLITE_OK) {
147+
if (![self createTableSchema]) {
154148
// Remove database before fail.
155-
[strongSelf removeDatabase:dbPath];
149+
[self removeDatabase:dbPath];
156150
// If it failed again, there's nothing we can do here.
157-
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000010", @"Failed to create table.");
151+
FIRLogError(kFIRLoggerSegmentation, @"I-SEG00007", @"Failed to create table.");
152+
completionHandler(NO, @{kSEGErrorDescription : @"Failed to re-open new database file"});
158153
} else {
159154
// Exclude the app data used from iCloud backup.
160155
SEGAddSkipBackupAttributeToItemAtPath(dbPath);
156+
// Skip reading the db into memory, since it's empty.
157+
completionHandler(YES, @{});
161158
}
162159
} else {
163-
[strongSelf logDatabaseError];
164-
completionHandler(NO, @{@"ErrorDescription" : @"Could not create database."});
160+
[self logDatabaseError];
161+
completionHandler(NO, @{kSEGErrorDescription : @"Could not create database."});
165162
}
166163
}
167164
} else {
168-
[strongSelf logDatabaseError];
169-
completionHandler(NO, @{@"ErrorDescription" : @"Error creating database."});
165+
[self logDatabaseError];
166+
completionHandler(NO, @{kSEGErrorDescription : @"Error creating database."});
170167
}
171168
});
172169
}
173170

174171
- (void)removeDatabase:(NSString *)path completion:(SEGRequestCompletion)completionHandler {
175172
dispatch_async(_databaseOperationQueue, ^{
176-
SEGDatabaseManager *strongSelf = self;
177-
if (!strongSelf) {
178-
return;
179-
}
180-
[strongSelf removeDatabase:path];
173+
[self removeDatabase:path];
181174
completionHandler(YES, nil);
182175
});
183176
}
@@ -193,6 +186,8 @@ - (NSDictionary *)loadMainTable {
193186

194187
sqlite3_stmt *statement = [self prepareSQL:[SQLQuery cStringUsingEncoding:NSUTF8StringEncoding]];
195188
if (!statement) {
189+
FIRLogError(kFIRLoggerSegmentation, @"I-SEG00008",
190+
@"Failed to create sqlite statement with query: %@.", SQLQuery);
196191
return nil;
197192
}
198193

@@ -279,12 +274,11 @@ - (void)insertMainTableApplicationNamed:(NSString *)firebaseApplication
279274
associationStatus:(NSString *)associationStatus
280275
completionHandler:(SEGRequestCompletion)handler {
281276
// TODO: delete the row first.
282-
__weak SEGDatabaseManager *weakSelf = self;
283277
dispatch_async(_databaseOperationQueue, ^{
284-
NSArray<NSString *> *values =
285-
[[NSArray alloc] initWithObjects:firebaseApplication, customInstanceIdentifier,
286-
firebaseInstanceIdentifier, associationStatus, nil];
287-
BOOL success = [weakSelf insertMainTableWithValues:values];
278+
NSArray<NSString *> *values = @[
279+
firebaseApplication, customInstanceIdentifier, firebaseInstanceIdentifier, associationStatus
280+
];
281+
BOOL success = [self insertMainTableWithValues:values];
288282
if (handler) {
289283
dispatch_async(dispatch_get_main_queue(), ^{
290284
handler(success, nil);
@@ -367,7 +361,7 @@ - (BOOL)bindStringsToStatement:(sqlite3_stmt *)statement stringArray:(NSArray *)
367361
int index = 1;
368362
for (NSString *param in array) {
369363
if (![self bindStringToStatement:statement index:index string:param]) {
370-
return [self logErrorWithSQL:nil finalizeStatement:statement returnValue:NO];
364+
return [self logErrorWithSQL:sql finalizeStatement:statement returnValue:NO];
371365
}
372366
index++;
373367
}
@@ -408,6 +402,15 @@ - (BOOL)logErrorWithSQL:(const char *)SQL
408402
returnValue:(BOOL)returnValue {
409403
if (SQL) {
410404
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000016", @"Failed with SQL: %s.", SQL);
405+
} else {
406+
const char *sqlString = sqlite3_sql(statement);
407+
NSString *sql;
408+
if (sqlString != NULL) {
409+
sql = [NSString stringWithCString:sqlString encoding:NSUTF8StringEncoding];
410+
}
411+
if (sql) {
412+
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000016", @"Failed with SQL: %s.", SQL);
413+
}
411414
}
412415
[self logDatabaseError];
413416

FirebaseSegmentation/Sources/SEGNetworkManager.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ - (void)makeAssociationRequestToBackendWithData:
9292
NSString *URL = [self constructServerURLWithAssociationData:associationData];
9393
if (!URL) {
9494
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000020", @"Could not construct backend URL.");
95-
completionHandler(NO, @{@"errorDescription" : @"Could not construct backend URL"});
95+
completionHandler(NO, @{kSEGErrorDescription : @"Could not construct backend URL"});
9696
}
9797

9898
FIRLogDebug(kFIRLoggerSegmentation, @"I-SEG000019", @"%@",
@@ -112,7 +112,7 @@ - (void)makeAssociationRequestToBackendWithData:
112112
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000021", @"Could not create request data. %@",
113113
error.localizedDescription);
114114
completionHandler(NO,
115-
@{@"errorDescription" : @"Could not serialize JSON data for network call."});
115+
@{kSEGErrorDescription : @"Could not serialize JSON data for network call."});
116116
}
117117

118118
// Handle NSURLSession completion.
@@ -126,7 +126,7 @@ - (void)makeAssociationRequestToBackendWithData:
126126
FIRLogError(kFIRLoggerSegmentation, @"I-SEG000022",
127127
@"Internal error making network request.");
128128
completionHandler(
129-
NO, @{@"errorDescription" : @"Internal error making network request."});
129+
NO, @{kSEGErrorDescription : @"Internal error making network request."});
130130
return;
131131
}
132132

@@ -140,7 +140,7 @@ - (void)makeAssociationRequestToBackendWithData:
140140
@"SEGNetworkManager: Network request failed with status code:%lu",
141141
(long)statusCode);
142142
completionHandler(NO, @{
143-
@"ErrorDescription" :
143+
kSEGErrorDescription :
144144
[NSString stringWithFormat:@"Network Error: %lu", (long)statusCode]
145145
});
146146
};

FirebaseSegmentation/Sources/SEGSegmentationConstants.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19-
#ifndef SEGSegmentationConstants_h
20-
#define SEGSegmentationConstants_h
21-
2219
#if defined(DEBUG)
2320
#define SEG_MUST_NOT_BE_MAIN_THREAD() \
2421
do { \
@@ -42,11 +39,15 @@ static NSString* const kSEGAssociationStatusPending = @"PENDING";
4239
static NSString* const kSEGAssociationStatusAssociated = @"ASSOCIATED";
4340

4441
/// Segmentation error domain when logging errors.
45-
static NSString* const kFirebaseSegmentationErrorDomain = @"com.firebase.segmentation";
42+
kFirebaseSegmentationErrorDomain = @"com.firebase.segmentation";
43+
44+
/// Segmentation FIRLogger domain.
45+
static NSString* const kFIRLoggerSegmentation = @"[Firebase/Segmentation]";
46+
47+
/// Used for reporting generic internal Segmentation errors.
48+
NSString* const kSEGErrorDescription = @"SEGErrorDescription";
4649

4750
/// Segmentation Request Completion callback.
4851
/// @param success Decide whether the network operation succeeds.
4952
/// @param result Return operation result data.
5053
typedef void (^SEGRequestCompletion)(BOOL success, NSDictionary<NSString*, id>* result);
51-
52-
#endif /* SEGSegmentationConstants_h */

0 commit comments

Comments
 (0)