Skip to content

Commit

Permalink
Merge pull request #145 from Unshut-Labs/noe/db-sql-crash
Browse files Browse the repository at this point in the history
Noe/db sql crash
  • Loading branch information
nmalzieu authored Jun 26, 2024
2 parents 6e29b0d + 667428f commit e94b40d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
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 () => {};

0 comments on commit e94b40d

Please sign in to comment.