- iOS 8.0+
- Xcode 9.2 (Swift 4.0+)
Embedded frameworks require a minimum deployment target of iOS 8
CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:
$ 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
:
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'RealmS', '~> 4.0.0'
Then, run the following command:
$ pod install
Rule:
- Object has
primaryKey
must be StaticMappable (i) - Object has no
primaryKey
should be Mappable (ii)
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<Pet>()
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"]
}
}
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 setnil
please useNSNull()
instead.
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.