Skip to content

Commit 62591c6

Browse files
committed
Remove duplicates from the server blogs list if local and remote doesn't match.
Remove duplicates from the server blogs list if local and remote doesn't match. This is required because under obscure circumstances the server can return duplicates. We could have modified the function isSameList to eliminate the length check, but it's better to keep it separate since we aim to remove this check as soon as possible.
1 parent 7f03846 commit 62591c6

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

WordPress/src/main/java/org/wordpress/android/ui/reader/services/update/ReaderUpdateLogic.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.wordpress.android.datasets.ReaderPostTable;
1515
import org.wordpress.android.datasets.ReaderTagTable;
1616
import org.wordpress.android.fluxc.store.AccountStore;
17+
import org.wordpress.android.models.ReaderBlog;
1718
import org.wordpress.android.models.ReaderBlogList;
1819
import org.wordpress.android.models.ReaderTag;
1920
import org.wordpress.android.models.ReaderTagList;
@@ -321,7 +322,13 @@ private void handleFollowedBlogsResponse(final JSONObject jsonObject) {
321322
public void run() {
322323
ReaderBlogList serverBlogs = ReaderBlogList.fromJson(jsonObject);
323324
ReaderBlogList localBlogs = ReaderBlogTable.getFollowedBlogs();
324-
325+
// Remove duplicates from the server blogs list only if local and remote lists don't match.
326+
if (serverBlogs.size() != localBlogs.size()) {
327+
// This is required because under rare circumstances the server can return duplicates.
328+
// We could have modified the function isSameList to eliminate the length check,
329+
// but it's better to keep it separate since we aim to remove this check as soon as possible.
330+
removeDuplicateFromServerResponse(serverBlogs);
331+
}
325332
if (!localBlogs.isSameList(serverBlogs)) {
326333
// always update the list of followed blogs if there are *any* changes between
327334
// server and local (including subscription count, description, etc.)
@@ -338,6 +345,20 @@ public void run() {
338345

339346
taskCompleted(UpdateTask.FOLLOWED_BLOGS);
340347
}
348+
/* This method remove duplicate ReaderBlog from list. */
349+
private void removeDuplicateFromServerResponse(ReaderBlogList serverBlogs) {
350+
for (int i = 0; i < serverBlogs.size(); i++) {
351+
ReaderBlog outer = serverBlogs.get(i);
352+
for (int j = serverBlogs.size() - 1; j > i; j--) {
353+
ReaderBlog inner = serverBlogs.get(j);
354+
if (outer.blogId == inner.blogId) {
355+
// If the 'id' property is the same,
356+
// remove the later object to avoid duplicates
357+
serverBlogs.remove(j);
358+
}
359+
}
360+
}
361+
}
341362
}.start();
342363
}
343364
}

0 commit comments

Comments
 (0)