diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 472c9ea975..c4b1b1f8ff 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -4172,7 +4172,22 @@ class Core { if (chunk_size < 200 && (account.isGmail() || account.isOutlook())) chunk_size = 200; - List>> chunks = Helper.chunkList(ranges, chunk_size); + + List>> chunks = new ArrayList<>(); + + int s = 0; + List> r = new ArrayList<>(); + for (Pair range : ranges) { + long n = range.second - range.first + 1; + if (s + n > range_size) { + chunks.addAll(Helper.chunkList(r, chunk_size)); + s = 0; + r.clear(); + } + s += n; + r.add(range); + } + chunks.addAll(Helper.chunkList(r, chunk_size)); Log.i(folder.name + " executing uid fetch count=" + uids.size() + " ranges=" + ranges.size() + " chunks=" + chunks.size() + diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index 2032fbb75f..932a40da2f 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -3789,6 +3789,8 @@ public class Helper { } static List> chunkList(List list, int size) { + if (list == null || list.isEmpty()) + return new ArrayList<>(); List> result = new ArrayList<>(list.size() / size); for (int i = 0; i < list.size(); i += size) result.add(list.subList(i, i + size < list.size() ? i + size : list.size()));