fix: seed existing local data to cloud on first launch
Previously cloud sync only triggered on writes. If the user already had training records before cloud was enabled, they'd never be pushed — leaving the database empty until the next training session. Now onLaunch handles both cases: - Local empty → restore from cloud (recovery after deletion) - Local has data → check cloud; if cloud empty, push existing data up Also extract _restoreFromCloud to reduce nesting.
This commit is contained in:
@@ -24,10 +24,26 @@ App({
|
|||||||
|
|
||||||
this.globalData.theme = themeMod.getCurrentTheme()
|
this.globalData.theme = themeMod.getCurrentTheme()
|
||||||
|
|
||||||
// If local records are empty, try pulling from cloud
|
// Sync with cloud: pull on empty local, push on empty cloud
|
||||||
const records = wx.getStorageSync('training_records')
|
const records = wx.getStorageSync('training_records')
|
||||||
if (!records || Object.keys(records).length === 0) {
|
if (!records || Object.keys(records).length === 0) {
|
||||||
cloud.pullAll().then((cloudData) => {
|
// Case 1: Fresh install — restore from cloud
|
||||||
|
cloud.pullAll().then(cloudData => this._restoreFromCloud(cloudData))
|
||||||
|
} else {
|
||||||
|
// Case 2: Has local data — check if cloud needs seeding
|
||||||
|
cloud.pullAll().then(cloudData => {
|
||||||
|
if (!cloudData) {
|
||||||
|
// Cloud empty: push existing local data up
|
||||||
|
cloud.pushAll()
|
||||||
|
} else {
|
||||||
|
// Both have data — trust local (latest) and re-push
|
||||||
|
cloud.pushAll()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
_restoreFromCloud(cloudData) {
|
||||||
if (!cloudData) return
|
if (!cloudData) return
|
||||||
try {
|
try {
|
||||||
if (cloudData.records && Object.keys(cloudData.records).length > 0) {
|
if (cloudData.records && Object.keys(cloudData.records).length > 0) {
|
||||||
@@ -45,8 +61,6 @@ App({
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Cloud restore failed:', e)
|
console.error('Cloud restore failed:', e)
|
||||||
}
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
|
||||||
globalData: {
|
globalData: {
|
||||||
|
|||||||
Reference in New Issue
Block a user