generated from dellevin/template
同步优化2
This commit is contained in:
@@ -129,6 +129,7 @@ class _ServerSyncPageState extends State<ServerSyncPage> {
|
||||
if (value && _isActivated) {
|
||||
// 开启同步:合并本地与服务端数据
|
||||
await _prefs.setSyncEnabled(true);
|
||||
if (!mounted) return;
|
||||
setState(() => _syncEnabled = true);
|
||||
if (mounted) ToastUtil.show(context, '正在同步数据...');
|
||||
await ServerSyncService.instance.syncWithServer();
|
||||
@@ -142,6 +143,7 @@ class _ServerSyncPageState extends State<ServerSyncPage> {
|
||||
if (mounted) ToastUtil.show(context, '正在从服务器下载数据...');
|
||||
final success = await ServerSyncService.instance.downloadToLocal();
|
||||
await _prefs.setSyncEnabled(false);
|
||||
if (!mounted) return;
|
||||
setState(() => _syncEnabled = false);
|
||||
final provider = context.read<AppProvider>();
|
||||
await provider.loadMovies();
|
||||
|
||||
@@ -404,9 +404,14 @@ class ServerSyncService {
|
||||
|
||||
final dbPath = await DatabaseHelper.instance.databasePath;
|
||||
if (dbPath != null) {
|
||||
final dbFile = File(dbPath);
|
||||
// 先关掉旧连接,删除旧文件,再写入新数据库
|
||||
await DatabaseHelper.instance.close();
|
||||
await File(dbPath).writeAsBytes(dbResp.bodyBytes);
|
||||
await DatabaseHelper.instance.reopen();
|
||||
if (await dbFile.exists()) await dbFile.delete();
|
||||
await dbFile.writeAsBytes(dbResp.bodyBytes);
|
||||
// 设置正确的版本号
|
||||
final db = await openDatabase(dbPath, version: 14);
|
||||
await db.close();
|
||||
debugPrint('[Sync] 数据库已写入: $dbPath');
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user