Skip to content

Commit e0573f5

Browse files
committed
[GDTCORFlatFileStoragecheckForExpirations] - autoreleasepool in cycles
1 parent b936110 commit e0573f5

File tree

1 file changed

+53
-39
lines changed

1 file changed

+53
-39
lines changed

GoogleDataTransport/GDTCORLibrary/GDTCORFlatFileStorage.m

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -407,33 +407,45 @@ - (void)checkForExpirations {
407407
NSArray<NSString *> *batchDataPaths = [fileManager contentsOfDirectoryAtPath:batchDataPath
408408
error:nil];
409409
for (NSString *path in batchDataPaths) {
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) {
410+
@autoreleasepool {
411+
NSString *fileName = [path lastPathComponent];
412+
NSDictionary<NSString *, id> *batchComponents = [self batchComponentsFromFilename:fileName];
413+
NSDate *expirationDate = batchComponents[kGDTCORBatchComponentsExpirationKey];
415414
NSNumber *batchID = batchComponents[kGDTCORBatchComponentsBatchIDKey];
416-
// Move all events from the expired batch back to the main storage.
417-
[self syncThreadUnsafeRemoveBatchWithID:batchID deleteEvents:NO];
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+
}
418420
}
419421
}
420422

421423
// Find expired events and remove them from the storage.
422424
NSString *eventDataPath = [GDTCORFlatFileStorage eventDataStoragePath];
423425
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:eventDataPath];
424426
NSString *path;
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);
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+
}
437449
}
438450
}
439451
}
@@ -547,28 +559,30 @@ - (void)syncThreadUnsafeRemoveBatchWithID:(nonnull NSNumber *)batchID
547559
};
548560

549561
for (NSString *batchDirPath in batchDirPaths) {
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);
562+
@autoreleasepool {
563+
if (deleteEvents) {
564+
removeBatchDir(batchDirPath);
565565
} else {
566-
GDTCORLogDebug(@"Error encountered whilst moving events back: %@", error);
567-
}
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+
}
568581

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);
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);
585+
}
572586
}
573587
}
574588

0 commit comments

Comments
 (0)