Skip to content

Commit 53a0154

Browse files
author
Edward Smith
committed
Updated branch PR suggestions.
1 parent a3d1bc8 commit 53a0154

File tree

2 files changed

+62
-89
lines changed

2 files changed

+62
-89
lines changed

Branch-SDK/Branch-SDK/BNCPreferenceHelper.m

Lines changed: 35 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -609,62 +609,53 @@ - (void)writeObjectToDefaults:(NSString *)key value:(NSObject *)value {
609609
[self persistPrefsToDisk];
610610
}
611611

612-
- (void)persistPrefsToDisk
613-
{
614-
@synchronized (self)
615-
{
612+
- (void)persistPrefsToDisk {
613+
@synchronized (self) {
616614
NSDictionary *persistenceDict = [self.persistenceDict copy];
617-
NSBlockOperation *newPersistOp = [NSBlockOperation blockOperationWithBlock:
618-
^ {
615+
NSBlockOperation *newPersistOp = [NSBlockOperation blockOperationWithBlock:^ {
619616
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:persistenceDict];
620-
if (!data)
621-
{
617+
if (!data) {
622618
[self logWarning:@"Can't create preferences archive."];
623619
return;
624-
}
620+
}
625621
NSError *error = nil;
626622
[data writeToURL:self.class.URLForPrefsFile
627623
options:NSDataWritingAtomic error:&error];
628-
if (error)
629-
{
624+
if (error) {
630625
[self logWarning:
631626
[NSString stringWithFormat:
632627
@"Failed to persist preferences to disk: %@.", error]];
633-
}
634-
}];
635-
[self.persistPrefsQueue addOperation:newPersistOp];
636-
}
628+
}
629+
}];
630+
[self.persistPrefsQueue addOperation:newPersistOp];
637631
}
632+
}
638633

639634
#pragma mark - Reading From Persistence
640635

641-
- (NSMutableDictionary *)persistenceDict
642-
{
643-
if (!_persistenceDict)
644-
{
636+
- (NSMutableDictionary *)persistenceDict {
637+
if (!_persistenceDict) {
645638
NSDictionary *persistenceDict = nil;
646639
@try {
647640
NSError *error = nil;
648-
NSData *data =
649-
[NSData dataWithContentsOfURL:self.class.URLForPrefsFile
641+
NSData *data = [NSData dataWithContentsOfURL:self.class.URLForPrefsFile
650642
options:0 error:&error];
651643
if (error || !data)
652644
NSLog(@"Error opening prefs file: %@.", error);
653645
else
654646
persistenceDict = [NSKeyedUnarchiver unarchiveObjectWithData:data];
655-
}
656-
@catch (NSException *exception)
657-
{
647+
}
648+
@catch (NSException *exception) {
658649
[self logWarning:@"Failed to load preferences from disk."];
659-
}
650+
}
660651

661652
if ([persistenceDict isKindOfClass:[NSDictionary class]])
662653
_persistenceDict = [persistenceDict mutableCopy];
663654
else
664655
_persistenceDict = [[NSMutableDictionary alloc] init];
665-
}
666-
return _persistenceDict;
667656
}
657+
return _persistenceDict;
658+
}
668659

669660
- (NSObject *)readObjectFromDefaults:(NSString *)key {
670661
NSObject *obj = self.persistenceDict[key];
@@ -696,17 +687,15 @@ - (NSInteger)readIntegerFromDefaults:(NSString *)key {
696687
return NSNotFound;
697688
}
698689

699-
+ (NSString *)prefsFile_deprecated
700-
{
690+
+ (NSString *)prefsFile_deprecated {
701691
NSString * path =
702692
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
703693
firstObject]
704694
stringByAppendingPathComponent:BRANCH_PREFS_FILE];
705695
return path;
706-
}
696+
}
707697

