这个库是 RealmSwift 的一个轻量级封装 (Realm 文档),灵感来源于 RxSwift 社区的 RxRealm 库。
目录
CombineRealm 可以用来从 Results
, List
, LinkingObjects
或 AnyRealmCollection
类型的对象创建 Publisher
。这些类型通常用于从 Realm 移动数据库加载和观察对象集合。
每次集合更改时都会发出一个事件
let realm = try! Realm()
let colors = realm.objects(Color.self)
RealmPublishers.collection(from: colors)
.map { colors in "colors: \(colors.count)" }
.sink(receiveCompletion: { _ in
print("Completed")
}, receiveValue: { result in
print(result)
})
以上代码会在每次 Color
实例被添加到数据库或从数据库中删除时打印 "colors: X"。 如果你将 synchronousStart
设置为 true
(默认值),则第一个元素将同步发出 - 例如,当你绑定 UI 时,异步通知可能无法传递。
每次更改时,都会获取 Realm 集合的快照,并将其转换为数组值(例如,如果你想在集合上使用数组方法)
let realm = try! Realm()
let colors = realm.objects(Color.self)
RealmPublishers.array(from: colors)
.map { colors in colors.prefix(3) }
.sink(receiveCompletion: { _ in
print("Completed")
}, receiveValue: { colors in
print(colors)
})
每次集合更改时都会发出事件,并提供已删除、插入或更新的确切索引,以及相应的 AnyRealmCollection<T>
值
let realm = try! Realm()
let colors = realm.objects(Color.self)
RealmPublishers.changeset(from: colors)
.sink(receiveCompletion: { _ in
print("Completed")
}, receiveValue: { results, changes in
if let changes = changes {
// it's an update
print(results)
print("deleted: \(changes.deleted)")
print("inserted: \(changes.inserted)")
print("updated: \(changes.updated)")
} else {
// it's the initial data
print(results)
}
})
每次集合更改时都会发出事件,并提供已删除、插入或更新的确切索引,以及 Array<T>
值
let realm = try! Realm()
let colors = realm.objects(Color.self))
RealmPublishers.arrayWithChangeset(from: colors)
.sink(receiveCompletion: { _ in
print("Completed")
}, receiveValue: { array, changes in
if let changes = changes {
// it's an update
print(array)
print("deleted: \(changes.deleted)")
print("inserted: \(changes.inserted)")
print("updated: \(changes.updated)")
} else {
// it's the initial data
print(array)
}
})
每次观察对象的任何属性发生更改时都会发出事件。
默认情况下,它会将对象的初始状态作为其第一个值发出。你可以使用 emitInitialValue
参数并将其设置为 false
来禁用此行为。
RealmPublishers.from(object: color)
.sink(receiveCompletion: { _ in
print("Completed")
}) { color in
print(color)
}
你可以设置你想要观察哪些属性的更改
Observable.from(object: ticker, properties: ["red", "green", "blue"])
每次 Realm 发生更改时都会发出事件:任何创建、更新和删除操作都会发生。它提供 Realm 实例以及 Realm 更改通知。
let realm = try! Realm()
RealmPublishers.from(realm: realm)
.sink(receiveCompletion: { _ in
print("Completed")
}) { realm, notification in
print("Something happened!")
}
将对象写入默认 Realm:Realm(configuration: .defaultConfiguration)
。
let realm = try! Realm()
let colors = realm.objects(Color.self))
RealmPublishers.array(from: colors)
.addToRealm()
将对象写入自定义 Realm。 如果你想切换线程并且不使用默认的 Realm,请提供一个 Realm.Configuration
。 你还可以为观察者提供一个错误处理程序,以便在创建 Realm 引用或写入事务引发错误时调用。
注意:所有 3 个参数都是可选的,请查看函数定义以获取默认值
let realm = try! Realm()
let colors = realm.objects(Color.self))
RealmPublishers.array(from: colors)
.addToRealm(configuration: .defaultCOnfiguration, updatePolicy: .error, onError: {
print($0)
})
从对象所在的 Realm 中删除对象
let realm = try! Realm()
let colors = realm.objects(Color.self))
RealmPublishers.array(from: colors)
.deleteFromRealm()
从对象所在的 Realm 中删除对象。 你还可以为观察者提供一个错误处理程序,以便在创建 Realm 引用或写入事务引发错误时调用。
let realm = try! Realm()
let colors = realm.objects(Color.self))
RealmPublishers.array(from: colors)
.deleteFromRealm(onError: {
print($0)
})
要运行示例项目,请克隆存储库,导航到 Example 文件夹并打开 Example.xcodeproj
文件。
为确保你使用的是最新版本的 CombineRealm,请在 Xcode 的 File/Swift Packages/Add Package Dependency...
菜单中选择 Update to Latest Package Versions
。
该应用程序使用 CombineRealm 来观察 Realm 中的更改并写入 Realm。
要检查库的单元测试,请查看 Tests/CombineRealmTests
中的文件。 要运行测试,请转到存储库的根目录并运行命令
swift test
将以下行添加到你的 Podfile 并运行 pod install
pod 'Combine-Realm'
由于 Xcode 中的 import 语句不能包含破折号,因此导入库的正确方法是
import Combine_Realm
File/Swift Packages/Add Package Dependency...
https://github.com/istvan-kreisz/CombineRealm.git
粘贴到存储库 URL 文本字段中。Istvan Kreisz
CombineReachability 在 MIT 许可证下可用。 有关更多信息,请参见 LICENSE 文件。