8
8
*/
9
9
package com .parse ;
10
10
11
+ import java .lang .ref .WeakReference ;
11
12
import java .util .Collections ;
12
13
import java .util .HashSet ;
13
14
import java .util .Set ;
@@ -24,7 +25,13 @@ public class ParseRelation<T extends ParseObject> {
24
25
private final Object mutex = new Object ();
25
26
26
27
// The owning object of this ParseRelation.
27
- private ParseObject parent ;
28
+ private WeakReference <ParseObject > parent ;
29
+
30
+ // The object Id of the parent.
31
+ private String parentObjectId ;
32
+
33
+ // The classname of the parent to retrieve the parent ParseObject in case the parent is GC'ed.
34
+ private String parentClassName ;
28
35
29
36
// The key of the relation in the parent object.
30
37
private String key ;
@@ -36,13 +43,17 @@ public class ParseRelation<T extends ParseObject> {
36
43
private Set <ParseObject > knownObjects = new HashSet <>();
37
44
38
45
/* package */ ParseRelation (ParseObject parent , String key ) {
39
- this .parent = parent ;
46
+ this .parent = new WeakReference <>(parent );
47
+ this .parentObjectId = parent .getObjectId ();
48
+ this .parentClassName = parent .getClassName ();
40
49
this .key = key ;
41
50
this .targetClass = null ;
42
51
}
43
52
44
53
/* package */ ParseRelation (String targetClass ) {
45
54
this .parent = null ;
55
+ this .parentObjectId = null ;
56
+ this .parentClassName = null ;
46
57
this .key = null ;
47
58
this .targetClass = targetClass ;
48
59
}
@@ -52,6 +63,8 @@ public class ParseRelation<T extends ParseObject> {
52
63
*/
53
64
/* package */ ParseRelation (JSONObject jsonObject , ParseDecoder decoder ) {
54
65
this .parent = null ;
66
+ this .parentObjectId = null ;
67
+ this .parentClassName = null ;
55
68
this .key = null ;
56
69
this .targetClass = jsonObject .optString ("className" , null );
57
70
JSONArray objectsArray = jsonObject .optJSONArray ("objects" );
@@ -65,12 +78,14 @@ public class ParseRelation<T extends ParseObject> {
65
78
/* package */ void ensureParentAndKey (ParseObject someParent , String someKey ) {
66
79
synchronized (mutex ) {
67
80
if (parent == null ) {
68
- parent = someParent ;
81
+ parent = new WeakReference <>(someParent );
82
+ parentObjectId = someParent .getObjectId ();
83
+ parentClassName = someParent .getClassName ();
69
84
}
70
85
if (key == null ) {
71
86
key = someKey ;
72
87
}
73
- if (parent != someParent ) {
88
+ if (parent . get () != someParent ) {
74
89
throw new IllegalStateException (
75
90
"Internal error. One ParseRelation retrieved from two different ParseObjects." );
76
91
}
@@ -92,7 +107,7 @@ public void add(T object) {
92
107
ParseRelationOperation <T > operation =
93
108
new ParseRelationOperation <>(Collections .singleton (object ), null );
94
109
targetClass = operation .getTargetClass ();
95
- parent .performOperation (key , operation );
110
+ getParent () .performOperation (key , operation );
96
111
97
112
knownObjects .add (object );
98
113
}
@@ -109,7 +124,7 @@ public void remove(T object) {
109
124
ParseRelationOperation <T > operation =
110
125
new ParseRelationOperation <>(null , Collections .singleton (object ));
111
126
targetClass = operation .getTargetClass ();
112
- parent .performOperation (key , operation );
127
+ getParent () .performOperation (key , operation );
113
128
114
129
knownObjects .remove (object );
115
130
}
@@ -124,12 +139,12 @@ public ParseQuery<T> getQuery() {
124
139
synchronized (mutex ) {
125
140
ParseQuery .State .Builder <T > builder ;
126
141
if (targetClass == null ) {
127
- builder = new ParseQuery .State .Builder <T >(parent . getClassName () )
142
+ builder = new ParseQuery .State .Builder <T >(parentClassName )
128
143
.redirectClassNameForKey (key );
129
144
} else {
130
145
builder = new ParseQuery .State .Builder <>(targetClass );
131
146
}
132
- builder .whereRelatedTo (parent , key );
147
+ builder .whereRelatedTo (getParent () , key );
133
148
return new ParseQuery <>(builder );
134
149
}
135
150
}
@@ -193,7 +208,13 @@ public ParseQuery<T> getQuery() {
193
208
}
194
209
195
210
/* package for tests */ ParseObject getParent () {
196
- return parent ;
211
+ if (parent == null ){
212
+ return null ;
213
+ }
214
+ if (parent .get () == null ){
215
+ return ParseDecoder .get ().decodePointer (parentClassName , parentObjectId );
216
+ }
217
+ return parent .get ();
197
218
}
198
219
199
220
/* package for tests */ String getKey () {
0 commit comments