Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Noe/db sql crash #145

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion components/XmtpEngine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { useAppStore } from "../data/store/appStore";
import { useSelect } from "../data/store/storeHelpers";
import { getTopicsData } from "../utils/api";
import { loadSavedNotificationMessagesToContext } from "../utils/notifications";
import {
dropXmtpClientsDbConnections,
reconnectXmtpClientsDbConnections,
} from "../utils/xmtpRN/client";
import { createPendingConversations } from "../utils/xmtpRN/conversations";
import { sendPendingMessages } from "../utils/xmtpRN/send";
import { syncXmtpClient } from "../utils/xmtpRN/sync";
Expand Down Expand Up @@ -72,10 +76,16 @@ export default function XmtpEngine() {
appState.current.match(/inactive|background/) &&
hydrationDone
) {
await reconnectXmtpClientsDbConnections();
loadSavedNotificationMessagesToContext();
if (isInternetReachableRef.current) {
syncAccounts(accounts);
}
} else if (
nextAppState.match(/inactive|background/) &&
appState.current === "active"
) {
await dropXmtpClientsDbConnections();
}
appState.current = nextAppState;
}
Expand Down Expand Up @@ -105,7 +115,10 @@ export default function XmtpEngine() {
const runningCron = useRef(false);

const xmtpCron = useCallback(async () => {
if (!useAppStore.getState().splashScreenHidden) {
if (
!useAppStore.getState().splashScreenHidden ||
AppState.currentState.match(/inactive|background/)
) {
return;
}
runningCron.current = true;
Expand Down
10 changes: 8 additions & 2 deletions data/db/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Platform } from "react-native";
import { AppState, Platform } from "react-native";
import RNFS from "react-native-fs";
import { Repository } from "typeorm/browser";

Expand Down Expand Up @@ -38,7 +38,13 @@ export const getRepository = async <T extends keyof RepositoriesForAccount>(
// init. This means methods that try to interact with the database too
// early will not fail but just take longer to execute!

while (!repositories[account]?.[entity]) {
// We also use the same mechanism to postpone writing to
// database if the app is in background

while (
!repositories[account]?.[entity] ||
AppState.currentState.match(/inactive|background/)
) {
console.log(`Database for ${account} not yet initialized`);
await new Promise((r) => setTimeout(r, 100));
}
Expand Down
24 changes: 24 additions & 0 deletions utils/xmtpRN/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,27 @@ export const isOnXmtp = async (address: string) =>
export const xmtpClientByAccount: {
[account: string]: ConverseXmtpClientType;
} = {};

// On iOS, it's important to stop writing to SQLite database
// when the app is going from BACKGROUNDED to SUSPENDED
// (see https://github.com/xmtp/xmtp-ios/issues/336)

// There are currently 2 SQLite databases:
// 1st one managed by Converse, created to store v2 XMTP data
// 2nd one managed by LibXMTP, created to store v3 XMTP data

// Let's just stop writing to both of them as soon as the app is BACKGROUNDED

export const dropXmtpClientsDbConnections = async () => {
await Promise.all(
Object.values(xmtpClientByAccount).map((c) =>
c.dropLocalDatabaseConnection()
)
);
};

export const reconnectXmtpClientsDbConnections = async () => {
await Promise.all(
Object.values(xmtpClientByAccount).map((c) => c.reconnectLocalDatabase())
);
};
4 changes: 4 additions & 0 deletions utils/xmtpRN/client.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ export const isOnXmtp = async (address: string) =>
Client.canMessage(getCleanAddress(address), {
env,
});

export const dropXmtpClientsDbConnections = async () => {};

export const reconnectXmtpClientsDbConnections = async () => {};
Loading