708-
+ (NSURL*) URLForBranchDirectory
709-
{
698+
+ (NSURL*) URLForBranchDirectory {
710699
NSError *error = nil;
711700
NSURL *URL =
712701
[[NSFileManager defaultManager]
@@ -715,34 +704,30 @@ + (NSURL*) URLForBranchDirectory
715704
appropriateForURL:nil
716705
create:YES
717706
error:&error];
718-
if (error)
719-
{
707+
if (error) {
720708
NSLog(@"Error creating URLForPrefsDirectory: %@.", error);
721709
return nil;
722-
}
710+
}
723711
URL = [URL URLByAppendingPathComponent:@"io.branch"];
724712
[[NSFileManager defaultManager]
725713
createDirectoryAtURL:URL
726714
withIntermediateDirectories:YES
727715
attributes:nil
728716
error:&error];
729-
if (error)
730-
{
717+
if (error) {
731718
NSLog(@"Error creating URLForPrefsDirectory: %@.", error);
732719
return nil;
733-
}
734-
return URL;
735720
}
721+
return URL;
722+
}
736723

737-
+ (NSURL*) URLForPrefsFile
738-
{
724+
+ (NSURL*) URLForPrefsFile {
739725
NSURL *URL = [self URLForBranchDirectory];
740726
URL = [URL URLByAppendingPathComponent:BRANCH_PREFS_FILE];
741727
return URL;
742-
}
728+
}
743729

744-
+ (void) moveOldPrefsFile
745-
{
730+
+ (void) moveOldPrefsFile {
746731
NSURL *oldURL = [NSURL fileURLWithPath:self.prefsFile_deprecated];
747732
NSURL *newURL = [self URLForBranchDirectory];
748733

@@ -752,22 +737,19 @@ + (void) moveOldPrefsFile
752737
toURL:newURL
753738
error:&error];
754739

755-
if (error && error.code != NSFileNoSuchFileError)
756-
{
757-
if (error.code == NSFileWriteFileExistsError)
758-
{
740+
if (error && error.code != NSFileNoSuchFileError) {
741+
if (error.code == NSFileWriteFileExistsError) {
759742
[[NSFileManager defaultManager]
760743
removeItemAtURL:oldURL
761744
error:&error];
762-
}
763-
else
745+
} else {
764746
NSLog(@"Error moving prefs file: %@.", error);
765747
}
766748
}
749+
}
767750

768-
+ (void) initialize
769-
{
751+
+ (void) initialize {
770752
[self moveOldPrefsFile];
771-
}
753+
}
772754

773755
@end

Branch-SDK/Branch-SDK/BNCServerRequestQueue.m

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,12 @@ - (void)persistEventually {
187187

188188
- (void)persistImmediately {
189189
[self.writeTimer invalidate];
190-
191190
[self persistToDisk];
192191
}
193192

194193
- (void)persistToDisk {
195194
NSArray *requestsToPersist = [self.queue copy];
196-
dispatch_async(self.asyncQueue,
197-
^ {
195+
dispatch_async(self.asyncQueue, ^ {
198196
@try {
199197
NSMutableArray *encodedRequests = [[NSMutableArray alloc] init];
200198
for (BNCServerRequest *req in requestsToPersist) {
@@ -208,29 +206,26 @@ - (void)persistToDisk {
208206
}
209207

210208
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:encodedRequests];
211-
if (!data)
212-
{
209+
if (!data) {
213210
[[BNCPreferenceHelper preferenceHelper]
214211
logWarning:@"Cannot create archive data."];
215212
return;
216-
}
213+
}
217214
NSError *error = nil;
218215
[data writeToURL:self.class.URLForQueueFile
219216
options:NSDataWritingAtomic error:&error];
220-
if (error)
221-
{
217+
if (error) {
222218
[[BNCPreferenceHelper preferenceHelper] logWarning:
223219
[NSString stringWithFormat:@"Failed to persist queue to disk: %@", error]];
224-
}
225220
}
226-
@catch (NSException *exception)
227-
{
221+
}
222+
@catch (NSException *exception) {
228223
NSString *warningMessage =
229224
[NSString stringWithFormat:
230225
@"An exception occurred while attempting to save the queue. Exception information:\n\n%@",
231226
[self exceptionString:exception]];
232227
[[BNCPreferenceHelper preferenceHelper] logWarning:warningMessage];
233-
}
228+
}
234229
});
235230
}
236231

@@ -247,17 +242,16 @@ - (void)retrieve {
247242
[NSString stringWithFormat:@"Error loading network queue: %@.", error]];
248243
else
249244
encodedRequests = [NSKeyedUnarchiver unarchiveObjectWithData:data];
250-
}
251-
@catch (NSException *exception)
252-
{
245+
}
246+
@catch (NSException *exception) {
253247
NSString *warningMessage =
254248
[NSString stringWithFormat:
255249
@"An exception occurred while attempting to load the queue file, proceeding without requests. Exception information:\n\n%@",
256250
[self exceptionString:exception]];
257251
[[BNCPreferenceHelper preferenceHelper] logWarning:warningMessage];
258252
self.queue = queue;
259253
return;
260-
}
254+
}
261255

262256
for (NSData *encodedRequest in encodedRequests) {
263257
BNCServerRequest *request;
@@ -292,24 +286,21 @@ - (NSString *)exceptionString:(NSException *)exception {
292286
return [NSString stringWithFormat:@"Name: %@\nReason: %@\nStack:\n\t%@\n\n", exception.name, exception.reason, [exception.callStackSymbols componentsJoinedByString:@"\n\t"]];
293287
}
294288

295-
+ (NSString *)queueFile_deprecated
296-
{
289+
+ (NSString *)queueFile_deprecated {
297290
NSString *path =
298291
[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
299292
firstObject]
300293
stringByAppendingPathComponent:BRANCH_QUEUE_FILE];
301294
return path;
302-
}
295+
}
303296

304-
+ (NSURL*) URLForQueueFile
305-
{
297+
+ (NSURL*) URLForQueueFile {
306298
NSURL *URL = [BNCPreferenceHelper URLForBranchDirectory];
307299
URL = [URL URLByAppendingPathComponent:BRANCH_QUEUE_FILE];
308300
return URL;
309-
}
301+
}
310302

311-
+ (void) moveOldQueueFile
312-
{
303+
+ (void) moveOldQueueFile {
313304
NSURL *oldURL = [NSURL fileURLWithPath:self.queueFile_deprecated];
314305
NSURL *newURL = [self URLForQueueFile];
315306
NSError *error = nil;
@@ -318,36 +309,36 @@ + (void) moveOldQueueFile
318309
toURL:newURL
319310
error:&error];
320311

321-
if (error && error.code != NSFileNoSuchFileError)
322-
{
323-
if (error.code == NSFileWriteFileExistsError)
324-
{
312+
if (error && error.code != NSFileNoSuchFileError) {
313+
if (error.code == NSFileWriteFileExistsError) {
325314
[[NSFileManager defaultManager]
326315
removeItemAtURL:oldURL
327316
error:&error];
328-
}
329-
else
317+
} else {
330318
NSLog(@"Error moving queue file: %@.", error);
331319
}
332320
}
321+
}
333322

334-
+ (void) initialize
335-
{
323+
+ (void) initialize {
336324
[self moveOldQueueFile];
337-
}
325+
}
338326

339327
#pragma mark - Singleton method
340328

341329
+ (id)getInstance {
342330
static BNCServerRequestQueue *sharedQueue = nil;
343331
static dispatch_once_t onceToken;
344332

345-
dispatch_once(&onceToken, ^{
333+
dispatch_once(&onceToken, ^ {
346334
sharedQueue = [[BNCServerRequestQueue alloc] init];
347335
[sharedQueue retrieve];
348-
[[BNCPreferenceHelper preferenceHelper] log:FILE_NAME line:LINE_NUM message:@"Retrieved from Persist: %@", sharedQueue];
336+
[[BNCPreferenceHelper preferenceHelper]
337+
log:FILE_NAME
338+
line:LINE_NUM
339+
message:@"Retrieved from Persist: %@", sharedQueue];
349340
});
350-
341+
351342
return sharedQueue;
352343
}
353344

0 commit comments

Comments
 (0)