38

GitHub - arturdev/Unrealm: Unrealm is an extension on RealmCocoa, which enables...

 5 years ago
source link: https://github.com/arturdev/Unrealm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

README.md

Unrealm Header Logo



Swift 5.0 Build Status Version License Platform


Unrealm enables you to easily store Swift native Classes, Structs and Enums into Realm realmLogoSmall.png.
Stop inheriting from Object! Go for Protocol-Oriented programming!
Made with ❤️ by arturdev


preview.png


Features

Unrealm support the following types:

  • Swift Primitives
  • Swift Structs
  • Swift Classes
  • Swift Enums
  • Swift Arrays
  • Swift Dictionaries
  • Swift Optionals (String, Data, Date)
  • Nested Classes/Structs
  • Swift Optionals of primitives (Int, Double, etc..)

Example Project

To run the example project, clone the repo, and run pod install from the Example directory first.

Usage

All you have to do is

  1. Conform your Classes/Structs to Realmable protocol instead of inheriting from Object.
  2. Register your Classes/Structs in AppDelegate's didFinishLaunchingWithOptions.
Realm.registerRealmables(ToDoItem.self)

Thats it! Now you can store your Struct or Class object into Realm as usualy you do with Objc Classes.

Pros and Cons

Pros ?

  • Enables you to store Swift native types (Structs, Classes, Enums, Arrays, Dictionaries, etc...)
  • Getting rid of redundant inheriting from Object class
  • Getting rid of Realm crashes like "Object has been deleted or invalidated"
  • Getting rid of Realm crashes like "Realm accessed from incorrect thread"
  • Getting rid of boiletplate code such @objc dynamic var. Use just var or let

Cons ?

  • Losing "Live Objects" feature. Which means when you modify an object got from Realm the other ones will not be updated automatically. So after modifying an object you should manually update it in realm. f.e.;
let realm = try! Realm()
var todoItem = realm.object(ofType: ToDoItem.self, forPrimaryKey: "1")
todoItem.text = "Modified text"
try! realm.write {
    realm.add(todoItem, update: true) //<- force Realm to update the object
}

Installation

Unrealm is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Unrealm'

Author

arturdev, [email protected]

License

Unrealm is available under the MIT license. See the LICENSE file for more info.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK