Skip to content

Approach to Sync Local DB with Cloud - Need Help #1936

@Tholkappiar

Description

@Tholkappiar

I'm building a local-first chat app where data syncs one-way from the cloud to the client (data is only pulled from the cloud, not pushed back). The app has multiple related tables, and I'm concerned about how to manage syncing these tables without causing partial updates or redundant syncs.

Currently, I am syncing all tables every time something changes in the cloud (such as chat message updates). Is this an optimal approach, or should I consider a better solution?

I'm using a custom useEffect hook to handle the sync process, and I invoke this hook multiple times for each function that needs syncing. Here's the current implementation:

useEffect(() => {
    if (remoteData === undefined || isSyncingRef.current) return;

    const performSync = async () => {
        isSyncingRef.current = true;
        setSyncState({ isLoading: true, error: null });

        try {
            await syncData();  // Sync function that pulls all data , (synchronize function by watermelonDB)
            setSyncState({ isLoading: false, error: null });
        } catch (error) {
            setSyncState({
                isLoading: false,
                error:
                    error instanceof Error ? error.message : "Sync failed",
            });
        } finally {
            isSyncingRef.current = false;
        }
    };

    performSync();
}, [remoteData]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions