Swiftpack.co -  cicout/cico_persistent as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
cicout/cico_persistent
CICOPersistent
.package(url: "https://github.com/cicout/cico_persistent.git", from: "0.36.53")

CICOPersistent

Carthage compatible

CICOPersistent is a simple local storage service using codable, a new feature in Swift 4. It contains key-value file, key-value database, orm database, and key-value key chain. You can easily choose what you want. You can also use CICOAutoCodable, a simple extension for codable.

Installation

You can simply add CICOPersistent to your Cartfile:

github "cicout/cico_persistent"

Just add CICOPersistent.framework, CICOAutoCodable.framework and FMDB.framework to your project.

About CICOAutoCodable

Sample Code

Model And JSON Definition

enum MyEnum: String, CICOAutoCodable {
    case one
    case two
}

class MyClass: CICOAutoCodable {
    var stringValue: String = "default_string"
    private(set) var dateValue: Date?
    private(set) var intValue: Int = 0
    private(set) var doubleValue: Double = 1.0
    private(set) var boolValue: Bool = false
    private(set) var enumValue: MyEnum = .one
    private(set) var urlValue: URL?
    private(set) var nextValue: MyClass?
    private(set) var arrayValue: [String]?
    private(set) var dicValue: [String: String]?
}

extension MyClass: ORMProtocol {
    static func cicoORMPrimaryKeyColumnName() -> String {
        return "stringValue"
    }
}
{
    "stringValue": "string",
    "dateValue": 1234567890123,
    "intValue": 123,
    "doubleValue": 2.5,
    "boolValue": true,
    "enumValue": "two",
    "urlValue": "https://www.google.com",
    "nextValue": {
        "stringValue": "string",
        "intValue": 123,
        "doubleValue": 2.5,
        "boolValue": true,
        "enumValue": "two"
    },
    "arrayValue": [
              "string0",
              "string1",
              ],
    "dicValue": {
        "key0": "value0",
        "key1": "value1"
    }
}

Key-Value File Service

  • Initialization
let url = CICOPathAide.defaultPrivateFileURL(withSubPath: "cico_persistent_tests/kv_file")!
self.service = KVFileService.init(rootDirURL: url)
// You can also use (Public/Private/Cache/Temp)KVFileService.shared instead.
  • Read
let key = "test_my_class"
let readValue = self.service.readObject(MyClass.self, forKey: key)
  • Write
let key = "test_my_class"
let value = MyClass.init(jsonString: myJSONString)!
let writeResult = self.service.writeObject(value, forKey: key)
  • Remove
let key = "test_my_class"
let removeResult = self.service.removeObject(forKey: key)
  • Update
    It is a read-update-write sequence function during one lock.
let key = "test_my_class"
self.service
    .updateObject(MyClass.self,
                  forKey: key,
                  updateClosure: { (readObject) -> MyClass? in
                    readObject?.stringValue = "updated_string"
                    return readObject
    }) { (result) in
        print("result = \(result)")
}
  • ClearAll
let clearResult = self.service.clearAll()

URL Key-Value File Service

  • Initialization
self.service = URLKVFileService.init()

let dirURL = CICOPathAide.defaultPrivateFileURL(withSubPath: "cico_persistent_tests/url_kv_file")!
let _ = CICOFileManagerAide.createDir(with: dirURL, option: false)
  • Read
let url = CICOPathAide.defaultPrivateFileURL(withSubPath: "cico_persistent_tests/url_kv_file/test_my_class")!
let readValue = self.service.readObject(MyClass.self, fromFileURL: url)
  • Write
let url = CICOPathAide.defaultPrivateFileURL(withSubPath: "cico_persistent_tests/url_kv_file/test_my_class")!
let value = MyClass.init(jsonString: myJSONString)!
let writeResult = self.service.writeObject(value, toFileURL: url)
  • Remove
let url = CICOPathAide.defaultPrivateFileURL(withSubPath: "cico_persistent_tests/url_kv_file/test_my_class")!
let removeResult = self.service.removeObject(forFileURL: url)
  • Update
    It is a read-update-write sequence function during one lock.
let url = CICOPathAide.defaultPrivateFileURL(withSubPath: "cico_persistent_tests/url_kv_file/test_my_class")!
self.service
    .updateObject(MyClass.self,
                  fromFileURL: url,
                  updateClosure: { (readObject) -> MyClass? in
                    readObject?.stringValue = "updated_string"
                    return readObject
    }) { (result) in
        print("result = \(result)")
}

Key-Value DB Service

  • Initialization
let url = CICOPathAide.defaultPrivateFileURL(withSubPath: "cico_persistent_tests/kv.db")!
self.service = KVDBService.init(fileURL: url)
// You can also use (Public/Private/Cache/Temp)KVDBService.shared instead.
  • Read
let key = "test_my_class"
let readValue = self.service.readObject(MyClass.self, forKey: key)
  • Write
let key = "test_my_class"
let value = MyClass.init(jsonString: myJSONString)!
let writeResult = self.service.writeObject(value, forKey: key)
  • Remove
let key = "test_my_class"
let removeResult = self.service.removeObject(forKey: key)
  • Update
    It is a read-update-write sequence function during one lock.
let key = "test_my_class"
self.service
    .updateObject(MyClass.self,
                  forKey: key,
                  updateClosure: { (readObject) -> MyClass? in
                    readObject?.stringValue = "updated_string"
                    return readObject
    }) { (result) in
        print("result = \(result)")
}
  • ClearAll
let clearResult = self.service.clearAll()

ORM DB Service

let url = CICOPathAide.defaultPrivateFileURL(withSubPath: "cico_persistent_tests/orm.db")!
self.service = ORMDBService.init(fileURL: url)
// You can also use (Public/Private/Cache/Temp)ORMDBService.shared instead.
  • Read
let key = "string"
let readObject = self.service.readObject(ofType: MyClass.self, primaryKeyValue: key)
  • Read Array
let readObjectArray = self.service.readObjectArray(ofType: MyClass.self, whereString: nil, orderByName: "stringValue", descending: false, limit: 10)
  • Write
let value = MyClass.init(jsonString: myJSONString)!
let writeResult = self.service.writeObject(value)
  • Write Array
var objectArray = [MyClass](https://raw.github.com/cicout/cico_persistent/master/)
for i in 0..<20 {
    let object = MyClass.init(jsonString: myJSONString)!
    object.stringValue = "string_\(i)"
    objectArray.append(object)
}
let writeResult = self.service.writeObjectArray(objectArray)
  • Remove
let key = "string"
let removeResult = self.service.removeObject(ofType: MyClass.self, primaryKeyValue: key)
  • Remove Object Table
let removeResult = self.service.removeObjectTable(ofType: MyClass.self)
  • Update
    It is a read-update-write sequence function during one lock.
let key = "string"
self.service
    .updateObject(ofType: MyClass.self,
                  primaryKeyValue: key,
                  customTableName: nil,
                  updateClosure: { (readObject) -> MyClass? in
                    readObject?.stringValue = "updated_string"
                    return readObject
    }) { (result) in
        print("result = \(result)")
}
  • ClearAll
let clearResult = self.service.clearAll()

Key-Value KeyChain Service

  • Initialization
self.service = KVKeyChainService.init(encryptionKey: "test_encryption_key")
// You can also use KVKeyChainService.defaultService instead.
  • Read
let key = "test_my_class"
let readValue = KVKeyChainService.defaultService.readObject(MyClass.self, forKey: key)
  • Write
let key = "test_my_class"
let value = MyClass.init(jsonString: myJSONString)!
let result = KVKeyChainService.defaultService.writeObject(value, forKey: key)
  • Remove
let key = "test_my_class"
let removeResult = KVKeyChainService.defaultService.removeObject(forKey: key)
  • Update
    It is a read-update-write sequence function during one lock.
let key = "test_my_class"
KVKeyChainService
    .defaultService
    .updateObject(MyClass.self,
                  forKey: key,
                  updateClosure: { (readObject) -> MyClass? in
                    readObject?.stringValue = "updated_string"
                    return readObject
    }) { (result) in
        print("result = \(result)")
}

Persistent Service

It is all local storage API collection. It contains user defaults, key-value file, key-value database, orm database, and key-value key chain.

About Sandbox

For security purposes, iOS file system can be divided into four types as shown below.

  • Public: "Sandbox"/Documents/
    The contents of this directory can be made available to the user through file sharing. The files may be read/wrote/deleted by user. It should only contain imported/exported files here.

  • Private: "Sandbox"/Library/
    Any file you don’t want exposed to the user can be saved here.

  • Cache: "Sandbox"/Library/Caches/
    All cache files should be placed here.

  • Temp: "Sandbox"/tmp/
    Use this directory to write temporary files that do not need to persist between launches of your app. Your app should remove files from this directory when they are no longer needed.

Four shared services "Public/Private/Cache/Temp" have been created, you can use them directly.

Requirements

  • iOS 8.0+
  • Swift 4.0+

License

CICOPersistent is released under the MIT license. See LICENSE for details.

More

Have a question? Please open an issue!

GitHub

link
Stars: 1
Last commit: 5 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API