Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions Sources/Customization/DefaultEventDispatcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -54,7 +54,7 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher {
let reachability = NetworkReachability(maxContiguousFails: 1)

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) {
Expand All @@ -63,9 +63,14 @@ open class DefaultEventDispatcher: BackgroundingCallbacks, OPTEventDispatcher {
self.maxQueueSize = maxQueueSize >= 100 ? maxQueueSize : DefaultValues.maxQueueSize

switch backingStore {
case .file:
self.eventQueue = DataStoreQueueStackImpl<EventForDispatch>(queueStackName: "OPTEventQueue",
dataStore: DataStoreFile<[Data]>(storeName: dataStoreName))
case .file(let directory):
if let directory {
self.eventQueue = DataStoreQueueStackImpl<EventForDispatch>(queueStackName: "OPTEventQueue",
dataStore: DataStoreFile<[Data]>(storeName: dataStoreName, directory: directory))
} else {
self.eventQueue = DataStoreQueueStackImpl<EventForDispatch>(queueStackName: "OPTEventQueue",
dataStore: DataStoreFile<[Data]>(storeName: dataStoreName))
}
case .memory:
self.eventQueue = DataStoreQueueStackImpl<EventForDispatch>(queueStackName: "OPTEventQueue",
dataStore: DataStoreMemory<[Data]>(storeName: dataStoreName))
Expand Down
16 changes: 11 additions & 5 deletions Sources/Implementation/Datastore/DataStoreFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,27 @@ open class DataStoreFile<T>: 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

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)
}

}

func isArray() -> Bool {
Expand Down
24 changes: 20 additions & 4 deletions Tests/OptimizelyTests-Common/DataStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
Loading