/OYStore

Save persistent data or file in UserDefaults, Keychain, File System, Memory Cache, URLCache

Primary LanguageSwiftMIT LicenseMIT

Cocoapods SPM compatible Platforms Swift Xcode MIT

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 datas can store as .html, .json, .txt, .jpg, .png, .mov and .mp4 types in the file system.

Contents

Requirements

  • iOS 11.0+
  • Swift 5.7+

Installation

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"])
    ]
)

Usage

UserDefaults

  • Save data
try OYStore.save(to: .userDefaults(key: "ud_string_data_key"), data: "ud_string_data")
try OYStore.save(to: .userDefaults(key: "ud_codable_data_key"), data: User())
try OYStore.save(to: .userDefaults(key: "ud_uiimage_data_key"), data: UIImage(named: "Sample")?.pngData())
  • Fetch data
let data: User = try OYStore.data(of: .userDefaults(key: "ud_codable_data_key"))
  • Fetch data with default
OYStore.data(of: .userDefaults(key: "ud_string_data_key"), default: "ud_default_data")
  • Remove data
try OYStore.remove(of: .userDefaults(key: "ud_uiimage_data_key"))
  • Remove all data
try OYStore.removeAll(of: .userDefaults)
  • Is data exist?
try OYStore.isExist(at: .userDefaults(key: "ud_codable_data_key"))
  • Move data --UserDefaults doesn't support data move

Keychain

  • Save data
try OYStore.save(to: .keychain(key: "kc_string_data_key"), data: "kc_string_data")
try OYStore.save(to: .keychain(key: "kc_codable_data_key"), data: User())
try OYStore.save(to: .keychain(key: "kc_uiimage_data_key"), data: UIImage(named: "Sample")?.pngData())
  • Fetch data
let data: User = try OYStore.data(of: .keychain(key: "kc_codable_data_key"))
  • Fetch data with default
OYStore.data(of: .keychain(key: "kc_string_data_key"), default: "kc_default_data")
  • Remove data
try OYStore.remove(of: .keychain(key: "kc_uiimage_data_key"))
  • Remove all data
try OYStore.removeAll(of: .keychain)
  • Is data exist?
try OYStore.isExist(at: .keychain(key: "kc_codable_data_key"))
  • Move data --Keychain doesn't support data move

Memory Cache

Backed by NSCache for store datas on Memory.

  • Save data
try OYStore.save(to: .memoryCache(key: "mc_string_data_key"), data: "mc_string_data")
try OYStore.save(to: .memoryCache(key: "mc_codable_data_key"), data: User())
try OYStore.save(to: .memoryCache(key: "mc_uiimage_data_key"), data: UIImage(named: "Sample")?.pngData())
  • Fetch data
let data: User = try OYStore.data(of: .memoryCache(key: "mc_codable_data_key"))
  • Fetch data with default
OYStore.data(of: .memoryCache(key: "mc_string_data_key"), default: "mc_default_data")
  • Remove data
try OYStore.remove(of: .memoryCache(key: "mc_uiimage_data_key"))
  • Remove all data
try OYStore.removeAll(of: .memoryCache)
  • Is data exist?
try OYStore.isExist(at: .memoryCache(key: "mc_codable_data_key"))
  • Move data --Memory Cache doesn't support data move

URL Cache

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 data: User = try OYStore.data(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)
  • Is data exist?
try OYStore.isExist(at: .urlCache(key: "mc_codable_data_key"))
  • Move data --URL Cache doesn't support data move

Disk Cache

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 data
try OYStore.save(to: .diskCache(file: "dc_text_file", type: .txt), data: "dc_text_data")
try OYStore.save(to: .diskCache(file: "dc_html_file", type: .html), data: "dc_html_data")
try OYStore.save(to: .diskCache(file: "dc_image", type: .png), data: UIImage(named: "Sample")?.pngData())

... save data with Folder...

try OYStore.save(to: .diskCache(file: "dc_parent_folder/dc_image", type: .png), data: UIImage(named: "Sample")?.pngData())
  • Fetch data
let data: String = try OYStore.data(of: .diskCache(file: "dc_text_file", type: .txt))
  • Fetch data with default
OYStore.data(of: .diskCache(file: "dc_text_file", type: .txt), default: "dc_default_data")
  • Remove data
