From dc8d0334b5c843a7f7591b34b560999fc857d5de Mon Sep 17 00:00:00 2001 From: NidhiDixit09 <93544270+NidhiDixit09@users.noreply.github.com> Date: Mon, 23 Sep 2024 21:39:11 -0700 Subject: [PATCH] Fix for INTENG-21262 -> Remove archived request file from disk when those req. are added in queue for processing on app launch. -> When persist timer is fired and req queue is empty, delete cache file from disk. persistImmediately func was earlier returning without updating cache if req queue was empty. -> Added check fileExistsAtPath before removing it in func removeSaveFile. --- .../BNCServerRequestQueueTests.m | 24 +++++++++++++++++++ Sources/BranchSDK/BNCServerRequestQueue.m | 6 ++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Branch-TestBed/Branch-SDK-Tests/BNCServerRequestQueueTests.m b/Branch-TestBed/Branch-SDK-Tests/BNCServerRequestQueueTests.m index 4d8664b82..ff1b5e256 100644 --- a/Branch-TestBed/Branch-SDK-Tests/BNCServerRequestQueueTests.m +++ b/Branch-TestBed/Branch-SDK-Tests/BNCServerRequestQueueTests.m @@ -25,6 +25,8 @@ - (id)unarchiveObjectFromData:(NSData *)data; // returns data in the legacy format - (NSData *)oldArchiveQueue:(NSArray *)queue; ++ (NSURL * _Nonnull) URLForQueueFile; + @end @interface BNCServerRequestQueueTests : XCTestCase @@ -157,4 +159,26 @@ - (void)testOldArchiveArrayOfInvalidObjects { XCTAssert(unarchived.count == 2); } +- (void)testMultipleRequests { + BranchEventRequest *eventObject = [BranchEventRequest new]; + BranchOpenRequest *openObject = [BranchOpenRequest new]; + + [_queue enqueue: eventObject]; + [_queue enqueue: openObject]; + [_queue persistImmediately]; + + NSMutableArray *decodedQueue = nil; + NSData *data = [NSData dataWithContentsOfURL:[BNCServerRequestQueue URLForQueueFile] options:0 error:nil]; + if (data) { + decodedQueue = [_queue unarchiveQueueFromData:data]; + } + XCTAssert([decodedQueue count] == 2); + + [_queue remove:eventObject]; + [_queue remove:openObject]; + [_queue persistImmediately]; + // Request Queue is empty. So there should not be any queue file on disk. + XCTAssert([NSFileManager.defaultManager fileExistsAtPath:[[BNCServerRequestQueue URLForQueueFile] path]] == NO); +} + @end diff --git a/Sources/BranchSDK/BNCServerRequestQueue.m b/Sources/BranchSDK/BNCServerRequestQueue.m index 2fefbc419..259325b6b 100755 --- a/Sources/BranchSDK/BNCServerRequestQueue.m +++ b/Sources/BranchSDK/BNCServerRequestQueue.m @@ -217,6 +217,8 @@ - (void)cancelTimer { - (void)persistImmediately { @synchronized (self) { if (!self.queue || self.queue.count == 0) { + //No more requests. Delete cached queue file. + [self removeSaveFile]; return; } NSArray *queueCopy = [self.queue copy]; @@ -287,6 +289,8 @@ - (void)retrieve { } } self.queue = decodedQueue; + // Requests are loaded into queue now. Delete queue file stored on disk. + [self removeSaveFile]; } } } @@ -294,7 +298,7 @@ - (void)retrieve { // It's been reported that unarchive can fail in some situations. In that case, remove the queued requests file. - (void)removeSaveFile { NSURL *fileURL = [BNCServerRequestQueue URLForQueueFile]; - if (fileURL) { + if (fileURL && [NSFileManager.defaultManager fileExistsAtPath:[fileURL path]]) { NSError *error; [NSFileManager.defaultManager removeItemAtURL:fileURL error:&error];