Giter Site home page Giter Site logo

dbclient's Introduction

DBClient

cocoapods swift Platform License

Integration (Cocoapods)

There're three podspecs:

  • DBClient/Core contains pure (CoreData/Realm-free) interface / types used to abstract from implementation. Use it only in case you're about to provide custom implementation of any available storage types.
  • DBClient/CoreData contains CoreData implementation.
  • DBClient/Realm contains Realm implementation.

Usage

Depending on DataBase type you need to create a client: let client: DBClient = RealmDBClient(realm: realm) or let client: DBClient = CoreDataDBClient(forModel: "Users")

Base methods (CRUD, observe) are the same for each type and could be found documented in DBClient.swift

Each model you create required to conform Stored protocol with two properties:

extension User: Stored {

    public static var primaryKeyName: String? {
        return "id"
    }

    public var valueOfPrimaryKey: CVarArg? {
        return id
    }
}

For each model you create you need to define associated database model described below.

Realm

To adopt Realm, you need to provide RealmModelConvertible protocol implementation for each model you want to support. extension User: RealmModelConvertible

The protocol contains three required methods.

The first one provides a class (decendant of realm's Object) to be associated with your model:

static func realmClass() -> Object.Type {
    return ObjectUser.self
}

The second one converts abstract realm's Object to your model:

static func from(_ realmObject: Object) -> Stored {
    guard let objectUser = realmObject as? ObjectUser else {
        fatalError("Can't create `User` from \(realmObject)")
    }

    return User(id: objectUser.id, name: objectUser.name)
}

The last one converts your model to realm's object:

func toRealmObject() -> Object {
    let user = ObjectUser()
    user.id = id
    user.name = name

    return user
}

CoreData

To adopt CoreData, you need to create your model and provide appropriate file name to client's constructor (bundle could also be specified) and for each your model provide implementation of the CoreDataModelConvertible protocol. extension User: CoreDataModelConvertible

The protocol requires four methods and one field to be implemented. Documentation for each method/field could be found in CoreDataDBClient.swift

In the field entityName you should provide entity name (equal to one specified in your model):

public static var entityName: String {
    return String(describing: self)
}

The next method used to determine associated NSManagedObject ancestor to your model:

public static func managedObjectClass() -> NSManagedObject.Type {
    return ManagedUser.self
}

The next method determines whether given object equal to current:

func isPrimaryValueEqualTo(value: Any) -> Bool {
    if let value = value as? String {
        return value == id
    }

    return false
}

Next method used to convert NSManagedObject to your model. Feel free to fail with fatalError here since it's developer's issue.

public static func from(_ managedObject: NSManagedObject) -> Stored {
    guard let managedUser = managedObject as? ManagedUser else {
        fatalError("can't create User object from object \(managedObject)")
    }
    guard let id = managedUser.id,
        let name = managedUser.name else {
        fatalError("can't get required properties for user \(managedObject)")
    }

    return User(id: id, name: name)
}

The last method used to create/update NSManagedObject in given context based on your model:

public func upsertManagedObject(in context: NSManagedObjectContext, existedInstance: NSManagedObject?) -> NSManagedObject {
    var user: ManagedUser
    if let result = existedInstance as? ManagedUser { // fetch existing
        user = result
    } else { // or create new
        user = NSEntityDescription.insertNewObject(
            forEntityName: User.entityName,
            into: context
            ) as! ManagedUser
    }
    user.id = id
    user.name = name

    return user
}

Version history

Version Swift Dependencies iOS
1.4.2 5 RealmSwift 3.15.0, YALResult 1.4 10
1.3 4.2 RealmSwift 3.11.1, YALResult 1.1 10
1.0 4.2 RealmSwift 2.10.1, YALResult 1.0 10
0.7 4.0 RealmSwift 2.10.1, BoltsSwift 1.4 9
0.6 4 RealmSwift 2.10.1, BoltsSwift 1.3 9
0.4.2 3.2 RealmSwift 2.1.1, BoltsSwift 1.3 9

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.