Skip to content

Commit

Permalink
Fix for INTENG-21262
Browse files Browse the repository at this point in the history
-> 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.
  • Loading branch information
NidhiDixit09 committed Sep 24, 2024
1 parent 800ea81 commit dc8d033
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
24 changes: 24 additions & 0 deletions Branch-TestBed/Branch-SDK-Tests/BNCServerRequestQueueTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ - (id)unarchiveObjectFromData:(NSData *)data;
// returns data in the legacy format
- (NSData *)oldArchiveQueue:(NSArray<BNCServerRequest *> *)queue;

+ (NSURL * _Nonnull) URLForQueueFile;

@end

@interface BNCServerRequestQueueTests : XCTestCase
Expand Down Expand Up @@ -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
6 changes: 5 additions & 1 deletion Sources/BranchSDK/BNCServerRequestQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -287,14 +289,16 @@ - (void)retrieve {
}
}
self.queue = decodedQueue;
// Requests are loaded into queue now. Delete queue file stored on disk.
[self removeSaveFile];
}
}
}

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

Expand Down

0 comments on commit dc8d033

Please sign in to comment.