From 0fd7892e40e84d1508b22bf4a2938aecc06c3f31 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Fri, 31 Jan 2020 23:25:28 +0100 Subject: [PATCH] Avoid loading all the Notes in one go This can sometimes lead to too many files being opened in parallel and then failing because there aren't any file descriptors left. This patch isn't perfect as given the directory structure we could still be loading too many in one go, but it's a quick workaround for now. The proper solution is to only load the Notes which need to be displayed instead of loading all of them at startup. --- lib/core/notes_folder.dart | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/core/notes_folder.dart b/lib/core/notes_folder.dart index 59ebff8d..873d7c88 100644 --- a/lib/core/notes_folder.dart +++ b/lib/core/notes_folder.dart @@ -96,18 +96,28 @@ class NotesFolder with ChangeNotifier implements Comparable { // FIXME: This asynchronously loads everything. Maybe it should just list them, and the individual _entities // should be loaded as required? - // FIXME: This loads everything in one go. In some cases there can be too many files open and we hit the limit. Future loadRecursively() async { + const maxParallel = 10; + var futures = []; + await load(); - _entities.forEach((e) { + for (var i = 0; i < _entities.length; i++) { + var e = _entities[i]; if (e.isFolder) { - e.folder.loadRecursively(); + var f = e.folder.loadRecursively(); + futures.add(f); } else { // FIXME: Collected all the Errors, and report them back, along with "WHY", and the contents of the Note // Each of these needs to be reported to crashlytics, as Note loading should never fail - e.note.load(); + var f = e.note.load(); + futures.add(f); } - }); + + if (futures.length >= maxParallel) { + await Future.wait(futures); + futures = []; + } + } } // FIXME: This should not reconstruct the Notes or NotesFolders once constructed.