Skip to content

Commit ebe8934

Browse files
Merge pull request #433 from Iterable/feature/mob-2177-delete-all-tasks
[MOB-2177] - Delete all tasks on logout.
2 parents 4d09ac4 + 7e5ad4d commit ebe8934

File tree

7 files changed

+42
-0
lines changed

7 files changed

+42
-0
lines changed

swift-sdk/Internal/IterableAPIInternal.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,8 @@ final class IterableAPIInternal: NSObject, PushTrackerProtocol, AuthProvider {
444444
storeIdentifierData()
445445

446446
_ = inAppManager.reset()
447+
448+
try? requestHandler.handleLogout()
447449
}
448450

449451
private func loginNewUser() {

swift-sdk/Internal/IterableTaskScheduler.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class IterableTaskScheduler {
3838
return Result.success(taskId)
3939
}
4040

41+
func deleteAllTasks() throws {
42+
ITBInfo()
43+
try persistenceContextProvider.mainQueueContext().deleteAllTasks()
44+
}
45+
4146
private let persistenceContextProvider: IterablePersistenceContextProvider
4247
private let notificationCenter: NotificationCenterProtocol
4348
private let dateProvider: DateProviderProtocol

swift-sdk/Internal/LegacyRequestHandler.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ struct LegacyRequestHandler: RequestHandlerProtocol {
245245
forResult: apiClient.track(inAppClick: messageId, clickedUrl: clickedUrl))
246246
}
247247

248+
func handleLogout() {
249+
}
250+
248251
private let apiClient: ApiClientProtocol
249252
private weak var authManager: IterableInternalAuthManagerProtocol?
250253

swift-sdk/Internal/OfflineRequestProcessor.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ struct OfflineRequestProcessor: RequestProcessorProtocol {
245245
identifier: #function)
246246
}
247247

248+
func deleteAllTasks() throws {
249+
ITBInfo()
250+
try taskScheduler.deleteAllTasks()
251+
}
252+
248253
private let apiKey: String
249254
private weak var authProvider: AuthProvider?
250255
private weak var authManager: IterableInternalAuthManagerProtocol?

swift-sdk/Internal/RequestHandler.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ class RequestHandler: RequestHandlerProtocol {
252252
onFailure: onFailure)
253253
}
254254

255+
func handleLogout() throws {
256+
if strategy.chooseOfflineProcessor {
257+
try offlineProcessor?.deleteAllTasks()
258+
}
259+
}
260+
255261
private let onlineCreator: () -> OnlineRequestProcessor
256262
private let offlineCreator: () -> OfflineRequestProcessor?
257263

swift-sdk/Internal/RequestHandlerProtocol.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ protocol RequestHandlerProtocol {
108108
source: InAppDeleteSource?,
109109
onSuccess: OnSuccessHandler?,
110110
onFailure: OnFailureHandler?) -> Future<SendRequestValue, SendRequestError>
111+
112+
func handleLogout() throws
111113

112114
// MARK: DEPRECATED
113115

tests/offline-events-tests/RequestHandlerTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,25 @@ class RequestHandlerTests: XCTestCase {
592592
wait(for: [expectations.successExpectation, expectations.failureExpectation], timeout: 15.0)
593593
}
594594

595+
func testDeleteAllTasksOnLogout() throws {
596+
let config = IterableConfig()
597+
config.enableOfflineMode = true
598+
let internalApi = IterableAPIInternal.initializeForTesting(config: config)
599+
internalApi.email = "[email protected]"
600+
601+
let taskId = IterableUtil.generateUUID()
602+
try persistenceContextProvider.mainQueueContext().create(task: IterableTask(id: taskId,
603+
type: .apiCall,
604+
scheduledAt: Date(),
605+
data: nil,
606+
requestedAt: Date()))
607+
try persistenceContextProvider.mainQueueContext().save()
608+
609+
internalApi.logoutUser()
610+
611+
XCTAssertEqual(try persistenceContextProvider.mainQueueContext().findAllTasks().count, 0)
612+
}
613+
595614
private func handleRequestWithSuccessAndFailure(requestGenerator: (RequestHandlerProtocol) -> Future<SendRequestValue, SendRequestError>,
596615
path: String,
597616
bodyDict: [AnyHashable: Any]) throws {

0 commit comments

Comments
 (0)