try OYStore.remove(of: .diskCache(key: "dc_uiimage_data_key"))
  • Remove all data
try OYStore.removeAll(of: .diskCache)
  • Is data exist?
try OYStore.isExist(at: .diskCache(key: "mc_codable_data_key"))
  • Move data
try OYStore.move(from: .diskCache(file: "ds_text_file", type: .txt), to: .documents(file: "d_parent_folder/as_text_file", type: .txt))

Application Support

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 data
try OYStore.save(to: .applicationSupport(file: "as_text_file", type: .txt), data: "as_text_data")
try OYStore.save(to: .applicationSupport(file: "as_html_file", type: .html), data: "as_html_data")
try OYStore.save(to: .applicationSupport(file: "as_image", type: .png), data: UIImage(named: "Sample")?.pngData())

... save data with Folder...

try OYStore.save(to: .applicationSupport(file: "as_parent_folder/as_image", type: .png), data: UIImage(named: "Sample")?.pngData())
  • Fetch data
let data: String = try OYStore.data(of: .applicationSupport(file: "as_text_file", type: .txt))
  • Fetch data with default
OYStore.data(of: .applicationSupport(file: "as_text_file", type: .txt), default: "as_default_data")
  • Remove data
try OYStore.remove(of: .applicationSupport(key: "as_uiimage_data_key"))
  • Remove all data
try OYStore.removeAll(of: .applicationSupport)
  • Is data exist?
try OYStore.isExist(at: .applicationSupport(file: "as_text_file", type: .txt))
  • Move data
try OYStore.move(from: .applicationSupport(file: "as_text_file", type: .txt), to: .documents(file: "d_parent_folder/d_text_file", type: .txt))

Documents

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 data
try OYStore.save(to: .documents(file: "d_text_file", type: .txt), data: "d_text_data")
try OYStore.save(to: .documents(file: "d_html_file", type: .html), data: "d_html_data")
try OYStore.save(to: .documents(file: "d_image", type: .png), data: UIImage(named: "Sample")?.pngData())

... save data with Folder...

try OYStore.save(to: .documents(file: "d_parent_folder/d_image", type: .png), data: UIImage(named: "Sample")?.pngData())
  • Fetch data
let data: String = try OYStore.data(of: .documents(file: "d_text_file", type: .txt))
  • Fetch data with default
OYStore.data(of: .documents(file: "d_text_file", type: .txt), default: "d_default_data")
  • Remove data
try OYStore.remove(of: .documents(key: "d_uiimage_data_key"))
  • Remove all data
try OYStore.removeAll(of: .documents)
  • Is data exist?
try OYStore.isExist(at: .documents(file: "d_text_file", type: .txt))
  • Move data
try OYStore.move(from: .documents(file: "d_text_file", type: .txt), to: .applicationSupport(file: "as_parent_folder/as_text_file", type: .txt))

Temporary

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 data
try OYStore.save(to: .temporary(file: "tmp_text_file", type: .txt), data: "tmp_text_data")
try OYStore.save(to: .temporary(file: "tmp_html_file", type: .html), data: "tmp_html_data")
try OYStore.save(to: .temporary(file: "tmp_image", type: .png), data: UIImage(named: "Sample")?.pngData())

... save data with Folder...

try OYStore.save(to: .temporary(file: "tmp_parent_folder/tmp_image", type: .png), data: UIImage(named: "Sample")?.pngData())
  • Fetch data
let data: String = try OYStore.data(of: .temporary(file: "tmp_text_file", type: .txt))
  • Fetch data with default
OYStore.data(of: .temporary(file: "tmp_text_file", type: .txt), default: "tmp_default_data")
  • Remove data
try OYStore.remove(of: .temporary(key: "tmp_uiimage_data_key"))
  • Remove all data
try OYStore.removeAll(of: .temporary)
  • Is data exist?
try OYStore.isExist(at: .temporary(file: "tmp_text_file", type: .txt))
  • Move data
try OYStore.move(from: .temporary(file: "tmp_text_file", type: .txt), to: .applicationSupport(file: "as_parent_folder/as_text_file", type: .txt))

License

OYStore is released under an MIT license. See LICENSE for details.