From 8d5254e9e1ba545c2558ea4cb6eb7abbb127cbed Mon Sep 17 00:00:00 2001 From: artesmichael Date: Wed, 3 Dec 2025 13:00:30 +0100 Subject: [PATCH 1/8] refactor: Added optional directory parameter to DataStoreFile --- .../Datastore/DataStoreFile.swift | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/Sources/Implementation/Datastore/DataStoreFile.swift b/Sources/Implementation/Datastore/DataStoreFile.swift index 2ad28c628..027786d01 100644 --- a/Sources/Implementation/Datastore/DataStoreFile.swift +++ b/Sources/Implementation/Datastore/DataStoreFile.swift @@ -30,21 +30,27 @@ open class DataStoreFile: OPTDataStore where T: Codable { return threadSafeLogger.logger } - public init(storeName: String, async: Bool = true) { - self.async = async - dataStoreName = storeName - lock = DispatchQueue(label: storeName) + public convenience init(storeName: String, async: Bool = true) { #if os(tvOS) || os(macOS) let directory = FileManager.SearchPathDirectory.cachesDirectory #else let directory = FileManager.SearchPathDirectory.documentDirectory #endif - if let url = FileManager.default.urls(for: directory, in: .userDomainMask).first { - self.url = url.appendingPathComponent(storeName, isDirectory: false) + + self.init(storeName: storeName, directory: directory, async: async) + } + + public init(storeName: String, directory: FileManager.SearchPathDirectory, async: Bool = true) { + self.async = async + + dataStoreName = storeName + lock = DispatchQueue(label: storeName) + + url = if let url = FileManager.default.urls(for: directory, in: .userDomainMask).first { + url.appendingPathComponent(storeName, isDirectory: false) } else { - self.url = URL(fileURLWithPath: storeName) + URL(fileURLWithPath: storeName) } - } func isArray() -> Bool { From b1e288ac8ff0eb95608e856894d6f0e8d06ed820 Mon Sep 17 00:00:00 2001 From: artesmichael Date: Wed, 3 Dec 2025 13:08:27 +0100 Subject: [PATCH 2/8] refactor: Added ability to inject file data store directory into DefaultEventDispatcher --- .../Customization/DefaultEventDispatcher.swift | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Sources/Customization/DefaultEventDispatcher.swift b/Sources/Customization/DefaultEventDispatcher.swift index 782af0a9a..76f0ea630 100644 --- a/Sources/Customization/DefaultEventDispatcher.swift +++ b/Sources/Customization/DefaultEventDispatcher.swift @@ -17,7 +17,7 @@ import Foundation public enum DataStoreType { - case file, memory, userDefaults + case file(directory: FileManager.SearchPathDirectory? = nil), memory, userDefaults } open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher { @@ -59,7 +59,7 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher { private var isFlushing = false public init(batchSize: Int = DefaultValues.batchSize, - backingStore: DataStoreType = .file, + backingStore: DataStoreType = .file(directory: nil), dataStoreName: String = "OPTEventQueue", timerInterval: TimeInterval = DefaultValues.timeInterval, maxQueueSize: Int = DefaultValues.maxQueueSize) { @@ -68,9 +68,14 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher { self.maxQueueSize = maxQueueSize >= 100 ? maxQueueSize : DefaultValues.maxQueueSize switch backingStore { - case .file: - self.eventQueue = DataStoreQueueStackImpl(queueStackName: "OPTEventQueue", - dataStore: DataStoreFile<[Data]>(storeName: dataStoreName)) + case .file(let directory): + if let directory { + self.eventQueue = DataStoreQueueStackImpl(queueStackName: "OPTEventQueue", + dataStore: DataStoreFile<[Data]>(storeName: dataStoreName, directory: directory)) + } else { + self.eventQueue = DataStoreQueueStackImpl(queueStackName: "OPTEventQueue", + dataStore: DataStoreFile<[Data]>(storeName: dataStoreName)) + } case .memory: self.eventQueue = DataStoreQueueStackImpl(queueStackName: "OPTEventQueue", dataStore: DataStoreMemory<[Data]>(storeName: dataStoreName)) From 106dc732474a7b2d359e9b823771df8ff83851e5 Mon Sep 17 00:00:00 2001 From: artesmichael Date: Wed, 3 Dec 2025 13:17:51 +0100 Subject: [PATCH 3/8] test: Added test for DataStoreFile custom directory --- .../DataStoreTests.swift | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/Tests/OptimizelyTests-Common/DataStoreTests.swift b/Tests/OptimizelyTests-Common/DataStoreTests.swift index 05003b7d2..ec58c44cf 100644 --- a/Tests/OptimizelyTests-Common/DataStoreTests.swift +++ b/Tests/OptimizelyTests-Common/DataStoreTests.swift @@ -17,10 +17,9 @@ import XCTest class DataStoreTests: XCTestCase { - - override func setUp() { - // Put setup code here. This method is called before the invocation of each test method in the class. - if let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first { + + private func createDirectoryIfNeeded(directory: FileManager.SearchPathDirectory) { + if let url = FileManager.default.urls(for: directory, in: .userDomainMask).first { if (!FileManager.default.fileExists(atPath: url.path)) { do { try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil) @@ -30,7 +29,11 @@ class DataStoreTests: XCTestCase { } } + } + override func setUp() { + // Put setup code here. This method is called before the invocation of each test method in the class. + createDirectoryIfNeeded(directory: .documentDirectory) } override func tearDown() { @@ -168,6 +171,19 @@ class DataStoreTests: XCTestCase { datastore.removeItem(forKey: key) } + func testFileStoreCustomDirectory() throws { + createDirectoryIfNeeded(directory: .applicationSupportDirectory) + + let datastore = DataStoreFile<[String]>(storeName: "testFileStore") + + datastore.saveItem(forKey: "testString", value: ["value"]) + let vj = datastore.getItem(forKey: "testString") as! [String] + XCTAssertEqual(vj.first, "value") + + var url = try XCTUnwrap(FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first) + .appendingPathComponent("testFileStore", isDirectory: false) + XCTAssertTrue(FileManager.default.fileExists(atPath: url.path())) + } func testUserDefaults() { From 451bc0ccc3000f828fd86a34a7a1a9f71fdc5140 Mon Sep 17 00:00:00 2001 From: artesmichael Date: Thu, 4 Dec 2025 10:58:10 +0100 Subject: [PATCH 4/8] refactor: Updated to syntax supported by Swift 5.8 and lower --- Sources/Implementation/Datastore/DataStoreFile.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Sources/Implementation/Datastore/DataStoreFile.swift b/Sources/Implementation/Datastore/DataStoreFile.swift index 027786d01..51c92e310 100644 --- a/Sources/Implementation/Datastore/DataStoreFile.swift +++ b/Sources/Implementation/Datastore/DataStoreFile.swift @@ -46,10 +46,10 @@ open class DataStoreFile: OPTDataStore where T: Codable { dataStoreName = storeName lock = DispatchQueue(label: storeName) - url = if let url = FileManager.default.urls(for: directory, in: .userDomainMask).first { - url.appendingPathComponent(storeName, isDirectory: false) + if let url = FileManager.default.urls(for: directory, in: .userDomainMask).first { + self.url = url.appendingPathComponent(storeName, isDirectory: false) } else { - URL(fileURLWithPath: storeName) + self.url = URL(fileURLWithPath: storeName) } } From 51634e2dd5bad9786f6ba6615f0aaf070bb62e37 Mon Sep 17 00:00:00 2001 From: artesmichael Date: Tue, 31 Mar 2026 11:42:49 +0200 Subject: [PATCH 5/8] Fixed build issues --- Tests/OptimizelyTests-Common/DataStoreTests.swift | 2 +- Tests/OptimizelyTests-Common/EventDispatcherTests.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/OptimizelyTests-Common/DataStoreTests.swift b/Tests/OptimizelyTests-Common/DataStoreTests.swift index ec58c44cf..808c9f3df 100644 --- a/Tests/OptimizelyTests-Common/DataStoreTests.swift +++ b/Tests/OptimizelyTests-Common/DataStoreTests.swift @@ -182,7 +182,7 @@ class DataStoreTests: XCTestCase { var url = try XCTUnwrap(FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first) .appendingPathComponent("testFileStore", isDirectory: false) - XCTAssertTrue(FileManager.default.fileExists(atPath: url.path())) + XCTAssertTrue(FileManager.default.fileExists(atPath: url.path)) } diff --git a/Tests/OptimizelyTests-Common/EventDispatcherTests.swift b/Tests/OptimizelyTests-Common/EventDispatcherTests.swift index bb146e054..4e61c07a1 100644 --- a/Tests/OptimizelyTests-Common/EventDispatcherTests.swift +++ b/Tests/OptimizelyTests-Common/EventDispatcherTests.swift @@ -89,7 +89,7 @@ class EventDispatcherTests: XCTestCase { } func testEventDispatcherFile() { - eventDispatcher = DefaultEventDispatcher( backingStore: .file) + eventDispatcher = DefaultEventDispatcher( backingStore: .file(directory: nil)) let pEventD: OPTEventDispatcher = eventDispatcher! eventDispatcher?.timerInterval = 1 let wait = {() in From 4ba267afd0a50e553adc92d09fe95607bd462ef6 Mon Sep 17 00:00:00 2001 From: artesmichael Date: Tue, 31 Mar 2026 13:44:58 +0200 Subject: [PATCH 6/8] Fixed failing test --- Tests/OptimizelyTests-Common/DataStoreTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/OptimizelyTests-Common/DataStoreTests.swift b/Tests/OptimizelyTests-Common/DataStoreTests.swift index 808c9f3df..3c36e2058 100644 --- a/Tests/OptimizelyTests-Common/DataStoreTests.swift +++ b/Tests/OptimizelyTests-Common/DataStoreTests.swift @@ -174,7 +174,7 @@ class DataStoreTests: XCTestCase { func testFileStoreCustomDirectory() throws { createDirectoryIfNeeded(directory: .applicationSupportDirectory) - let datastore = DataStoreFile<[String]>(storeName: "testFileStore") + let datastore = DataStoreFile<[String]>(storeName: "testFileStore", directory: .applicationSupportDirectory) datastore.saveItem(forKey: "testString", value: ["value"]) let vj = datastore.getItem(forKey: "testString") as! [String] From 9eceac765113d86e5b666ca9009a8e5e5d49a556 Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Fri, 16 Oct 2026 21:34:59 +0600 Subject: [PATCH 7/8] [FSSDK-12251] fix: keep custom data store directory API source-compatible Rework #612 so it adds API without breaking 6.x users: - Restore `DataStoreType.file` (no associated value) so `.file` and `==` keep compiling - Keep `DataStoreFile.init(storeName:async:)` designated so subclasses can call super.init - Expose the directory via new convenience inits on DataStoreFile and DefaultEventDispatcher (new designated inits on open classes are flagged by diagnose-api-breaking-changes) - Add a test for the dispatcher custom-directory path Co-authored-by: artesmichael Co-Authored-By: Claude Opus 5.5 --- .../DefaultEventDispatcher.swift | 34 +++++++++++++------ .../Datastore/DataStoreFile.swift | 26 +++++++------- .../EventDispatcherTests.swift | 24 ++++++++++++- 3 files changed, 60 insertions(+), 24 deletions(-) diff --git a/Sources/Customization/DefaultEventDispatcher.swift b/Sources/Customization/DefaultEventDispatcher.swift index 76f0ea630..37e1ea9ba 100644 --- a/Sources/Customization/DefaultEventDispatcher.swift +++ b/Sources/Customization/DefaultEventDispatcher.swift @@ -17,7 +17,7 @@ import Foundation public enum DataStoreType { - case file(directory: FileManager.SearchPathDirectory? = nil), memory, userDefaults + case file, memory, userDefaults } open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher { @@ -46,7 +46,7 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher { // for dispatching events let queueLock = DispatchQueue(label: "DefaultEventDispatcherQueue") // using a datastore queue with a backing file - let eventQueue: DataStoreQueueStackImpl + private(set) var eventQueue: DataStoreQueueStackImpl // timer as a atomic property. var timer = AtomicProperty() @@ -59,7 +59,7 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher { private var isFlushing = false public init(batchSize: Int = DefaultValues.batchSize, - backingStore: DataStoreType = .file(directory: nil), + backingStore: DataStoreType = .file, dataStoreName: String = "OPTEventQueue", timerInterval: TimeInterval = DefaultValues.timeInterval, maxQueueSize: Int = DefaultValues.maxQueueSize) { @@ -68,14 +68,9 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher { self.maxQueueSize = maxQueueSize >= 100 ? maxQueueSize : DefaultValues.maxQueueSize switch backingStore { - case .file(let directory): - if let directory { - self.eventQueue = DataStoreQueueStackImpl(queueStackName: "OPTEventQueue", - dataStore: DataStoreFile<[Data]>(storeName: dataStoreName, directory: directory)) - } else { - self.eventQueue = DataStoreQueueStackImpl(queueStackName: "OPTEventQueue", - dataStore: DataStoreFile<[Data]>(storeName: dataStoreName)) - } + case .file: + self.eventQueue = DataStoreQueueStackImpl(queueStackName: "OPTEventQueue", + dataStore: DataStoreFile<[Data]>(storeName: dataStoreName)) case .memory: self.eventQueue = DataStoreQueueStackImpl(queueStackName: "OPTEventQueue", dataStore: DataStoreMemory<[Data]>(storeName: dataStoreName)) @@ -91,6 +86,23 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher { subscribe() } + + /// Creates a dispatcher whose event queue is a file stored in `dataStoreDirectory` + /// (e.g. `.applicationSupportDirectory` to keep it out of the Files app). + // convenience on purpose: any new designated init on an open class is an API break + public convenience init(batchSize: Int = DefaultValues.batchSize, + dataStoreName: String = "OPTEventQueue", + dataStoreDirectory: FileManager.SearchPathDirectory, + timerInterval: TimeInterval = DefaultValues.timeInterval, + maxQueueSize: Int = DefaultValues.maxQueueSize) { + self.init(batchSize: batchSize, + backingStore: .file, + dataStoreName: dataStoreName, + timerInterval: timerInterval, + maxQueueSize: maxQueueSize) + self.eventQueue = DataStoreQueueStackImpl(queueStackName: "OPTEventQueue", + dataStore: DataStoreFile<[Data]>(storeName: dataStoreName, directory: dataStoreDirectory)) + } deinit { stopTimer() diff --git a/Sources/Implementation/Datastore/DataStoreFile.swift b/Sources/Implementation/Datastore/DataStoreFile.swift index 51c92e310..07ab0b47a 100644 --- a/Sources/Implementation/Datastore/DataStoreFile.swift +++ b/Sources/Implementation/Datastore/DataStoreFile.swift @@ -22,7 +22,7 @@ open class DataStoreFile: OPTDataStore where T: Codable { let dataStoreName: String let lock: DispatchQueue let async: Bool - public let url: URL + public private(set) var url: URL // thread-safe lazy logger load (after HandlerRegisterService ready) private let threadSafeLogger = ThreadSafeLogger() @@ -30,27 +30,29 @@ open class DataStoreFile: OPTDataStore where T: Codable { return threadSafeLogger.logger } - public convenience init(storeName: String, async: Bool = true) { + public init(storeName: String, async: Bool = true) { + self.async = async + dataStoreName = storeName + lock = DispatchQueue(label: storeName) #if os(tvOS) || os(macOS) let directory = FileManager.SearchPathDirectory.cachesDirectory #else let directory = FileManager.SearchPathDirectory.documentDirectory #endif - - self.init(storeName: storeName, directory: directory, async: async) - } - - public init(storeName: String, directory: FileManager.SearchPathDirectory, async: Bool = true) { - self.async = async - - dataStoreName = storeName - lock = DispatchQueue(label: storeName) - if let url = FileManager.default.urls(for: directory, in: .userDomainMask).first { self.url = url.appendingPathComponent(storeName, isDirectory: false) } else { self.url = URL(fileURLWithPath: storeName) } + + } + + // convenience on purpose: any new designated init on an open class is an API break + public convenience init(storeName: String, directory: FileManager.SearchPathDirectory, async: Bool = true) { + self.init(storeName: storeName, async: async) + if let url = FileManager.default.urls(for: directory, in: .userDomainMask).first { + self.url = url.appendingPathComponent(storeName, isDirectory: false) + } } func isArray() -> Bool { diff --git a/Tests/OptimizelyTests-Common/EventDispatcherTests.swift b/Tests/OptimizelyTests-Common/EventDispatcherTests.swift index 4e61c07a1..4cea9b9c1 100644 --- a/Tests/OptimizelyTests-Common/EventDispatcherTests.swift +++ b/Tests/OptimizelyTests-Common/EventDispatcherTests.swift @@ -89,7 +89,7 @@ class EventDispatcherTests: XCTestCase { } func testEventDispatcherFile() { - eventDispatcher = DefaultEventDispatcher( backingStore: .file(directory: nil)) + eventDispatcher = DefaultEventDispatcher( backingStore: .file) let pEventD: OPTEventDispatcher = eventDispatcher! eventDispatcher?.timerInterval = 1 let wait = {() in @@ -113,6 +113,28 @@ class EventDispatcherTests: XCTestCase { // Use XCTAssert and related functions to verify your tests produce the correct results. } + func testEventDispatcherFileCustomDirectory() throws { + let dirUrl = try XCTUnwrap(FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first) + try FileManager.default.createDirectory(at: dirUrl, withIntermediateDirectories: true) + let storeName = "OPTEventQueueCustomDirectory" + let fileUrl = dirUrl.appendingPathComponent(storeName, isDirectory: false) + try? FileManager.default.removeItem(at: fileUrl) + defer { try? FileManager.default.removeItem(at: fileUrl) } + + let dispatcher = DefaultEventDispatcher(dataStoreName: storeName, + dataStoreDirectory: .applicationSupportDirectory, + timerInterval: 0) + let dataStore = try XCTUnwrap(dispatcher.eventQueue.dataStore as? DataStoreFile<[Data]>) + XCTAssertEqual(dataStore.url, fileUrl) + + dispatcher.eventQueue.save(item: EventForDispatch(body: Data())) + dispatcher.eventQueue.lock.sync {} + dataStore.lock.sync {} + + XCTAssertEqual(dispatcher.eventQueue.count, 1) + XCTAssertTrue(FileManager.default.fileExists(atPath: fileUrl.path)) + } + func testEventDispatcherUserDefaults() { eventDispatcher = DefaultEventDispatcher( backingStore: .userDefaults) let pEventD: OPTEventDispatcher = eventDispatcher! From 6999d38faf664ceed28e0e8a7808309c61816f56 Mon Sep 17 00:00:00 2001 From: Muzahidul Islam Date: Mon, 19 Oct 2026 19:17:50 +0600 Subject: [PATCH 8/8] [FSSDK-12251] fix: create custom data store directory if missing Application Support isn't guaranteed to exist on iOS, and Data.write doesn't create parent directories, so writes failed and saveItem swallowed the error, silently dropping queued events. Resolve the directory with url(for:in:appropriateFor:create: true). If creation fails, the store keeps its default location. Tests no longer pre-create the directory; add a test against a directory that doesn't exist yet. Co-Authored-By: Claude Opus 5.5 --- .../Implementation/Datastore/DataStoreFile.swift | 3 ++- Tests/OptimizelyTests-Common/DataStoreTests.swift | 15 +++++++++++++-- .../EventDispatcherTests.swift | 1 - 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Sources/Implementation/Datastore/DataStoreFile.swift b/Sources/Implementation/Datastore/DataStoreFile.swift index 07ab0b47a..6d8267da3 100644 --- a/Sources/Implementation/Datastore/DataStoreFile.swift +++ b/Sources/Implementation/Datastore/DataStoreFile.swift @@ -50,7 +50,8 @@ open class DataStoreFile: OPTDataStore where T: Codable { // convenience on purpose: any new designated init on an open class is an API break public convenience init(storeName: String, directory: FileManager.SearchPathDirectory, async: Bool = true) { self.init(storeName: storeName, async: async) - if let url = FileManager.default.urls(for: directory, in: .userDomainMask).first { + // create: true — unlike Documents, Application Support isn't guaranteed to exist on iOS + if let url = try? FileManager.default.url(for: directory, in: .userDomainMask, appropriateFor: nil, create: true) { self.url = url.appendingPathComponent(storeName, isDirectory: false) } } diff --git a/Tests/OptimizelyTests-Common/DataStoreTests.swift b/Tests/OptimizelyTests-Common/DataStoreTests.swift index 3c36e2058..539d9a122 100644 --- a/Tests/OptimizelyTests-Common/DataStoreTests.swift +++ b/Tests/OptimizelyTests-Common/DataStoreTests.swift @@ -172,8 +172,6 @@ class DataStoreTests: XCTestCase { } func testFileStoreCustomDirectory() throws { - createDirectoryIfNeeded(directory: .applicationSupportDirectory) - let datastore = DataStoreFile<[String]>(storeName: "testFileStore", directory: .applicationSupportDirectory) datastore.saveItem(forKey: "testString", value: ["value"]) @@ -185,6 +183,19 @@ class DataStoreTests: XCTestCase { XCTAssertTrue(FileManager.default.fileExists(atPath: url.path)) } + func testFileStoreCreatesMissingDirectory() throws { + // like Application Support on a fresh iOS install, this directory doesn't exist yet + let dirUrl = try XCTUnwrap(FileManager.default.urls(for: .autosavedInformationDirectory, in: .userDomainMask).first) + try XCTSkipIf(FileManager.default.fileExists(atPath: dirUrl.path), "directory already exists") + defer { try? FileManager.default.removeItem(at: dirUrl) } + + let datastore = DataStoreFile<[String]>(storeName: "testFileStore", directory: .autosavedInformationDirectory) + datastore.saveItem(forKey: "testString", value: ["value"]) + datastore.lock.sync {} + + XCTAssertTrue(FileManager.default.fileExists(atPath: dirUrl.appendingPathComponent("testFileStore").path)) + } + func testUserDefaults() { // simple user defaults test diff --git a/Tests/OptimizelyTests-Common/EventDispatcherTests.swift b/Tests/OptimizelyTests-Common/EventDispatcherTests.swift index 4cea9b9c1..64829e7f2 100644 --- a/Tests/OptimizelyTests-Common/EventDispatcherTests.swift +++ b/Tests/OptimizelyTests-Common/EventDispatcherTests.swift @@ -115,7 +115,6 @@ class EventDispatcherTests: XCTestCase { func testEventDispatcherFileCustomDirectory() throws { let dirUrl = try XCTUnwrap(FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first) - try FileManager.default.createDirectory(at: dirUrl, withIntermediateDirectories: true) let storeName = "OPTEventQueueCustomDirectory" let fileUrl = dirUrl.appendingPathComponent(storeName, isDirectory: false) try? FileManager.default.removeItem(at: fileUrl)