Skip to content

Commit ad83c65

Browse files
authored
Merge pull request #219 from jwfing/master
fixed: #218
2 parents 91e5c69 + 98b4ccd commit ad83c65

File tree

2 files changed

+210
-0
lines changed

2 files changed

+210
-0
lines changed

core/src/main/java/cn/leancloud/LCObject.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,19 @@ public LCObject apply(LCObject LCObject) throws Exception {
14541454
} else {
14551455
LCObject.this.serverData.clear();
14561456
}
1457+
} else {
1458+
// remove cached attribute-value.
1459+
String[] includedAttrs = includeKeys.split(",");
1460+
for(String attr: includedAttrs) {
1461+
if (StringUtil.isEmpty(attr)) {
1462+
continue;
1463+
} else if (attr.indexOf(".") > 0) {
1464+
String firstAttr = attr.substring(0, attr.indexOf("."));
1465+
LCObject.this.serverData.remove(firstAttr);
1466+
} else {
1467+
LCObject.this.serverData.remove(attr);
1468+
}
1469+
}
14571470
}
14581471
LCObject.this.serverData.putAll(LCObject.serverData);
14591472
LCObject.this.onDataSynchronized();

core/src/test/java/cn/leancloud/LCObjectTest.java

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import junit.framework.Test;
99
import junit.framework.TestCase;
1010
import junit.framework.TestSuite;
11+
import org.jetbrains.annotations.NotNull;
1112

1213
import java.util.*;
1314
import java.util.concurrent.CountDownLatch;
@@ -66,6 +67,202 @@ public void onComplete() {
6667
assertTrue(testSucceed);
6768
}
6869

70+
public void testFetchRemovedAttr() throws Exception {
71+
final LCObject object = new LCObject("Student");
72+
object.put("name", "Automatic Tester");
73+
object.put("age", 18);
74+
object.put("grade", 9);
75+
object.saveInBackground().subscribe(new Observer<LCObject>() {
76+
public void onSubscribe(Disposable disposable) {
77+
78+
}
79+
80+
public void onNext(LCObject lcObject) {
81+
System.out.println("try to remove grade field.");
82+
LCObject tmpObj = LCObject.createWithoutData("Student", object.getObjectId());
83+
tmpObj.remove("grade");
84+
tmpObj.saveInBackground().subscribe(new Observer<LCObject>() {
85+
public void onSubscribe(Disposable disposable) {
86+
}
87+
88+
public void onNext(LCObject lcObject) {
89+
System.out.println("remove field finished.");
90+
object.fetchInBackground("grade").subscribe(new Observer<LCObject>() {
91+
@Override
92+
public void onSubscribe(@NotNull Disposable disposable) {
93+
94+
}
95+
96+
@Override
97+
public void onNext(@NotNull LCObject aObject) {
98+
testSucceed = aObject.get("grade") == null;
99+
if (!testSucceed) {
100+
latch.countDown();
101+
return;
102+
}
103+
object.deleteInBackground().subscribe(new Observer<LCNull>() {
104+
@Override
105+
public void onSubscribe(Disposable disposable) {
106+
107+
}
108+
109+
@Override
110+
public void onNext(LCNull LCNull) {
111+
testSucceed = true;
112+
latch.countDown();
113+
}
114+
115+
@Override
116+
public void onError(Throwable throwable) {
117+
latch.countDown();
118+
}
119+
120+
@Override
121+
public void onComplete() {
122+
123+
}
124+
});
125+
}
126+
127+
@Override
128+
public void onError(@NotNull Throwable throwable) {
129+
latch.countDown();
130+
}
131+
132+
@Override
133+
public void onComplete() {
134+
135+
}
136+
});
137+
}
138+
139+
public void onError(Throwable throwable) {
140+
latch.countDown();
141+
}
142+
143+
public void onComplete() {
144+
}
145+
});
146+
147+
}
148+
149+
public void onError(Throwable throwable) {
150+
latch.countDown();
151+
}
152+
153+
public void onComplete() {
154+
155+
}
156+
});
157+
latch.await();
158+
assertTrue(testSucceed);
159+
}
160+
161+
public void testFetchRemovedPointerAttr() throws Exception {
162+
final LCObject object = new LCObject("Student");
163+
object.put("name", "Automatic Tester");
164+
object.put("age", 18);
165+
object.put("grade", 9);
166+
final LCObject friend = new LCObject("Student");
167+
friend.put("name", "tom");
168+
object.put("friend", friend);
169+
object.saveInBackground().subscribe(new Observer<LCObject>() {
170+
public void onSubscribe(Disposable disposable) {
171+
172+
}
173+
174+
public void onNext(LCObject lcObject) {
175+
System.out.println("try to remove grade field.");
176+
LCObject tmpObj = LCObject.createWithoutData("Student", object.getObjectId());
177+
tmpObj.remove("grade");
178+
tmpObj.remove("friend");
179+
tmpObj.saveInBackground().subscribe(new Observer<LCObject>() {
180+
public void onSubscribe(Disposable disposable) {
181+
}
182+
183+
public void onNext(LCObject lcObject) {
184+
System.out.println("remove field finished.");
185+
object.fetchInBackground("grade,friend.name").subscribe(new Observer<LCObject>() {
186+
@Override
187+
public void onSubscribe(@NotNull Disposable disposable) {
188+
189+
}
190+
191+
@Override
192+
public void onNext(@NotNull LCObject aObject) {
193+
testSucceed = aObject.get("grade") == null;
194+
if (!testSucceed) {
195+
System.out.println("failed to remote grade attr");
196+
latch.countDown();
197+
return;
198+
}
199+
testSucceed = aObject.get("friend") == null;
200+
if (!testSucceed) {
201+
System.out.println("failed to remote friend attr");
202+
latch.countDown();
203+
return;
204+
}
205+
System.out.println("succeed to remote grade/friend attr");
206+
object.deleteInBackground().subscribe(new Observer<LCNull>() {
207+
@Override
208+
public void onSubscribe(Disposable disposable) {
209+
210+
}
211+
212+
@Override
213+
public void onNext(LCNull LCNull) {
214+
System.out.println("succeed to delete origin student object");
215+
friend.deleteInBackground().blockingFirst();
216+
testSucceed = true;
217+
latch.countDown();
218+
}
219+
220+
@Override
221+
public void onError(Throwable throwable) {
222+
latch.countDown();
223+
}
224+
225+
@Override
226+
public void onComplete() {
227+
228+
}
229+
});
230+
}
231+
232+
@Override
233+
public void onError(@NotNull Throwable throwable) {
234+
latch.countDown();
235+
}
236+
237+
@Override
238+
public void onComplete() {
239+
240+
}
241+
});
242+
}
243+
244+
public void onError(Throwable throwable) {
245+
latch.countDown();
246+
}
247+
248+
public void onComplete() {
249+
}
250+
});
251+
252+
}
253+
254+
public void onError(Throwable throwable) {
255+
latch.countDown();
256+
}
257+
258+
public void onComplete() {
259+
260+
}
261+
});
262+
latch.await();
263+
assertTrue(testSucceed);
264+
}
265+
69266
public void testPutNull() throws Exception {
70267
LCObject object = new LCObject("Student");
71268
object.put("name", "Automatic Tester");

0 commit comments

Comments
 (0)