Permalink
Browse files

🌲 Update

  • Loading branch information...
muukii committed Apr 4, 2017
1 parent bb43401 commit aa52893546a78f47738f7112045904b86654c0c5
Showing with 24 additions and 1 deletion.
  1. +9 −1 OneStore/OneStore.swift
  2. +10 −0 OneStore/Stack.swift
  3. +5 −0 OneStoreTests/NonOptionalOneStoreSpec.swift
@@ -24,6 +24,8 @@ import Foundation
open class OneStore<T: OneStoreValueProtocol>: OneStoreType {
public typealias Value = T
open let storeKey: String
open let stack: Stack
@@ -53,7 +55,13 @@ open class OneStore<T: OneStoreValueProtocol>: OneStoreType {
get {
if let initialValue = initialValue, T.getOneStoreValue(stack.userDefaults, key: rawStoreKey) == nil {
self.value = initialValue
if Thread.isMainThread == false && writeOnMainThread {
DispatchQueue.main.sync {
initialValue.setOneStoreValue(stack.userDefaults, key: rawStoreKey)
}
} else {
initialValue.setOneStoreValue(stack.userDefaults, key: rawStoreKey)
}
}
return T.getOneStoreValue(stack.userDefaults, key: rawStoreKey)
@@ -27,6 +27,8 @@ public final class Stack {
public let userDefaults: UserDefaults
public let domain: String
private(set) public var managedKeys: [String] = []
public init(userDefaults: UserDefaults = UserDefaults.standard, domain: String) {
assert(domain.isEmpty == false, "Domain must be not empty")
@@ -41,6 +43,10 @@ public final class Stack {
userDefaults.synchronize()
}
public func removeManagedKeyObjects() {
managedKeys.forEach(remove(key: ))
}
internal func remove(key: String) {
userDefaults.removeObject(forKey: key)
synchronize()
@@ -50,4 +56,8 @@ public final class Stack {
internal func exists(key: String) -> Bool {
return userDefaults.dictionaryRepresentation()[key] != nil
}
internal func addManagedKey(_ key: String) {
managedKeys.append(key)
}
}
@@ -17,6 +17,11 @@ class NonOptionalOneStoreSpec: XCTestCase {
func testBool() {
let store = NonOptionalOneStore<Bool>(stack: stack, key: "me.muukii.bool2", initialValue: true)
store.value = false
XCTAssertEqual(store.value, false)
store.value = true
XCTAssertEqual(store.value, true)
store.value = false
XCTAssertEqual(store.value, false)
}
}

0 comments on commit aa52893

Please sign in to comment.