{"id":13829884,"url":"https://github.com/CombineCommunity/CombineRealm","last_synced_at":"2025-07-09T10:31:56.887Z","repository":{"id":49003469,"uuid":"238560354","full_name":"CombineCommunity/CombineRealm","owner":"CombineCommunity","description":"Combine extensions for Realm","archived":false,"fork":false,"pushed_at":"2021-07-01T10:05:31.000Z","size":57,"stargazers_count":60,"open_issues_count":1,"forks_count":20,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-11-07T10:04:30.045Z","etag":null,"topics":["combine","database","realm","realmswift","swift"],"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/CombineCommunity.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-02-05T22:22:04.000Z","updated_at":"2024-05-29T23:48:39.000Z","dependencies_parsed_at":"2022-09-07T14:00:34.907Z","dependency_job_id":null,"html_url":"https://github.com/CombineCommunity/CombineRealm","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CombineCommunity%2FCombineRealm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CombineCommunity%2FCombineRealm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CombineCommunity%2FCombineRealm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/CombineCommunity%2FCombineRealm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/CombineCommunity","download_url":"https://codeload.github.com/CombineCommunity/CombineRealm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225345839,"owners_count":17459873,"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":["combine","database","realm","realmswift","swift"],"created_at":"2024-08-04T10:00:48.404Z","updated_at":"2024-11-20T10:31:26.780Z","avatar_url":"https://github.com/CombineCommunity.png","language":"Swift","funding_links":[],"categories":["Swift"],"sub_categories":["\u003ca name=\"swift-libs\"\u003e\u003c/a\u003eSwift Libraries and Components"],"readme":"# CombineRealm\n\n![CombineRealm Logo](CombineRealm.png)\n\n[![Version](https://img.shields.io/cocoapods/v/Combine-Realm.svg?style=flat)](http://cocoapods.org/pods/Combine-Realm)\n[![License](https://img.shields.io/cocoapods/l/Combine-Realm.svg?style=flat)](http://cocoapods.org/pods/Combine-Realm)\n![Platform](https://img.shields.io/badge/platforms-iOS%2013%20\u0026%20macOS%2010.15%20\u0026%20tvOS%2013%20\u0026%20watchOS%206-success.svg)\n\nThis library is a thin wrapper around __RealmSwift__ ([Realm Docs](https://realm.io/docs/swift/latest/)), inspired by the RxSwift Community's [RxRealm](https://github.com/RxSwiftCommunity/RxRealm) library.\n\n## Usage\n\n**Table of contents:**\n\n 1. [Observing object collections](https://github.com/istvan-kreisz/CombineRealm#observing-object-collections)\n 2. [Observing a single object](https://github.com/istvan-kreisz/CombineRealm#observing-a-single-object)\n 3. [Observing a realm instance](https://github.com/istvan-kreisz/CombineRealm#observing-a-realm-instance)\n 4. [Write transactions](https://github.com/istvan-kreisz/CombineRealm#write-transactions)\n 5. [Delete transactions](https://github.com/istvan-kreisz/CombineRealm#delete-transactions)\n 6. [Example app](https://github.com/istvan-kreisz/CombineRealm#example-app)\n\n### Observing object collections\n\nCombineRealm can be used to create `Publisher`s from objects of type `Results`, `List`, `LinkingObjects` or `AnyRealmCollection`. These types are typically used to load and observe object collections from the Realm Mobile Database.\n\n#### `RealmPublishers.collection(from:synchronousStart:)`\n\nEmits an event each time the collection changes:\n\n```swift\nlet realm = try! Realm()\nlet colors = realm.objects(Color.self)\n\nRealmPublishers.collection(from: colors)\n    .map { colors in \"colors: \\(colors.count)\" }\n    .sink(receiveCompletion: { _ in\n        print(\"Completed\")\n    }, receiveValue: { result in\n        print(result)\n    })\n```\n\nThe above prints out \"colors: X\" each time a `Color` instance is added or removed from the database. If you set `synchronousStart` to `true` (the default value), the first element will be emitted synchronously - e.g. when you're binding UI it might not be possible for an asynchronous notification to come through.\n\n#### `RealmPublishers.array(from:synchronousStart:)`\nUpon each change fetches a snapshot of the Realm collection and converts it to an array value (for example if you want to use array methods on the collection):\n\n```swift\nlet realm = try! Realm()\nlet colors = realm.objects(Color.self)\n\nRealmPublishers.array(from: colors)\n    .map { colors in colors.prefix(3) }\n    .sink(receiveCompletion: { _ in\n        print(\"Completed\")\n    }, receiveValue: { colors in\n        print(colors)\n    })\n```\n\n#### `RealmPublishers.changeset(from:synchronousStart:)`\nEmits every time the collection changes and provides the exact indexes that have been deleted, inserted or updated along with the appropriate `AnyRealmCollection\u003cT\u003e` value:\n\n```swift\nlet realm = try! Realm()\nlet colors = realm.objects(Color.self)\n\nRealmPublishers.changeset(from: colors)\n    .sink(receiveCompletion: { _ in\n        print(\"Completed\")\n    }, receiveValue: { results, changes in\n        if let changes = changes {\n            // it's an update\n            print(results)\n            print(\"deleted: \\(changes.deleted)\")\n            print(\"inserted: \\(changes.inserted)\")\n            print(\"updated: \\(changes.updated)\")\n        } else {\n            // it's the initial data\n            print(results)\n        }\n    })\n```\n\n#### `RealmPublishers.arrayWithChangeset(from:synchronousStart:)`\n\nEmits every time the collection changes and provides the exact indexes that have been deleted, inserted or updated along with the `Array\u003cT\u003e` value:\n\n```swift\nlet realm = try! Realm()\nlet colors = realm.objects(Color.self))\n\nRealmPublishers.arrayWithChangeset(from: colors)\n    .sink(receiveCompletion: { _ in\n        print(\"Completed\")\n    }, receiveValue: { array, changes in\n        if let changes = changes {\n            // it's an update\n            print(array)\n            print(\"deleted: \\(changes.deleted)\")\n            print(\"inserted: \\(changes.inserted)\")\n            print(\"updated: \\(changes.updated)\")\n        } else {\n            // it's the initial data\n            print(array)\n        }\n    })\n```\n\n### Observing a single object\n\n#### `RealmPublishers.from(object:emitInitialValue:properties:)`\n\nEmits every time any of the properties of the observed object change.\n\nIt will by default emit the object's initial state as its first value. You can disable this behavior by using the `emitInitialValue` parameter and setting it to `false`.\n\n```swift\nRealmPublishers.from(object: color)\n    .sink(receiveCompletion: { _ in\n        print(\"Completed\")\n    }) { color in\n        print(color)\n    }\n```\n\nYou can set which property changes you'd like to observe:\n\n```swift\nObservable.from(object: ticker, properties: [\"red\", \"green\", \"blue\"])\n```\n\n### Observing a realm instance\n\n#### `RealmPublishers.from(realm:)`\n\nEmits every time the realm changes: any create \u0026 update \u0026 delete operation happens in it. It provides the realm instance along with the realm change notification.\n\n```swift\nlet realm = try! Realm()\n\nRealmPublishers.from(realm: realm)\n    .sink(receiveCompletion: { _ in\n        print(\"Completed\")\n    }) { realm, notification in\n        print(\"Something happened!\")\n    }\n```\n\n### Write transactions\n\n#### `addToRealm()`\n\nWrites object(s) to the default Realm: `Realm(configuration: .defaultConfiguration)`.\n\n```swift\nlet realm = try! Realm()\nlet colors = realm.objects(Color.self))\n\nRealmPublishers.array(from: colors)\n  .addToRealm()\n```\n\n#### `addToRealm(configuration:updatePolicy:onError:)`\n\nWrites object(s) to a **custom** Realm. If you want to switch threads and not use the default Realm, provide a `Realm.Configuration`. You an also provide an error handler for the observer to be called if either creating the realm reference or the write transaction raise an error:\n\nNOTE: All 3 arguments are optional, check the function definition for the default values\n\n```swift\nlet realm = try! Realm()\nlet colors = realm.objects(Color.self))\n\nRealmPublishers.array(from: colors)\n  .addToRealm(configuration: .defaultCOnfiguration, updatePolicy: .error, onError: {\n      print($0)\n  })\n```\n\n### Delete transactions\n\n#### `deleteFromRealm()`\n\nDeletes object(s) from the object(s)'s realm:\n\n```swift\nlet realm = try! Realm()\nlet colors = realm.objects(Color.self))\n\nRealmPublishers.array(from: colors)\n  .deleteFromRealm()\n```\n\n#### `deleteFromRealm(onError:)`\n\nDeletes object(s) from the object(s)'s realm. You an also provide an error handler for the observer to be called if either creating the realm reference or the write transaction raise an error:\n\n```swift\nlet realm = try! Realm()\nlet colors = realm.objects(Color.self))\n\nRealmPublishers.array(from: colors)\n  .deleteFromRealm(onError: {\n      print($0)\n  })\n```\n\n### Example app\n\nTo run the example project, clone the repo, navigate to the __Example__ folder and open the `Example.xcodeproj` file.\n\nTo ensure that you're using the latest version of CombineRealm, in Xcode select `Update to Latest Package Versions` in the `File/Swift Packages/Add Package Dependency...` menu.\n\nThe app uses CombineRealm to observe changes in and write to Realm.\n\n## Testing\n\nTo inspect the library's Unit tests, check out the files in `Tests/CombineRealmTests`.\nTo run the tests, go to the root directory of the repo and run the command:\n\n```swift\nswift test\n```\n\n## Installation\n\n### CocoaPods\n\nAdd the following line to your Podfile and run `pod install`:\n\n```ruby\npod 'Combine-Realm'\n```\nSince import statements in Xcode can't contain dashes, the correct way to import the library is:\n\n```swift\nimport Combine_Realm\n```\n\n### Swift Package Manager\n\n- In Xcode select `File/Swift Packages/Add Package Dependency...`\n- Paste `https://github.com/istvan-kreisz/CombineRealm.git` into the repository URL textfield.\n\n### Future ideas\n\n- Add CI tests\n- Add Carthage support\n- Your ideas?\n\n## Author\n\n__Istvan Kreisz__\n\n[kreiszdev@gmail.com](mailto:kreiszdev@gmail.com)\n\n[@IKreisz](https://twitter.com/IKreisz)\n\n## License\n\nCombineReachability is available under the MIT license. See the LICENSE file for more info.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCombineCommunity%2FCombineRealm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FCombineCommunity%2FCombineRealm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FCombineCommunity%2FCombineRealm/lists"}