Store persistent data or file in UserDefaults, Keychain, File System, Memory Cache.
Supports persistence of all
Codable
types.
These types include Standard library types like String, Int; and Foundation types like Date, Data, URL etc.
That values can store as .html, .json, .txt, .jpg, .png, .mov and .mp4 types in the file system.
- iOS 11.0+
- Swift 5.0+
CocoaPods
Add the following line to your Podfile
pod 'OYStore'
Swift Package Manager
Add OYStore as a dependency to your Package.swift
and specify OYStore as a target dependency
import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
targets: [],
dependencies: [
.package(url: "https://github.com/osmanyildirim/OYStore", .upToNextMinor(from: "1.0")),
],
targets: [
.target(
name: "YOUR_PROJECT_NAME",
dependencies: ["OYStore"])
]
)
- Save value
try OYStore.save(to: .userDefaults(key: "ud_string_value_key"), value: "ud_string_value")
try OYStore.save(to: .userDefaults(key: "ud_codable_value_key"), value: User())
try OYStore.save(to: .userDefaults(key: "ud_uiimage_value_key"), value: UIImage(named: "Sample")?.pngData())
- Fetch value
let value: User = try OYStore.value(of: .userDefaults(key: "ud_codable_value_key"))
- Fetch value with default
OYStore.value(of: .userDefaults(key: "ud_string_value_key"), default: "ud_default_value")
- Remove value
try OYStore.remove(of: .userDefaults(key: "ud_uiimage_value_key"))
- Remove all value
try OYStore.removeAll(of: .userDefaults)
- Save value
try OYStore.save(to: .keychain(key: "kc_string_value_key"), value: "kc_string_value")
try OYStore.save(to: .keychain(key: "kc_codable_value_key"), value: User())
try OYStore.save(to: .keychain(key: "kc_uiimage_value_key"), value: UIImage(named: "Sample")?.pngData())
- Fetch value
let value: User = try OYStore.value(of: .keychain(key: "kc_codable_value_key"))
- Fetch value with default
OYStore.value(of: .keychain(key: "kc_string_value_key"), default: "kc_default_value")
- Remove value
try OYStore.remove(of: .keychain(key: "kc_uiimage_value_key"))
- Remove all value
try OYStore.removeAll(of: .keychain)
Backed by NSCache for store values on Memory.
- Save value
try OYStore.save(to: .memoryCache(key: "mc_string_value_key"), value: "mc_string_value")
try OYStore.save(to: .memoryCache(key: "mc_codable_value_key"), value: User())
try OYStore.save(to: .memoryCache(key: "mc_uiimage_value_key"), value: UIImage(named: "Sample")?.pngData())
- Fetch value
let value: User = try OYStore.value(of: .memoryCache(key: "mc_codable_value_key"))
- Fetch value with default
OYStore.value(of: .memoryCache(key: "mc_string_value_key"), default: "mc_default_value")
- Remove value
try OYStore.remove(of: .memoryCache(key: "mc_uiimage_value_key"))
- Remove all value
try OYStore.removeAll(of: .memoryCache)
Cache for
URLRequest
- Cache response
let session = URLSession.shared
let request = URLRequest(url: URL(string: "API_URL")!)
session.dataTask(with: request) { data, response, error in
try? OYStore.save(to: .urlCache(urlRequest: request, data: data, urlSession: session, urlResponse: response))
}.resume()
- Fetch cached response
let request = URLRequest(url: URL(string: "API_URL")!)
let value: User = try OYStore.value(of: .urlCache(urlRequest: request))
- Remove cached response
let request = URLRequest(url: URL(string: "API_URL")!)
OYStore.remove(of: .urlCache(urlRequest: request))
- Remove all cached responses
try? OYStore.removeAll(of: .urlCache)
Library/Caches
Directory
Note that the system may delete the
Caches
directory to free up disk space, so your app must be able to re-create or download these files as needed.
- Save value
try OYStore.save(to: .diskCache(file: "dc_text_file", type: .txt), value: "dc_text_value")
try OYStore.save(to: .diskCache(file: "dc_html_file", type: .html), value: "dc_html_value")
try OYStore.save(to: .diskCache(file: "dc_image", type: .png), value: UIImage(named: "Sample")?.pngData())
... save value with Folder...
try OYStore.save(to: .diskCache(file: "dc_parent_folder/dc_image", type: .png), value: UIImage(named: "Sample")?.pngData())
- Fetch value
let value: String = try OYStore.value(of: .diskCache(file: "dc_text_file", type: .txt))
- Fetch value with default
OYStore.value(of: .diskCache(file: "dc_text_file", type: .txt), default: "dc_default_value")
- Remove value
try OYStore.remove(of: .diskCache(key: "dc_uiimage_value_key"))
- Remove all value
try OYStore.removeAll(of: .diskCache)
Library/Application Support
Directory
Store files in here that are required for your app but should never be visible to the user like your app’s database file. You can store files in here at the top level or create sub-directories. Content of the directory is persisted and included in the iCloud and iTunes backups.
- Save value
try OYStore.save(to: .applicationSupport(file: "as_text_file", type: .txt), value: "as_text_value")
try OYStore.save(to: .applicationSupport(file: "as_html_file", type: .html), value: "as_html_value")
try OYStore.save(to: .applicationSupport(file: "as_image", type: .png), value: UIImage(named: "Sample")?.pngData())
... save value with Folder...
try OYStore.save(to: .applicationSupport(file: "as_parent_folder/as_image", type: .png), value: UIImage(named: "Sample")?.pngData())
- Fetch value
let value: String = try OYStore.value(of: .applicationSupport(file: "as_text_file", type: .txt))
- Fetch value with default
OYStore.value(of: .applicationSupport(file: "as_text_file", type: .txt), default: "as_default_value")
- Remove value
try OYStore.remove(of: .applicationSupport(key: "as_uiimage_value_key"))
- Remove all value
try OYStore.removeAll(of: .applicationSupport)
Documents
Directory
Documents and other data that is user-generated and stored in the
Documents
directory can be automatically backed up by iCloud on iOS devices, if the iCloud Backup setting is turned on. The data can be recovered when user sets up a new device or resets an existing device.
- Save value
try OYStore.save(to: .documents(file: "d_text_file", type: .txt), value: "d_text_value")
try OYStore.save(to: .documents(file: "d_html_file", type: .html), value: "d_html_value")
try OYStore.save(to: .documents(file: "d_image", type: .png), value: UIImage(named: "Sample")?.pngData())
... save value with Folder...
try OYStore.save(to: .documents(file: "d_parent_folder/d_image", type: .png), value: UIImage(named: "Sample")?.pngData())
- Fetch value
let value: String = try OYStore.value(of: .documents(file: "d_text_file", type: .txt))
- Fetch value with default
OYStore.value(of: .documents(file: "d_text_file", type: .txt), default: "d_default_value")
- Remove value
try OYStore.remove(of: .documents(key: "d_uiimage_value_key"))
- Remove all value
try OYStore.removeAll(of: .documents)
tmp
Directory
Data that is used only temporarily should be stored in the
tmp
directory. Although these files are not backed up to iCloud, remember to delete those files when you are done with them so that they do not continue to consume space on the user’s device.
- Save value
try OYStore.save(to: .temporary(file: "tmp_text_file", type: .txt), value: "tmp_text_value")
try OYStore.save(to: .temporary(file: "tmp_html_file", type: .html), value: "tmp_html_value")
try OYStore.save(to: .temporary(file: "tmp_image", type: .png), value: UIImage(named: "Sample")?.pngData())
... save value with Folder...
try OYStore.save(to: .temporary(file: "d_parent_folder/tmp_image", type: .png), value: UIImage(named: "Sample")?.pngData())
- Fetch value
let value: String = try OYStore.value(of: .temporary(file: "tmp_text_file", type: .txt))
- Fetch value with default
OYStore.value(of: .temporary(file: "tmp_text_file", type: .txt), default: "tmp_default_value")
- Remove value
try OYStore.remove(of: .temporary(key: "tmp_uiimage_value_key"))
- Remove all value
try OYStore.removeAll(of: .temporary)
OYStore is released under an MIT license. See LICENSE for details.