Coder Social home page Coder Social logo

pisces / orangerealm Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 22.94 MB

Integrate UI with Realm easily.

License: MIT License

Swift 6.82% Ruby 0.04% Objective-C 7.11% C++ 70.83% Objective-C++ 11.78% Shell 3.16% C 0.26%
realm realmswift carthage cocoapods xcode filter safety-multithreading swift limit-query generic

orangerealm's Introduction

OrangeRealm

Swift CI Status Version License Platform Carthage Compatible

  • OrangeRealm helps you safety multithreading and UI integration using Realm

Features

  • Thread safety
  • Simple interface
  • Easy UI integration
  • Support query limit, max, offset with filter, unlink
  • Abstraction for life cycle of Realm

Import

import OrangeRealm

Example

First - Create your RealmManager

Your RealmManager will match to one realm file by one to one

import RealmSwift
import OrangeRealm

class SampleRealmManager: AbstractRealmManager {

    // MARK: - Overridden: AbstractRealmManager

    override class var shared: AbstractRealmManager {
        struct Static {
            static let instance = SampleRealmManager()
        }
        return Static.instance
    }
    
    override var schemaVersion: UInt64 {
        return 1
    }
    
    override var fileURL: URL {
        return URL(fileURLWithPath: "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!)/sample.realm", isDirectory: false)
    }
    
    override var objectTypes: [Object.Type]? {
        return [SampleObject.self]
    }
    
    override func deleteAll(_ realm: Realm) {
        realm.deleteAll()
    }
    
    override func process(forMigration migration: Migration, oldSchemaVersion: UInt64) {
    }
    
    override func recover() {
    }
}

Second - Create your Realm Object

It is same to bagic implementation for realm object

import RealmSwift

class SampleObject: Object {
    dynamic var name: String?
    dynamic var id: Int = 0
    
    convenience init(id: Int, name: String?) {
        self.init()
        
        self.id = id
        self.name = name
    }
    
    override class func primaryKey() -> String? {
        return "id"
    }
}

And Last - Integrate your UI

This sample is integration with UITableView

// Sync section for UITableView
// Update UITableView after add notification for realm
let result: RealmQueryResult<SampleObject> = SampleRealmManager.shared.query("id > 0", sortProperty: "id", ascending: false)
    .set(section: 1)
    .changed({ [weak self] (section, deletions, insertions, modifications) in
        guard let weakSelf = self else {return}
        
        weakSelf.tableView.beginUpdates()
        weakSelf.tableView.deleteRows(at: deletions, with: .none)
        weakSelf.tableView.insertRows(at: insertions, with: .none)
        weakSelf.tableView.reloadRows(at: modifications, with: .none)
        weakSelf.tableView.endUpdates()
    })

self.tableView.reloadData()

Support results of 3 types - RealmQueryResult, Generic array, RealmResult

let result: RealmQueryResult<SampleObject> = SampleRealmManager.shared.query()
let result: [SampleObject] = SampleRealmManager.shared.objects()
let result: Results<SampleObject> = SampleRealmManager.shared.results()

Doing limit, max query

let result: RealmQueryResult<SampleObject> = SampleRealmManager.shared.query("id > 0", sortProperty: "id", ascending: false, limit: 10, max: 10)

Doing offset query with filter

let offset = 2

let result: RealmQueryResult<SampleObject> = SampleRealmManager.shared.query("id > 0", sortProperty: "id", ascending: false) { (object) -> Bool in
    return object.id! > offset
}

Gain unlinked objects from Realm

let result: RealmQueryResult<SampleObject> = SampleRealmManager.shared.query("id > 0", sortProperty: "id", ascending: false, unlink: true)

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

CocoaPods 1.1.0+ is required to build OrangeRealm 0.1.0+.

To integrate OrangeRealm into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'

target '<Your Target Name>' do
    pod 'OrangeRealm', '~> 0.1.0'
end

Then, run the following command:

$ pod install

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Alamofire into your Xcode project using Carthage, specify it in your Cartfile:

github "pisces/OrangeRealm" ~> 0.1.0

Run carthage update to build the framework and drag the built OrangeRealm.framework into your Xcode project.

Requirements

iOS Deployment Target 8.0 higher

Author

Steve Kim, [email protected]

License

OrangeRealm is available under the MIT license. See the LICENSE file for more info.

orangerealm's People

Contributors

pisces avatar

Stargazers

ArenHansy avatar

Watchers

 avatar ArenHansy avatar

Forkers

rubythonode

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.