From 94e6db669fe75caf5b349e3d41cf8875b33ac541 Mon Sep 17 00:00:00 2001 From: John Estropia Date: Tue, 18 Apr 2017 12:02:39 +0900 Subject: [PATCH] added a way to lazily-initialize user info data --- Sources/Setup/DataStack.swift | 30 ++++++++++++++++++- .../Transactions/BaseDataTransaction.swift | 29 ++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Sources/Setup/DataStack.swift b/Sources/Setup/DataStack.swift index cad1e2f..d6bff57 100644 --- a/Sources/Setup/DataStack.swift +++ b/Sources/Setup/DataStack.swift @@ -454,7 +454,6 @@ public final class DataStack: Equatable { ``` - Important: Do not use this method to store thread-sensitive data. - parameter userInfoKey: the key for custom data. Make sure this is a static pointer that will never be changed. - - returns: A custom data identified by `userInfoKey` */ public subscript(userInfoKey key: UnsafeRawPointer) -> Any? { @@ -478,6 +477,35 @@ public final class DataStack: Equatable { } } + /** + Allow external libraries to store custom data in the `DataStack`. App code should rarely have a need for this. + ``` + enum Static { + static var myDataKey: Void? + } + CoreStore.defaultStack[userInfoKey: &Static.myDataKey, lazyInit: { MyObject() }] = myObject + ``` + - Important: Do not use this method to store thread-sensitive data. + - parameter userInfoKey: the key for custom data. Make sure this is a static pointer that will never be changed. + - parameter lazyInit: a closure to use to lazily-initialize the data + - returns: A custom data identified by `userInfoKey` + */ + public subscript(userInfoKey key: UnsafeRawPointer, lazyInit closure: () -> Any) -> Any? { + + self.userInfoLock.lock() + defer { + + self.userInfoLock.unlock() + } + if let value = self.userInfo[key] { + + return value + } + let value = closure() + self.userInfo[key] = value + return value + } + // MARK: Equatable diff --git a/Sources/Transactions/BaseDataTransaction.swift b/Sources/Transactions/BaseDataTransaction.swift index 9406e0c..03fa157 100644 --- a/Sources/Transactions/BaseDataTransaction.swift +++ b/Sources/Transactions/BaseDataTransaction.swift @@ -474,6 +474,35 @@ public /*abstract*/ class BaseDataTransaction { } } + /** + Allow external libraries to store custom data in the transaction. App code should rarely have a need for this. + ``` + enum Static { + static var myDataKey: Void? + } + CoreStore.defaultStack[userInfoKey: &Static.myDataKey, lazyInit: { MyObject() }] = myObject + ``` + - Important: Do not use this method to store thread-sensitive data. + - parameter userInfoKey: the key for custom data. Make sure this is a static pointer that will never be changed. + - parameter lazyInit: a closure to use to lazily-initialize the data + - returns: A custom data identified by `userInfoKey` + */ + public subscript(userInfoKey key: UnsafeRawPointer, lazyInit closure: () -> Any) -> Any? { + + self.userInfoLock.lock() + defer { + + self.userInfoLock.unlock() + } + if let value = self.userInfo[key] { + + return value + } + let value = closure() + self.userInfo[key] = value + return value + } + // MARK: Internal