Repository navigation
[FSSDK-12251] feat: allow custom directory for DataStoreFile and DefaultEventDispatcher - #668
Conversation
…ultEventDispatcher
…ible 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 <michael.artes@icloud.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Matjaz Pirnovar (Mat001)
left a comment
There was a problem hiding this comment.
API-compat strategy (convenience inits, no enum change) is well done and the zero-IO throwaway-store claim checks out. One blocker: DataStoreFile never creates the target directory, and .applicationSupportDirectory does not exist by default on iOS — the feature's headline use case silently drops events/datafile writes.
| 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) | ||
| } | ||
| } |
There was a problem hiding this comment.
The target directory is never created, and .applicationSupportDirectory — the directory this feature exists to enable — does not exist by default on iOS. Writes there fail silently and events are dropped.
FileManager.urls(for:in:) returns the URL whether or not the folder exists on disk. iOS auto-creates Documents, tmp, and Library/Caches, but not Library/Application Support — the app must create it. The old code only used .documentDirectory (always present), so this never mattered before.
Downstream, writeData uses Data.write(to:options:.atomic), which does not create intermediate directories and throws if the parent is missing. saveItem swallows that into a log line, so the failure is silent:
} catch let e {
self.logger.e(e.localizedDescription) // event dropped, count stays 0
}Failure scenario (the PR's own example):
let dispatcher = DefaultEventDispatcher(dataStoreDirectory: .applicationSupportDirectory)On a real app where Library/Application Support hasn't been created, every eventQueue.save throws and is swallowed, count stays 0, flushEvents finds nothing — analytics events are silently lost. The datafile-cache path (createDataStore → DataStoreFile(directory: .applicationSupportDirectory)) fails the same way.
Why the tests miss it: both new tests pre-create the directory before using the store (DataStoreTests.testFileStoreCustomDirectory calls createDirectoryIfNeeded(...); EventDispatcherTests.testEventDispatcherFileCustomDirectory calls FileManager.default.createDirectory(at: dirUrl, withIntermediateDirectories: true)), so they only exercise a precondition the feature never establishes itself.
Proposed fix:
// 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 dirUrl = FileManager.default.urls(for: directory, in: .userDomainMask).first {
// Application Support (unlike Documents) is not guaranteed to exist on iOS;
// create it so writes don't silently fail and drop events.
try? FileManager.default.createDirectory(at: dirUrl, withIntermediateDirectories: true)
self.url = dirUrl.appendingPathComponent(storeName, isDirectory: false)
}
}And drop the createDirectory pre-steps from the new tests (or add a variant without them) so they actually prove the store is self-sufficient.
There was a problem hiding this comment.
Good catch, thanks. Fixed in 6999d38.
- The convenience init now resolves the directory with
FileManager.default.url(for:in:appropriateFor:create: true). This is Foundation's built-in way to create a standard directory if it's missing. - One difference from your suggestion: if creating the directory fails, the store keeps its default location (Documents), so events are still saved and not dropped. With
try? createDirectoryfollowed by always settingurl, events would still be dropped in that rare case. - Tests: removed the pre-create steps from
testFileStoreCustomDirectoryandtestEventDispatcherFileCustomDirectory. I couldn't use Application Support to prove the directory gets created, because these tests have no host app. Application Support resolves to the simulator device's shared folder, which always exists and can't be deleted. So I addedtestFileStoreCreatesMissingDirectory, which uses.autosavedInformationDirectory(missing on a fresh simulator). It failed before the fix and passes with it. DataStoreTests,EventDispatcherTests,DatafileHandlerTests: 41/41 passed.diagnose-api-breaking-changes origin/master: no breaking changes.
Could you re-review?
Matjaz Pirnovar (Mat001)
left a comment
There was a problem hiding this comment.
Looks good, one comment.
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 <noreply@anthropic.com>
❌ ErrorsYour PR has failed checks. Please review the issues below and take necessary action before merging. 🚦 1 Pipeline job failed
Useful? React with 👍 / 👎 This comment will be updated automatically if new data arrives.🔗 Commit SHA: 6999d38 | Docs | View more details | Give us feedback! |
Matjaz Pirnovar (Mat001)
left a comment
There was a problem hiding this comment.
Your fix is actually better than my proposed try? createDirectory + unconditional self.url = …. Because the create: true call is inside the if let, a creation failure (nil) leaves self.url at the Documents default set by self.init(storeName:async:) — so in the rare failure case events still get persisted to Documents instead of being dropped. My version would have reassigned url even when createDirectory failed, reintroducing the silent-drop. His gracefully degrades.
b48c241
into
master
Summary
Internal continuation of #612 by Michael Artes (@asmeili). Their commits are kept as-is, with authorship preserved, and one commit on top makes the change source-compatible with 6.x.
DataStoreFile: newconvenience init(storeName:directory:async:)stores the file in anyFileManager.SearchPathDirectoryDefaultEventDispatcher: newconvenience init(batchSize:dataStoreName:dataStoreDirectory:timerInterval:maxQueueSize:)puts the event-queue file in a custom directoryDefaultDatafileHandler: no change needed. OverridecreateDataStore(sdkKey:)and returnDataStoreFile<Data>(storeName: sdkKey, directory: .applicationSupportDirectory)Why:
DataStoreFiledefaults to.documentDirectoryon iOS, so the SDK's datafile cache and event-queue files show up in the user's Files app. Apps can now move them to.applicationSupportDirectory, for example:What changed vs. #612 and why
The original shape broke public API in a minor release. CI's
api_breaking_changesjob (diagnose-api-breaking-changes) blocks that:case file→case file(directory: ... = nil)backingStore: .fileno longer compiles; the enum loses synthesizedEquatable/Hashable, so== .memoryfailsDataStoreTypeunchanged; the directory is passed to a new initDataStoreFile.init(storeName:async:)madeconveniencesuper.init(storeName:)fail ("must call a designated initializer")openclassesconvenience: they call the existing designated init, then set the custom storeSupporting internal changes:
DataStoreFile.url:public let→public private(set) var. It is still read-only publicly.DefaultEventDispatcher.eventQueue: internallet→private(set) varThese let the convenience inits replace the default store. The throwaway default store costs nothing because creating it does no disk I/O.
Test plan
swift package diagnose-api-breaking-changes origin/master→No breaking changes detected in Optimizely. Both refactor: Added ability to inject custom directory for DataStoreFile #612's shape and an internal-designated-init variant failed this check.xcodebuild test(iOS 27 sim):EventDispatcherTests,DataStoreTests,DatafileHandlerTests, 40/40 passedDataStoreTests.testFileStoreCustomDirectory(from refactor: Added ability to inject custom directory for DataStoreFile #612) coversDataStoreFilein Application SupportEventDispatcherTests.testEventDispatcherFileCustomDirectory: the dispatcher's queue file is created in Application SupporttestEventDispatcherFileis back to using.file, which checks the original call site still compilesIssues
🤖 Generated with Claude Code