https://github.com/blkbrds/realms-ios
Safe method for Realm
https://github.com/blkbrds/realms-ios
objectmapper realm realmswift
Last synced: 6 months ago
JSON representation
Safe method for Realm
- Host: GitHub
- URL: https://github.com/blkbrds/realms-ios
- Owner: blkbrds
- License: mit
- Created: 2016-01-10T14:57:10.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-12-09T06:30:50.000Z (almost 6 years ago)
- Last Synced: 2025-04-01T21:59:30.000Z (6 months ago)
- Topics: objectmapper, realm, realmswift
- Language: Swift
- Size: 233 KB
- Stars: 20
- Watchers: 9
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/tsrnd/realms-ios)
[](https://img.shields.io/cocoapods/v/RealmS.svg)
[](http://cocoadocs.org/docsets/RealmS)
[](https://codecov.io/github/tsrnd/realms-ios?branch=master)[](https://img.shields.io/badge/RealmSwift-~%3E%202.2-brightgreen.svg)
[](https://img.shields.io/badge/ObjectMapper-~%3E%202.2-brightgreen.svg)Realm + ObjectMapper
====================## Requirements
- iOS 8.0+
- Xcode 9.2 (Swift 4.0+)## Installation
> Embedded frameworks require a minimum deployment target of iOS 8### CocoaPods
[CocoaPods](http://cocoapods.org) is a dependency manager for Cocoa projects. You can install it with the following command:
```bash
$ gem install cocoapods
```> CocoaPods 1.2+ is required to build RealmS 2.3+
To integrate RealmS into your Xcode project using CocoaPods, specify it in your `Podfile`:
```ruby
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!pod 'RealmS', '~> 4.0.0'
```Then, run the following command:
```bash
$ pod install
```## Usage
### Mapping
**Rule:**
- Object has `primaryKey` must be StaticMappable (i)
- Object has no `primaryKey` should be Mappable (ii)```swift
import RealmSwift
import ObjectMapper
import RealmS// (i)
final class User: Object, StaticMappable {
@objc dynamic var id: String!
@objc dynamic var name: String?
@objc dynamic var address: Address?
let dogs = List()override class func primaryKey() -> String? {
return "id"
}func mapping(map: Map) {
name <- map["name"]
address <- map["address"]
dogs <- map["dogs"]
}static func objectForMapping(map: Map) -> BaseMappable? {
return RealmS().object(ofType: self, forMapping: map)
}
}// (ii)
final class Address: Object, Mappable {
@objc dynamic var street = ""
@objc dynamic var city = ""
@objc dynamic var country = ""@objc dynamic var phone: Phone?
let users = LinkingObjects(fromType: User.self, property: "address")
convenience required init?(map: Map) {
self.init()
}func mapping(map: Map) {
street <- map["street"]
city <- map["city"]
country <- map["country"]
phone <- map["phone"]
}
}
```### Import JSON to Realm
```swift
let realm = RealmS()
realm.write {
realm.map(User.self, jsUser) // map JSON object
realm.map(Shop.self, jsShops) // map JSON array
}
```> - `nil` value will be bypass, if you want set `nil` please use `NSNull()` instead.
### Clean Up
```swift
extension User {
override public class func relativedTypes() -> [Object.Type] {
return [Address.self, Pet.self]
}override public class func clean() { }
}extension Address {
override class func relativedTypes() -> [Object.Type] {
return [Phone.self]
}override class func clean() {
let realm = RealmS()
let objs = realm.objects(self).filter("users.@count = 0")
realm.write {
realm.delete(objs)
}
}
}
````Address` table will be clean-up after a `User` is deleted from Realm.