目录
目录README.md

Arcade

Arcade is a lightweight persistence layer for Swift structures or objects!

Installation

Arcade can be installed using the Swift Package Manager. Add the following to your Package.swift file:


dependencies: [
    .Package(url: "https://github.com/infinitetoken/Arcade.git", from: "1.0.0")
]

Usage

Models

Models (structures) in your project must conform to Storable.

import Arcade

struct Owner: Storable {

    static var table: Table = AppTable.owner

    var persistentId: String {
        // Return your models unique store ID
    }
    
    var name: String?

}

Table

The Table protocol defines the names of the tables in which to store your models.

import Arcade

enum AppTable: String, Table {
    case owner = "Owner"

    var name: String {
        return self.rawValue
    }
}

Configure Adapter

import Arcade

let adapter = InMemoryAdapter()

Inserting

import Arcade

let owner = Owner(id: id(), name: "Foo")

adapter.insert(storable: owner) { (result) in
    // Inserted! (or Error)
}

Updating

import Arcade

owner.name = "Fred"

adapter.update(storable: owner) { (result) in
    // Updated! (or Error)
}

Deleting

import Arcade

let id = owner.id

adapter.delete(id: id, type: Owner.self) { (result) in
    // Deleted! (or Error)
}

Fetching

To find a specific item by id:

import Arcade

adapter.find(id: owner.id) { (result) in
    // Found it! (or Error)
}

To query a set of items:

import Arcade

let expression = Expression.equal("name", "Foo")
let query = Query.expression(expression)

adapter.fetch(query: query) { (result) in
    // Do something with result... (or Error)
}

CoreData

To use Arcade with the CoreData adapter some additional protocol conformance must be setup. Your CoreData entites should conform to CoreDataStorable:


@objc(OwnerEntity)
class OwnerEntity: NSManagedObject {

    @NSManaged var id: id
    @NSManaged var name: String?

    override func awakeFromInsert() {
        super.awakeFromInsert()

        self.id = id()
    }

}

extension OwnerEntity: CoreDataStorable {

    public var storable: Storable {
        return Owner(id: self.id, name: self.name)
    }

    public func update(with storable: Storable) -> Bool {
        if let id = storable as? id {
            self.id = id
        }
    
        if let name = storable.name as? String {
            self.name = name
        }

        return true
    }

}

That’s it! Now you can save your objects using the CoreDataAdapter just like any other object!

License

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

关于
1.4 MB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号