Tips for you
Combine
You can get values from Lobster with Combine's stream.
Here is a sampl viewmodel class.
import Lobster
import Combine
extension ConfigKeys {
static let title = ConfigKey<String>("title")
}
final class ViewModel: ObservableObject {
@Published var title: String
private var cancellables: Set<AnyCancellable> = []
init() {
title = Lobster.shared[.titleText]
Lobster.shared.combine.fetched(.title)
.receive(on: RunLoop.main)
.assign(to: \.title, on: self)
.store(in: &cancellables)
}
}
NOTE: You need to install Lobster/Combine
before using it.
Get value with subscripting syntax.
Use subscripting syntax.
extension ConfigKeys {
static let text = ConfigKey<String>("text")
}
// Get value from config.
// If value didn't fetch from remote yet. returns default value (if exists).
let text: String = Lobster.shared[.text]
// Get value from only config.
// it is possible to crash if value didn't fetch from remote yet.
let text: String = Lobster.shared[config: .text]
// Get value from only default.
// It is possible to crash if the default value is not set yet.
let text: String = Lobster.shared[default: .text]
// [safe:], [safeConfig:], [safeDefault:] subscripting syntax.
// It is safe because they return nil if they have no value.(return type is `Optional<T>`.)
let text: String? = Lobster.shared[safe: .text]
let text: String? = Lobster.shared[safeConfig: .text]
let text: String? = Lobster.shared[safeDefault: .text]
extension ConfigKeys {
static let textOptional = ConfigKey<String?>("text_optional")
}
let text: String? = Lobster.shared[.textOptional]
let text: String? = Lobster.shared[config: .textOptional]
let text: String? = Lobster.shared[default: .textOptional]
Set Default value
You can set default values using subscripting syntax
or plist.
// Set default value using `[default:]` syntax.
Lobster.shared[default: .titleText] = "Cart Items"
Lobster.shared[default: .titleColor] = .black
// or load from `defaults.plist`
Lobster.shared.setDefaults(fromPlist: "defaults")
Set debug mode
// Enable debug mode (development only)
Lobster.shared.debugMode = true
Lobster.shared.fetchExpirationDuration = 0.0
isStaled
If you set isStaled
to true, Lobster will fetch remote value ignoring fetchExpirationDuration
.
That is, You can retrieve config values immediately when you call fetch
after You set isStales
to true.
And isStaled
will be set to false
after fetched remote value.
Lobster.shared.fetchExpirationDuration = 60 * 12
Lobster.shared.isStaled = true
// Default expire duration is 12 hours.
// But if `isStaled` set to true,
// Lobster fetch values from remote ignoring expire duration.
Lobster.shared.fetch()