Skip to content

Commit 34e9eee

Browse files
committed
Revert "[GDTCORFlatFileStoragecheckForExpirations] - autoreleasepool in cycles"
This reverts commit e0573f5.
1 parent f353289 commit 34e9eee

File tree

1 file changed

+39
-53
lines changed

1 file changed

+39
-53
lines changed

GoogleDataTransport/GDTCORLibrary/GDTCORFlatFileStorage.m

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -407,45 +407,33 @@ - (void)checkForExpirations {
407407
NSArray<NSString *> *batchDataPaths = [fileManager contentsOfDirectoryAtPath:batchDataPath
408408
error:nil];
409409
for (NSString *path in batchDataPaths) {
410-
@autoreleasepool {
411-
NSString *fileName = [path lastPathComponent];
412-
NSDictionary<NSString *, id> *batchComponents = [self batchComponentsFromFilename:fileName];
413-
NSDate *expirationDate = batchComponents[kGDTCORBatchComponentsExpirationKey];
410+
NSString *fileName = [path lastPathComponent];
411+
NSDictionary<NSString *, id> *batchComponents = [self batchComponentsFromFilename:fileName];
412+
NSDate *expirationDate = batchComponents[kGDTCORBatchComponentsExpirationKey];
413+
NSNumber *batchID = batchComponents[kGDTCORBatchComponentsBatchIDKey];
414+
if (expirationDate != nil && expirationDate.timeIntervalSince1970 < now && batchID != nil) {
414415
NSNumber *batchID = batchComponents[kGDTCORBatchComponentsBatchIDKey];
415-
if (expirationDate != nil && expirationDate.timeIntervalSince1970 < now && batchID != nil) {
416-
NSNumber *batchID = batchComponents[kGDTCORBatchComponentsBatchIDKey];
417-
// Move all events from the expired batch back to the main storage.
418-
[self syncThreadUnsafeRemoveBatchWithID:batchID deleteEvents:NO];
419-
}
416+
// Move all events from the expired batch back to the main storage.
417+
[self syncThreadUnsafeRemoveBatchWithID:batchID deleteEvents:NO];
420418
}
421419
}
422420

423421
// Find expired events and remove them from the storage.
424422
NSString *eventDataPath = [GDTCORFlatFileStorage eventDataStoragePath];
425423
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:eventDataPath];
426424
NSString *path;
427-
428-
while (YES) {
429-
@autoreleasepool {
430-
431-
// Call `[enumerator nextObject]` under autorelease pool to make sure all autoreleased objects created under the hood are released on each iteration end to avoid unnecessary memory growth.
432-
path = [enumerator nextObject];
433-
if (path == nil) {
434-
break;
435-
}
436-
437-
NSString *fileName = [path lastPathComponent];
438-
NSDictionary<NSString *, id> *eventComponents = [self eventComponentsFromFilename:fileName];
439-
NSDate *expirationDate = eventComponents[kGDTCOREventComponentsExpirationKey];
440-
if (expirationDate != nil && expirationDate.timeIntervalSince1970 < now) {
441-
NSString *pathToDelete = [eventDataPath stringByAppendingPathComponent:path];
442-
NSError *error;
443-
[fileManager removeItemAtPath:pathToDelete error:&error];
444-
if (error != nil) {
445-
GDTCORLogDebug(@"There was an error deleting an expired item: %@", error);
446-
} else {
447-
GDTCORLogDebug(@"Item deleted because it expired: %@", pathToDelete);
448-
}
425+
while ((path = [enumerator nextObject])) {
426+
NSString *fileName = [path lastPathComponent];
427+
NSDictionary<NSString *, id> *eventComponents = [self eventComponentsFromFilename:fileName];
428+
NSDate *expirationDate = eventComponents[kGDTCOREventComponentsExpirationKey];
429+
if (expirationDate != nil && expirationDate.timeIntervalSince1970 < now) {
430+
NSString *pathToDelete = [eventDataPath stringByAppendingPathComponent:path];
431+
NSError *error;
432+
[fileManager removeItemAtPath:pathToDelete error:&error];
433+
if (error != nil) {
434+
GDTCORLogDebug(@"There was an error deleting an expired item: %@", error);
435+
} else {
436+
GDTCORLogDebug(@"Item deleted because it expired: %@", pathToDelete);
449437
}
450438
}
451439
}
@@ -559,30 +547,28 @@ - (void)syncThreadUnsafeRemoveBatchWithID:(nonnull NSNumber *)batchID
559547
};
560548

561549
for (NSString *batchDirPath in batchDirPaths) {
562-
@autoreleasepool {
563-
if (deleteEvents) {
564-
removeBatchDir(batchDirPath);
550+
if (deleteEvents) {
551+
removeBatchDir(batchDirPath);
552+
} else {
553+
NSString *batchDirName = [batchDirPath lastPathComponent];
554+
NSDictionary<NSString *, id> *components = [self batchComponentsFromFilename:batchDirName];
555+
NSNumber *target = components[kGDTCORBatchComponentsTargetKey];
556+
NSString *destinationPath = [[GDTCORFlatFileStorage eventDataStoragePath]
557+
stringByAppendingPathComponent:target.stringValue];
558+
559+
// `- [NSFileManager moveItemAtPath:toPath:error:]` method fails if an item by the
560+
// destination path already exists (which usually is the case for the current method). Move
561+
// the events one by one instead.
562+
if ([self moveContentsOfDirectoryAtPath:batchDirPath to:destinationPath error:&error]) {
563+
GDTCORLogDebug(@"Batched events at path: %@ moved back to the storage: %@", batchDirPath,
564+
destinationPath);
565565
} else {
566-
NSString *batchDirName = [batchDirPath lastPathComponent];
567-
NSDictionary<NSString *, id> *components = [self batchComponentsFromFilename:batchDirName];
568-
NSNumber *target = components[kGDTCORBatchComponentsTargetKey];
569-
NSString *destinationPath = [[GDTCORFlatFileStorage eventDataStoragePath]
570-
stringByAppendingPathComponent:target.stringValue];
571-
572-
// `- [NSFileManager moveItemAtPath:toPath:error:]` method fails if an item by the
573-
// destination path already exists (which usually is the case for the current method). Move
574-
// the events one by one instead.
575-
if ([self moveContentsOfDirectoryAtPath:batchDirPath to:destinationPath error:&error]) {
576-
GDTCORLogDebug(@"Batched events at path: %@ moved back to the storage: %@", batchDirPath,
577-
destinationPath);
578-
} else {
579-
GDTCORLogDebug(@"Error encountered whilst moving events back: %@", error);
580-
}
581-
582-
// Even if not all events where moved back to the storage, there is not much can be done at
583-
// this point, so cleanup batch directory now to avoid clattering.
584-
removeBatchDir(batchDirPath);
566+
GDTCORLogDebug(@"Error encountered whilst moving events back: %@", error);
585567
}
568+
569+
// Even if not all events where moved back to the storage, there is not much can be done at
570+
// this point, so cleanup batch directory now to avoid clattering.
571+
removeBatchDir(batchDirPath);
586572
}
587573
}
588574

0 commit comments

Comments
 (0)