项目作者: k-lpmg

项目描述 :
Safe and easy wrappers for RealmSwift
高级语言: Swift
项目地址: git://github.com/k-lpmg/RealmWrapper.git
创建时间: 2018-06-10T00:51:29Z
项目社区:https://github.com/k-lpmg/RealmWrapper

开源协议:MIT License

下载


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
```swift
@objcMembers
class User: Object {
dynamic var id: String?
dynamic var name: String?

  1. override static func primaryKey() -> String? {
  2. return "id"
  3. }

}

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

  1. > Using RealmSwift
  2. ```swift
  3. let realm = try! Realm(configuration: Realm.Configuration(fileURL: URL(fileURLWithPath: RLMRealmPathForFile("user.realm")), schemaVersion: 1, objectTypes: [User.self]))
  4. try! realm.write {
  5. realm.add(user)
  6. }

Using RealmWrapper

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

Threading

  • By default, you can use the transaction function to process a Realm Transaction in MainThread

    1. UserRealmManager().transaction(writeHandler: { (realm) in
    2. realm.add(user)
    3. })
  • It can be implemented by a background thread using DispatchQueue and isSync parameters.

    1. UserRealmManager().transaction(isSync: false, writeHandler: { (realm) in
    2. realm.add(user)
    3. })
  1. UserRealmManager().transaction(writeQueue: DispatchQueue(label: "background"), isSync: false, writeHandler: { (realm) in
  2. realm.add(user)
  3. })
  • You can add completion closure.
    1. UserRealmManager().transaction(isSync: false, writeHandler: { (realm) in
    2. realm.add(user)
    3. }) { (realm, error) in
    4. self.tableView.reloadData()
    5. }

Getting Started

  1. Create a RealmManager that manages one realm file.
  1. final class UserRealmManager: RealmManageable {
  2. var isUseInMemory: Bool {
  3. return false
  4. }
  5. var schemaVersion: UInt64 {
  6. return 1
  7. }
  8. var fileName: String {
  9. return "user"
  10. }
  11. var objectTypes: [Object.Type]? {
  12. return [User.self]
  13. }
  14. }
  1. Create a RealmProxy that is responsible for the CRUD function to be accessed by the Controller.
  1. struct UserRealmProxy<RealmManager: UserRealmManager>: RealmProxiable {
  2. var users: RealmQuery<User> {
  3. return query(sortProperty: "date", ordering: .ascending)
  4. }
  5. func append(_ user: User) {
  6. rm.transaction(writeHandler: { (realm) in
  7. realm.add(user, update: .all)
  8. })
  9. }
  10. func delete(_ user: User) {
  11. rm.transaction(writeHandler: { (realm) in
  12. realm.delete(user)
  13. })
  14. }
  15. func updateName(id: String, name: String, age: Int) {
  16. guard let user = userFromId(id) else {return}
  17. rm.transaction(writeHandler: { (realm) in
  18. user.name = name
  19. user.age = age
  20. realm.add(user, update: .all)
  21. })
  22. }
  23. func userFromId(_ id: String) -> User? {
  24. return query(filter: "id == '\(id)'").results.first
  25. }
  26. }
  27. var user = User()
  28. user.id = UUID().uuidString
  29. user.name = "Kevin"
  30. UserRealmProxy().append(user)
  31. UserRealmProxy().delete(user)
  32. 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.
  1. let users: RealmQuery<User> = UserRealmProxy().users
  2. users.setSection(0)
  3. .addInsertNotificationBlock(self) { (self, insertions) in
  4. self.tableView.reloadData()
  5. }
  6. .addModificateNotificationBlock(self) { (self, modifications) in
  7. self.tableView.reloadRows(at: modifications, with: .fade)
  8. }
  9. .addDeleteNotificationBlock(self) { (self, deletions) in
  10. self.tableView.deleteRows(at: deletions, with: .fade)
  11. }
  12. .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.

  1. public func addDeleteNotificationBlock<Object: AnyObject>(_ object: Object, block: @escaping (Object, [IndexPath]) -> Void) -> Self {
  2. deleteNotificationBlock = { [weak object] (deletions) in
  3. guard let weakObject = object else {return}
  4. block(weakObject, deletions)
  5. }
  6. return self
  7. }

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

    1. $ carthage update --platform iOS
  2. open RealmWrapper.xcodeproj

    1. $ open RealmWrapper.xcodeproj
  3. run RealmWrapperExample

Installation

CocoaPods (iOS 9+)

  1. platform :ios, '9.0'
  2. use_frameworks!
  3. target '<Your Target Name>' do
  4. pod 'RealmWrapper'
  5. end

Carthage (iOS 9+)

  1. github "k-lpmg/RealmWrapper"

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

  1. .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.