ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - k-lpmg/RealmWrapper: Safe and easy wrappers for RealmSwift
Safe and easy wrappers for RealmSwift. Contribute to k-lpmg/RealmWrapper development by creating an account on GitHub.
Visit Site

GitHub - k-lpmg/RealmWrapper: Safe and easy wrappers for RealmSwift

GitHub - k-lpmg/RealmWrapper: Safe and easy wrappers for RealmSwift

RealmWrapper

Cocoapods Carthage compatible Swift GitHub license Build Status

RealmWrapper is wrapper library for RealmSwift in realm-cocoa

If you use RealmWrapper, you can easily use UI update through Notification and Transaction processing. Also, you do not have to worry about the retain cycle when using self in the Notification block.

At a Glance

If you use RealmSwift, you have to be careful about try statement and thread processing every transaction. However, In RealmManageable which manages one realm file, transaction processing is performed using Realm-only DispatchQueue. RealmProxiable, which owns RealmManageable, can easily add, modify or delete models without having to worry about try statement and thread processing.

User Model

@objcMembers
class User: Object {
    dynamic var id: String?
    dynamic var name: String?

    override static func primaryKey() -> String? {
        return "id"
    }
}

var user = User()
user.id = UUID().uuidString
user.name = "Kevin"

Using RealmSwift

let realm = try! Realm(configuration: Realm.Configuration(fileURL: URL(fileURLWithPath: RLMRealmPathForFile("user.realm")), schemaVersion: 1, objectTypes: [User.self]))
try! realm.write {
    realm.add(user)
}

Using RealmWrapper

UserRealmManager().transaction(writeHandler: { (realm) in
    realm.add(user)
})
UserRealmProxy().append(user)

Threading

  • By default, you can use the transaction function to process a Realm Transaction in MainThread
UserRealmManager().transaction(writeHandler: { (realm) in
    realm.add(user)
})
  • It can be implemented by a background thread using DispatchQueue and isSync parameters.
UserRealmManager().transaction(isSync: false, writeHandler: { (realm) in
    realm.add(user)
})
UserRealmManager().transaction(writeQueue: DispatchQueue(label: "background"), isSync: false, writeHandler: { (realm) in
    realm.add(user)
})
  • You can add completion closure.
UserRealmManager().transaction(isSync: false, writeHandler: { (realm) in
    realm.add(user)
}) { (realm, error) in
    self.tableView.reloadData()
}

Getting Started

  1. Create a RealmManager that manages one realm file.
final class UserRealmManager: RealmManageable {

    var isUseInMemory: Bool {
        return false
    }
    var schemaVersion: UInt64 {
        return 1
    }
    var fileName: String {
        return "user"
    }
    var objectTypes: [Object.Type]? {
        return [User.self]
    }

}
  1. Create a RealmProxy that is responsible for the CRUD function to be accessed by the Controller.
struct UserRealmProxy<RealmManager: UserRealmManager>: RealmProxiable {

    var users: RealmQuery<User> {
        return query(sortProperty: "date", ordering: .ascending)
    }

    func append(_ user: User) {
        rm.transaction(writeHandler: { (realm) in
            realm.add(user, update: .all)
        })
    }

    func delete(_ user: User) {
        rm.transaction(writeHandler: { (realm) in
            realm.delete(user)
        })
    }

    func updateName(id: String, name: String, age: Int) {
        guard let user = userFromId(id) else {return}

        rm.transaction(writeHandler: { (realm) in
            user.name = name
            user.age = age
            realm.add(user, update: .all)
        })
    }

    func userFromId(_ id: String) -> User? {
        return query(filter: "id == '\(id)'").results.first
    }

}

var user = User()
user.id = UUID().uuidString
user.name = "Kevin"

UserRealmProxy().append(user)
UserRealmProxy().delete(user)
UserRealmProxy().updateName(id: user.id, name: "Kris")
  1. If you want to register the notification of the status of the Realm object in the controller, you can register the notification in RealmQuery.
let users: RealmQuery<User> = UserRealmProxy().users

users.setSection(0)
    .addInsertNotificationBlock(self) { (self, insertions) in
        self.tableView.reloadData()
    }
    .addModificateNotificationBlock(self) { (self, modifications) in
        self.tableView.reloadRows(at: modifications, with: .fade)
    }
    .addDeleteNotificationBlock(self) { (self, deletions) in
        self.tableView.deleteRows(at: deletions, with: .fade)
    }
    .registerNotification()

Also, since RealmQuery is doing weak handling to prevent the retain cycle, you can use closures without worrying about the retain cycle if you pass only self.


public func addDeleteNotificationBlock<Object: AnyObject>(_ object: Object, block: @escaping (Object, [IndexPath]) -> Void) -> Self {
    deleteNotificationBlock = { [weak object] (deletions) in
        guard let weakObject = object else {return}

        block(weakObject, deletions)
    }
    
    return self
}

Usage

RealmManageable Property

Property Type Default Description
isUseInMemory Bool required Use InMemory Realm
fileName Bool required Realm File Name
appGroupIdentifier String? nil Value for Realm file directory for App Extension

Example

  1. run carthage update
$ carthage update --platform iOS
  1. open RealmWrapper.xcodeproj
$ open RealmWrapper.xcodeproj
  1. run RealmWrapperExample

Installation

CocoaPods (iOS 9+)

platform :ios, '9.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'RealmWrapper'
end

Carthage (iOS 9+)

github "k-lpmg/RealmWrapper"

Swift Package Manager (Swift 5.2+, iOS 11+)

.package(url: "https://github.com/k-lpmg/RealmWrapper.git", .upToNextMajor(from: "1.4.4")),

LICENSE

These works are available under the MIT license. See the LICENSE file for more info.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory