/
Entity.swift
122 lines (106 loc) · 4.35 KB
/
Entity.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//
// Entity.swift
// Kinvey
//
// Created by Victor Barros on 2016-06-14.
// Copyright © 2016 Kinvey. All rights reserved.
//
import Foundation
import Realm
import RealmSwift
internal func StringFromClass(cls: AnyClass) -> String {
var className = NSStringFromClass(cls)
while className.hasPrefix("RLMStandalone_") || className.hasPrefix("RLMUnmanaged_") {
let classObj: AnyClass! = NSClassFromString(className)!
let superClass: AnyClass! = class_getSuperclass(classObj)
className = NSStringFromClass(superClass)
}
return className
}
/// Base class for entity classes that are mapped to a collection in Kinvey.
public class Entity: Object, Persistable, BuilderType {
/// Override this method and return the name of the collection for Kinvey.
public class func collectionName() -> String {
preconditionFailure("Method \(#function) must be overridden")
}
/// The `_id` property mapped in the Kinvey backend.
public dynamic var entityId: String?
/// The `_kmd` property mapped in the Kinvey backend.
public dynamic var metadata: Metadata?
/// The `_acl` property mapped in the Kinvey backend.
public dynamic var acl: Acl?
/// Constructor that validates if the map contains the required fields.
public required init?(_ map: Map) {
super.init()
}
/// Default Constructor.
public required init() {
super.init()
}
/**
WARNING: This is an internal initializer not intended for public use.
:nodoc:
*/
public required init(realm: RLMRealm, schema: RLMObjectSchema) {
super.init(realm: realm, schema: schema)
}
/**
WARNING: This is an internal initializer not intended for public use.
:nodoc:
*/
public required init(value: AnyObject, schema: RLMSchema) {
super.init(value: value, schema: schema)
}
/// Override this method to tell how to map your own objects.
public func propertyMapping(map: Map) {
entityId <- ("entityId", map[PersistableIdKey])
metadata <- ("metadata", map[PersistableMetadataKey])
acl <- ("acl", map[PersistableAclKey])
}
/**
WARNING: This is an internal initializer not intended for public use.
:nodoc:
*/
public override class func primaryKey() -> String? {
return entityIdProperty()
}
/**
WARNING: This is an internal initializer not intended for public use.
:nodoc:
*/
public override class func ignoredProperties() -> [String] {
var properties = [String]()
for property in ObjCRuntime.properties(self) {
if !(ObjCRuntime.type(property.1, isSubtypeOf: NSDate.self) ||
ObjCRuntime.type(property.1, isSubtypeOf: NSData.self) ||
ObjCRuntime.type(property.1, isSubtypeOf: NSString.self) ||
ObjCRuntime.type(property.1, isSubtypeOf: RLMObjectBase.self) ||
ObjCRuntime.type(property.1, isSubtypeOf: RLMOptionalBase.self) ||
ObjCRuntime.type(property.1, isSubtypeOf: RLMListBase.self) ||
ObjCRuntime.type(property.1, isSubtypeOf: RLMCollection.self))
{
properties.append(property.0)
}
}
return properties
}
/// This function is where all variable mappings should occur. It is executed by Mapper during the mapping (serialization and deserialization) process.
public func mapping(map: Map) {
let originalThread = NSThread.currentThread()
let runningMapping = originalThread.threadDictionary[KinveyMappingTypeKey] != nil
if runningMapping {
let operationQueue = NSOperationQueue()
operationQueue.name = "Kinvey Property Mapping"
operationQueue.maxConcurrentOperationCount = 1
operationQueue.addOperationWithBlock {
let className = StringFromClass(self.dynamicType)
NSThread.currentThread().threadDictionary[KinveyMappingTypeKey] = [className : [String : String]()]
self.propertyMapping(map)
originalThread.threadDictionary[KinveyMappingTypeKey] = NSThread.currentThread().threadDictionary[KinveyMappingTypeKey]
}
operationQueue.waitUntilAllOperationsAreFinished()
} else {
self.propertyMapping(map)
}
}
}