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];