Skip to content

Commit 2d88341

Browse files
committed
fix(examples): init network each time because it network data can be cleaned up on logout
1 parent dbd5ad2 commit 2d88341

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

examples/sport-tracker-app/src/SQLiteSync.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ export class SQLiteSync {
2222
this.db = db;
2323
}
2424

25+
/**
26+
*
27+
*/
28+
async initSQLiteSyncNetwork(): Promise<void> {
29+
console.log("SQLite Sync - init network...", import.meta.env.VITE_SQLITECLOUD_CONNECTION_STRING);
30+
await this.db.sqliteSyncInitNetwork(import.meta.env.VITE_SQLITECLOUD_CONNECTION_STRING);
31+
}
2532

2633
/**
2734
* Sets up SQLite Sync using Access Tokens authentication for a specific user.

examples/sport-tracker-app/src/components/UserLogin.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ const UserLogin: React.FC<UserLoginProps> = ({
104104
// If SQLite Sync is enabled, sync with cloud before refreshing
105105
if (sqliteSyncEnabled && sqliteSync && currentSession) {
106106
try {
107+
console.log("SQLite Sync - Starting init network...");
108+
await sqliteSync.initSQLiteSyncNetwork();
107109
await sqliteSync.setupWithToken(currentSession);
108110

109111
console.log("SQLite Sync - Starting sync...");

examples/sport-tracker-app/src/db/database.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ export class Database {
9393
return this.sendMessage("sqliteVersion");
9494
}
9595

96+
async sqliteSyncInitNetwork(connectionString: string): Promise<void> {
97+
return this.sendMessage("sqliteSyncInitNetwork", connectionString);
98+
}
99+
96100
async sqliteSyncSetToken(token: string): Promise<void> {
97101
return this.sendMessage("sqliteSyncSetToken", token);
98102
}

examples/sport-tracker-app/src/db/sqliteSyncOperations.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ export const getSqliteSyncOperations = (db: any) => ({
1414
return version;
1515
},
1616

17+
sqliteSyncInitNetwork(connectionString: string) {
18+
// Initialize SQLite Sync with the SQLite Cloud Connection String.
19+
// On the SQLite Cloud Dashboard, enable OffSync (SQLite Sync)
20+
// on the remote database and copy the Connection String.
21+
db.exec(
22+
`SELECT cloudsync_network_init('${connectionString}')`
23+
);
24+
console.log("SQLite Sync - init network done", connectionString);
25+
},
26+
1727
/** Authorize SQLite Sync with the user's Access Token. */
1828
sqliteSyncSetToken(token: string) {
1929
db.exec(`SELECT cloudsync_network_set_token('${token}')`);
@@ -29,6 +39,12 @@ export const getSqliteSyncOperations = (db: any) => ({
2939
*/
3040
sqliteSyncNetworkSync() {
3141
console.log("SQLite Sync - Starting sync...");
42+
db.exec({
43+
sql: "SELECT hex(cloudsync_siteid())",
44+
callback: (row: any) => {
45+
console.log("SQLite Sync - Device site_id: ", row[0]);
46+
},
47+
});
3248
db.exec("SELECT cloudsync_network_sync(1000, 2);");
3349
console.log("SQLite Sync - Sync completed");
3450
},
@@ -89,13 +105,4 @@ export const initSQLiteSync = (db: any) => {
89105
db.exec(`SELECT cloudsync_init('workouts');`);
90106
// ...or initialize all tables at once
91107
// db.exec('SELECT cloudsync_init("*");');
92-
93-
// Initialize SQLite Sync with the SQLite Cloud Connection String.
94-
// On the SQLite Cloud Dashboard, enable OffSync (SQLite Sync)
95-
// on the remote database and copy the Connection String.
96-
db.exec(
97-
`SELECT cloudsync_network_init('${
98-
import.meta.env.VITE_SQLITECLOUD_CONNECTION_STRING
99-
}')`
100-
);
101108
};

0 commit comments

Comments
 (0)