@@ -13,7 +13,14 @@ public protocol CKRecordRecoverable {
13
13
}
14
14
15
15
extension CKRecordRecoverable where Self: Object {
16
- static func parseFromRecord< U: Object , V: Object , W: Object > ( record: CKRecord , realm: Realm , notificationToken: NotificationToken ? , uListType: U . Type , vListType: V . Type , wListType: W . Type ) -> Self ? {
16
+ static func parseFromRecord< U: Object , V: Object , W: Object > (
17
+ record: CKRecord ,
18
+ realm: Realm ,
19
+ notificationToken: NotificationToken ? ,
20
+ pendingUTypeRelationshipsWorker: PendingRelationshipsWorker < U > ,
21
+ pendingVTypeRelationshipsWorker: PendingRelationshipsWorker < V > ,
22
+ pendingWTypeRelationshipsWorker: PendingRelationshipsWorker < W >
23
+ ) -> Self ? {
17
24
let o = Self ( )
18
25
for prop in o. objectSchema. properties {
19
26
var recordValue : Any ?
@@ -57,66 +64,51 @@ extension CKRecordRecoverable where Self: Object {
57
64
recordValue = list
58
65
case . object:
59
66
guard let value = record. value ( forKey: prop. name) as? [ CKRecord . Reference ] else { break }
60
- // 这里应该根据 List 去创建一个 unmanaged 版本的 List,而不是使用 dynamicList 获取 managed 版本的 list
61
- // 另外需要考虑的就是类型问题
67
+
62
68
let uList = List < U > ( )
63
69
let vList = List < V > ( )
64
70
let wList = List < W > ( )
65
71
66
- // guard let list = o.value(forKey: prop.name) as? List<Object> else { break }
67
- // let list = o.dynamicList(prop.name)
68
72
for reference in value {
69
73
if let objectClassName = prop. objectClassName,
70
74
let schema = realm. schema. objectSchema. first ( where: { $0. className == objectClassName } ) ,
71
75
let primaryKeyValue = primaryKeyForRecordID ( recordID: reference. recordID, schema: schema) {
72
-
73
- // 其实在这里一个数组里所有的 className 都只会是一种
74
-
75
- if schema. className == uListType. className ( ) {
76
- if let existObject = realm. object ( ofType: uListType, forPrimaryKey: primaryKeyValue) {
76
+ if schema. className == U . className ( ) {
77
+ if let existObject = realm. object ( ofType: U . self, forPrimaryKey: primaryKeyValue) {
77
78
uList. append ( existObject)
78
79
} else {
79
- try ! realm. write {
80
- let object = realm. create ( uListType)
81
- uList. append ( object)
82
- }
80
+ pendingUTypeRelationshipsWorker. addToPendingListElement ( propertyName: prop. name, primaryKeyValue: primaryKeyValue)
83
81
}
84
82
}
85
83
86
- if schema. className == vListType . className ( ) {
87
- if let existObject = realm. object ( ofType: vListType , forPrimaryKey: primaryKeyValue) {
84
+ if schema. className == V . className ( ) {
85
+ if let existObject = realm. object ( ofType: V . self , forPrimaryKey: primaryKeyValue) {
88
86
vList. append ( existObject)
89
87
} else {
90
- try ! realm. write {
91
- let object = realm. create ( vListType)
92
- vList. append ( object)
93
- }
88
+ pendingVTypeRelationshipsWorker. addToPendingListElement ( propertyName: prop. name, primaryKeyValue: primaryKeyValue)
94
89
}
95
90
}
96
91
97
- if schema. className == wListType . className ( ) {
98
- if let existObject = realm. object ( ofType: wListType , forPrimaryKey: primaryKeyValue) {
92
+ if schema. className == W . className ( ) {
93
+ if let existObject = realm. object ( ofType: W . self , forPrimaryKey: primaryKeyValue) {
99
94
wList. append ( existObject)
100
95
} else {
101
- try ! realm. write {
102
- let object = realm. create ( wListType)
103
- wList. append ( object)
104
- }
96
+ pendingWTypeRelationshipsWorker. addToPendingListElement ( propertyName: prop. name, primaryKeyValue: primaryKeyValue)
105
97
}
106
98
}
107
99
108
100
}
109
101
}
110
102
111
- if prop. objectClassName == uListType . className ( ) {
103
+ if prop. objectClassName == U . className ( ) {
112
104
recordValue = uList
113
105
}
114
106
115
- if prop. objectClassName == vListType . className ( ) {
107
+ if prop. objectClassName == V . className ( ) {
116
108
recordValue = vList
117
109
}
118
110
119
- if prop. objectClassName == wListType . className ( ) {
111
+ if prop. objectClassName == W . className ( ) {
120
112
recordValue = wList
121
113
}
122
114
0 commit comments