Commit b238051
committed
Add sync mechanism to prevent background crashes and fix ndb reopen order
This adds a sync mechanism in Ndb.swift to coordinate certain usage of
nostrdb.c calls and the need to close nostrdb due to app lifecycle
requirements. Furthermore, it fixes the order of operations when
re-opening NostrDB, to avoid race conditions where a query uses an older
Ndb generation.
This sync mechanism allows multiple queries to happen simultaneously
(from the Swift-side), while preventing ndb from simultaneously closing
during such usages. It also does that while keeping the Ndb interface
sync and nonisolated, which keeps the API easy to use from
Swift/SwiftUI and allows for parallel operations to occur.
If Swift Actors were to be used (e.g. creating an NdbActor), the Ndb.swift
interface would change in such a way that it would propagate the need for
several changes throughout the codebase, including loading logic in
some ViewModels. Furthermore, it would likely decrease performance by
forcing Ndb.swift operations to run sequentially when they could run in
parallel.
A changelog is not needed since the background crashes were not widely
spread in the last public release.
Changelog-None
Closes: #3245
Signed-off-by: Daniel D’Aquino <[email protected]>1 parent 8c578ce commit b238051
File tree
4 files changed
+273
-43
lines changed- damus.xcodeproj
- nostrdb
4 files changed
+273
-43
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1668 | 1668 | | |
1669 | 1669 | | |
1670 | 1670 | | |
| 1671 | + | |
| 1672 | + | |
| 1673 | + | |
| 1674 | + | |
1671 | 1675 | | |
1672 | 1676 | | |
1673 | 1677 | | |
| |||
2767 | 2771 | | |
2768 | 2772 | | |
2769 | 2773 | | |
| 2774 | + | |
2770 | 2775 | | |
2771 | 2776 | | |
2772 | 2777 | | |
| |||
3299 | 3304 | | |
3300 | 3305 | | |
3301 | 3306 | | |
| 3307 | + | |
3302 | 3308 | | |
3303 | 3309 | | |
3304 | 3310 | | |
| |||
5740 | 5746 | | |
5741 | 5747 | | |
5742 | 5748 | | |
| 5749 | + | |
5743 | 5750 | | |
5744 | 5751 | | |
5745 | 5752 | | |
| |||
6781 | 6788 | | |
6782 | 6789 | | |
6783 | 6790 | | |
| 6791 | + | |
6784 | 6792 | | |
6785 | 6793 | | |
6786 | 6794 | | |
| |||
7206 | 7214 | | |
7207 | 7215 | | |
7208 | 7216 | | |
| 7217 | + | |
7209 | 7218 | | |
7210 | 7219 | | |
7211 | 7220 | | |
| |||
7470 | 7479 | | |
7471 | 7480 | | |
7472 | 7481 | | |
| 7482 | + | |
7473 | 7483 | | |
7474 | 7484 | | |
7475 | 7485 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
| 38 | + | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
| |||
158 | 160 | | |
159 | 161 | | |
160 | 162 | | |
| 163 | + | |
161 | 164 | | |
162 | 165 | | |
163 | 166 | | |
| |||
207 | 210 | | |
208 | 211 | | |
209 | 212 | | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
210 | 219 | | |
211 | 220 | | |
212 | 221 | | |
| |||
216 | 225 | | |
217 | 226 | | |
218 | 227 | | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
223 | 235 | | |
224 | 236 | | |
225 | 237 | | |
| |||
229 | 241 | | |
230 | 242 | | |
231 | 243 | | |
232 | | - | |
| 244 | + | |
| 245 | + | |
233 | 246 | | |
234 | | - | |
| 247 | + | |
235 | 248 | | |
236 | 249 | | |
237 | 250 | | |
| |||
628 | 641 | | |
629 | 642 | | |
630 | 643 | | |
631 | | - | |
632 | | - | |
633 | | - | |
634 | | - | |
635 | | - | |
636 | | - | |
637 | | - | |
638 | | - | |
639 | | - | |
640 | | - | |
641 | | - | |
642 | | - | |
643 | | - | |
644 | | - | |
645 | | - | |
646 | | - | |
647 | | - | |
648 | | - | |
649 | | - | |
650 | | - | |
651 | | - | |
652 | | - | |
653 | | - | |
654 | | - | |
655 | | - | |
656 | | - | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
657 | 673 | | |
658 | 674 | | |
659 | 675 | | |
| |||
1002 | 1018 | | |
1003 | 1019 | | |
1004 | 1020 | | |
1005 | | - | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
47 | 46 | | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
52 | | - | |
53 | | - | |
54 | | - | |
55 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
56 | 58 | | |
57 | 59 | | |
58 | 60 | | |
| |||
97 | 99 | | |
98 | 100 | | |
99 | 101 | | |
100 | | - | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
101 | 109 | | |
102 | 110 | | |
103 | 111 | | |
| |||
0 commit comments