{"id":18555449,"url":"https://github.com/k-lpmg/realmwrapper","last_synced_at":"2025-04-09T23:32:20.779Z","repository":{"id":49112731,"uuid":"136768578","full_name":"k-lpmg/RealmWrapper","owner":"k-lpmg","description":"Safe and easy wrappers for RealmSwift","archived":false,"fork":false,"pushed_at":"2021-07-05T16:02:05.000Z","size":142,"stargazers_count":73,"open_issues_count":1,"forks_count":17,"subscribers_count":4,"default_branch":"feature/support-xcode12","last_synced_at":"2025-04-08T14:39:39.849Z","etag":null,"topics":["realm-mobile-database","realmcocoa","realmswift","swift","wrapper-library"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/k-lpmg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-06-10T00:51:29.000Z","updated_at":"2025-02-19T11:53:13.000Z","dependencies_parsed_at":"2022-09-21T10:43:47.122Z","dependency_job_id":null,"html_url":"https://github.com/k-lpmg/RealmWrapper","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-lpmg%2FRealmWrapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-lpmg%2FRealmWrapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-lpmg%2FRealmWrapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/k-lpmg%2FRealmWrapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/k-lpmg","download_url":"https://codeload.github.com/k-lpmg/RealmWrapper/tar.gz/refs/heads/feature/support-xcode12","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248129925,"owners_count":21052664,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["realm-mobile-database","realmcocoa","realmswift","swift","wrapper-library"],"created_at":"2024-11-06T21:26:42.409Z","updated_at":"2025-04-09T23:32:19.083Z","avatar_url":"https://github.com/k-lpmg.png","language":"Swift","readme":"# RealmWrapper \n\n[![Cocoapods](https://img.shields.io/cocoapods/v/RealmWrapper.svg?style=flat)](https://cocoapods.org/pods/RealmWrapper)\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n![Swift](https://img.shields.io/badge/Swift-5.0-orange.svg)\n[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg?style=flat)](https://raw.githubusercontent.com/k-lpmg/RealmWrapper/master/LICENSE)\n[![Build Status](https://travis-ci.org/k-lpmg/RealmWrapper.svg?branch=master)](https://travis-ci.org/k-lpmg/RealmWrapper)\n\nRealmWrapper is wrapper library for [RealmSwift](https://github.com/realm/realm-cocoa/tree/master/RealmSwift) in [realm-cocoa](https://github.com/realm/realm-cocoa)\n\nIf you use [RealmWrapper](https://github.com/k-lpmg/RealmWrapper), you can easily use UI update through Notification and Transaction processing.\nAlso, you do not have to worry about the retain cycle when using self in the Notification block.\n\n- [RealmWrapper](#realmwrapper)\n  - [At a Glance](#at-a-glance)\n  - [Threading](#threading)\n  - [Getting Started](#getting-started)\n  - [Usage](#usage)\n      - [RealmManageable Property](#realmmanageable-property)\n  - [Example](#example)\n  - [Installation](#installation)\n      - [CocoaPods (iOS 9+)](#cocoapods-ios-9)\n      - [Carthage (iOS 9+)](#carthage-ios-9)\n      - [Swift Package Manager (Swift 5.2+, iOS 11+)](#swift-package-manager-swift-52-ios-11)\n  - [LICENSE](#license)\n\n\n## At a Glance\n\nIf you use [RealmSwift](https://github.com/realm/realm-cocoa/tree/master/RealmSwift), you have to be careful about try statement and thread processing every transaction.\nHowever, In [RealmManageable](https://github.com/k-lpmg/RealmWrapper/blob/master/Sources/RealmManageable.swift) which manages one realm file, transaction processing is performed using Realm-only DispatchQueue.\n[RealmProxiable](https://github.com/k-lpmg/RealmWrapper/blob/master/Sources/RealmProxiable.swift), which owns [RealmManageable](https://github.com/k-lpmg/RealmWrapper/blob/master/Sources/RealmManageable.swift), can easily add, modify or delete models without having to worry about try statement and thread processing.\n\n\u003e User Model\n```swift\n@objcMembers\nclass User: Object {\n    dynamic var id: String?\n    dynamic var name: String?\n\n    override static func primaryKey() -\u003e String? {\n        return \"id\"\n    }\n}\n\nvar user = User()\nuser.id = UUID().uuidString\nuser.name = \"Kevin\"\n```\n\n\u003e Using RealmSwift\n```swift\nlet realm = try! Realm(configuration: Realm.Configuration(fileURL: URL(fileURLWithPath: RLMRealmPathForFile(\"user.realm\")), schemaVersion: 1, objectTypes: [User.self]))\ntry! realm.write {\n    realm.add(user)\n}\n```\n\n\u003e Using RealmWrapper\n```swift\nUserRealmManager().transaction(writeHandler: { (realm) in\n    realm.add(user)\n})\n```\n\n```swift\nUserRealmProxy().append(user)\n```\n\n\n## Threading\n\n- By default, you can use the transaction function to process a Realm Transaction in MainThread\n```swift\nUserRealmManager().transaction(writeHandler: { (realm) in\n    realm.add(user)\n})\n```\n\n- It can be implemented by a background thread using DispatchQueue and isSync parameters.\n```swift\nUserRealmManager().transaction(isSync: false, writeHandler: { (realm) in\n    realm.add(user)\n})\n```\n\n```swift\nUserRealmManager().transaction(writeQueue: DispatchQueue(label: \"background\"), isSync: false, writeHandler: { (realm) in\n    realm.add(user)\n})\n```\n\n- You can add completion closure.\n```swift\nUserRealmManager().transaction(isSync: false, writeHandler: { (realm) in\n    realm.add(user)\n}) { (realm, error) in\n    self.tableView.reloadData()\n}\n```\n\n\n## Getting Started\n\n1. Create a `RealmManager` that manages one realm file.\n\n```swift\nfinal class UserRealmManager: RealmManageable {\n\n    var isUseInMemory: Bool {\n        return false\n    }\n    var schemaVersion: UInt64 {\n        return 1\n    }\n    var fileName: String {\n        return \"user\"\n    }\n    var objectTypes: [Object.Type]? {\n        return [User.self]\n    }\n\n}\n```\n\n2. Create a `RealmProxy` that is responsible for the CRUD function to be accessed by the Controller.\n\n```swift\nstruct UserRealmProxy\u003cRealmManager: UserRealmManager\u003e: RealmProxiable {\n\n    var users: RealmQuery\u003cUser\u003e {\n        return query(sortProperty: \"date\", ordering: .ascending)\n    }\n\n    func append(_ user: User) {\n        rm.transaction(writeHandler: { (realm) in\n            realm.add(user, update: .all)\n        })\n    }\n\n    func delete(_ user: User) {\n        rm.transaction(writeHandler: { (realm) in\n            realm.delete(user)\n        })\n    }\n\n    func updateName(id: String, name: String, age: Int) {\n        guard let user = userFromId(id) else {return}\n\n        rm.transaction(writeHandler: { (realm) in\n            user.name = name\n            user.age = age\n            realm.add(user, update: .all)\n        })\n    }\n\n    func userFromId(_ id: String) -\u003e User? {\n        return query(filter: \"id == '\\(id)'\").results.first\n    }\n\n}\n\nvar user = User()\nuser.id = UUID().uuidString\nuser.name = \"Kevin\"\n\nUserRealmProxy().append(user)\nUserRealmProxy().delete(user)\nUserRealmProxy().updateName(id: user.id, name: \"Kris\")\n```\n\n3. If you want to register the notification of the status of the Realm object in the controller, you can register the notification in [RealmQuery](https://github.com/k-lpmg/RealmWrapper/blob/master/Sources/RealmWrapper/RealmQuery.swift).\n\n```swift\nlet users: RealmQuery\u003cUser\u003e = UserRealmProxy().users\n\nusers.setSection(0)\n    .addInsertNotificationBlock(self) { (self, insertions) in\n        self.tableView.reloadData()\n    }\n    .addModificateNotificationBlock(self) { (self, modifications) in\n        self.tableView.reloadRows(at: modifications, with: .fade)\n    }\n    .addDeleteNotificationBlock(self) { (self, deletions) in\n        self.tableView.deleteRows(at: deletions, with: .fade)\n    }\n    .registerNotification()\n```\n\nAlso, since [RealmQuery](https://github.com/k-lpmg/RealmWrapper/blob/master/Sources/RealmWrapper/RealmQuery.swift) is doing weak handling to prevent the retain cycle, you can use closures without worrying about the retain cycle if you pass only self.\n\n```swift\n\npublic func addDeleteNotificationBlock\u003cObject: AnyObject\u003e(_ object: Object, block: @escaping (Object, [IndexPath]) -\u003e Void) -\u003e Self {\n    deleteNotificationBlock = { [weak object] (deletions) in\n        guard let weakObject = object else {return}\n\n        block(weakObject, deletions)\n    }\n    \n    return self\n}\n```\n\n\n## Usage\n\n#### RealmManageable Property\n\n| Property | Type | Default | Description |\n| -------- | ---- | ------- | ---------- |\n| `isUseInMemory` | `Bool` | `required` |`Use InMemory Realm` |\n| `fileName` | `Bool` | `required` |`Realm File Name` |\n| `appGroupIdentifier` | `String?` | `nil` |`Value for Realm file directory for App Extension`|\n\n\n## Example\n\n1. run carthage update\n```console\n$ carthage update --platform iOS\n```\n\n2.  open RealmWrapper.xcodeproj\n```console\n$ open RealmWrapper.xcodeproj\n```\n\n3. run RealmWrapperExample\n\n\n## Installation\n\n#### CocoaPods (iOS 9+)\n\n```ruby\nplatform :ios, '9.0'\nuse_frameworks!\n\ntarget '\u003cYour Target Name\u003e' do\n    pod 'RealmWrapper'\nend\n```\n\n#### Carthage (iOS 9+)\n\n```ruby\ngithub \"k-lpmg/RealmWrapper\"\n```\n\n#### Swift Package Manager (Swift 5.2+, iOS 11+)\n\n```swift\n.package(url: \"https://github.com/k-lpmg/RealmWrapper.git\", .upToNextMajor(from: \"1.4.4\")),\n```\n\n## LICENSE\n\nThese works are available under the MIT license. See the [LICENSE][license] file\nfor more info.\n\n[license]: LICENSE\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk-lpmg%2Frealmwrapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fk-lpmg%2Frealmwrapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fk-lpmg%2Frealmwrapper/lists"}