Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cellular/localstorage-swift
iOS, tvOS, watchOS storage for persisting small data sets
https://github.com/cellular/localstorage-swift
cocoapods codable ios storage swift tvos watchos xcode
Last synced: about 19 hours ago
JSON representation
iOS, tvOS, watchOS storage for persisting small data sets
- Host: GitHub
- URL: https://github.com/cellular/localstorage-swift
- Owner: cellular
- License: mit
- Created: 2018-08-29T12:55:14.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-03-30T12:11:15.000Z (over 4 years ago)
- Last Synced: 2024-10-11T17:46:39.718Z (26 days ago)
- Topics: cocoapods, codable, ios, storage, swift, tvos, watchos, xcode
- Language: Swift
- Homepage:
- Size: 13.2 MB
- Stars: 5
- Watchers: 9
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
LocalStorage
## Example
To run the example project, clone the repo and open Example/Example.xcodproj with Xcode 11+.
#### 1. Choose a model to persist
``` swift
/// Make model Codable to use default Decoder and Encoder
struct User: Codable {let name: String
init(name: String) {
self.name = name
}
}```
#### 2. Create an LocalStorage.Manager instance and register storages```swift
// Identifies existing Storages. Used internally for easy storage access through LocalStorage.Manager.
enum Identifier: String {
case user// Path to store data to.
var path: String {
switch self {
case .user: return "example.storage.user"
}
}
}// Storage persisting data to user storage to key 'path'
let defaultsStorage = UserDefaultsStorage(userDefaults: UserDefaults.standard, path: Identifier.user.path)
// Wrap defaultsStorage with CachedStorage for faster access.
let userStorage = CachedStorage(storage: defaultsStorage)// Dictionary containing all storages handled by Manager instance
let storages = [Identifier.user.rawValue: userStorage]// DispatchLock to allow multiple reads but single write operations on storages. It will perform all
// operations on a concurrent DispatchQueue. It is also possible to simply use a NSLock, which may be
// easier to handle due to reduced thread states. On the downside NSLock has a lower performance on read
// operations than a DispatchLock.
let lock = DispatchLock(queue: DispatchQueue(label: "example.queue.storage", attributes: .concurrent))// Serial queue for async handling. Enables sequential dispatching of completion blocks.
// Needed for required behaviour in example app.
// NOTE: Default Manager(storages: _, lock: _) uses a concurrent queue.
let asyncQueue = DispatchQueue(label: "async.queue")let manager = Manager(storages: storages, lock: lock, asyncQueue: asyncQueue)
```#### 3. Save user
```swift
let user = User(name: "Bernd")
let encoder = FoundationEncoder(encoder: JSONEncoder())
// Prefer async access over sync access
// Sync access := manager.append(_, using: _) -> Result
manager.async.append(user, to: Identifier.user.rawValue, using: encoder) { result in
switch result {
case .success(let savedUser):
print("\(savedUser.name)")
case .failure(let error):
print(error)
}
}
```#### 4. Load stored user list
```swift
let decoder = FoundationDecoder(decoder: JSONDecoder())
// Prefer async access over sync access
// Sync access := manager.all(from: _, using: _) -> Result<[T], Error>
manager.async.all(from: Identifier.user.rawValue, using: decoder) { result in
switch result {
case .success(let user):
user.forEach { print("\($0.name)") }
case .failure(let error):
print(error)
}
}
```
## Requirements
- Swift 5.0+
- iOS 11.0+
- tvOS 11.0+
- watchOS 5.0+## Installation
### [Swift Package Manager](https://swift.org/package-manager/)
```swift
dependencies: [
.package(url: "https://github.com/cellular/cellular-swift.git", from: "6.0.0")
]
```### [CocoaPods](http://cocoapods.org)
```ruby
pod "CellularLocalStorage"
```## License
CellularLocalStorage is released under the MIT license. [See LICENSE](https://github.com/cellular/localstorage-swift/blob/master/LICENSE) for details.