22
33import android .content .Context ;
44
5+ import androidx .annotation .NonNull ;
6+
57import com .android .volley .VolleyError ;
68import com .wordpress .rest .RestRequest ;
79
1416import org .wordpress .android .datasets .ReaderPostTable ;
1517import org .wordpress .android .datasets .ReaderTagTable ;
1618import org .wordpress .android .fluxc .store .AccountStore ;
19+ import org .wordpress .android .models .ReaderBlog ;
1720import org .wordpress .android .models .ReaderBlogList ;
1821import org .wordpress .android .models .ReaderTag ;
1922import org .wordpress .android .models .ReaderTagList ;
@@ -322,6 +325,11 @@ public void run() {
322325 ReaderBlogList serverBlogs = ReaderBlogList .fromJson (jsonObject );
323326 ReaderBlogList localBlogs = ReaderBlogTable .getFollowedBlogs ();
324327
328+ // This is required because under rare circumstances the server can return duplicates.
329+ // We could have modified the function isSameList to eliminate the length check,
330+ // but it's better to keep it separate since we aim to remove this check as soon as possible.
331+ removeDuplicateBlogs (serverBlogs );
332+
325333 if (!localBlogs .isSameList (serverBlogs )) {
326334 // always update the list of followed blogs if there are *any* changes between
327335 // server and local (including subscription count, description, etc.)
@@ -340,4 +348,24 @@ public void run() {
340348 }
341349 }.start ();
342350 }
351+
352+ /**
353+ * Remove duplicates from the input list.
354+ * Note that this method modifies the input list.
355+ *
356+ * @param blogList The list of blogs to remove duplicates from.
357+ */
358+ private void removeDuplicateBlogs (@ NonNull ReaderBlogList blogList ) {
359+ for (int i = 0 ; i < blogList .size (); i ++) {
360+ ReaderBlog outer = blogList .get (i );
361+ for (int j = blogList .size () - 1 ; j > i ; j --) {
362+ ReaderBlog inner = blogList .get (j );
363+ if (outer .blogId == inner .blogId ) {
364+ // If the 'id' property is the same,
365+ // remove the later object to avoid duplicates
366+ blogList .remove (j );
367+ }
368+ }
369+ }
370+ }
343371}
0 commit comments