-
Notifications
You must be signed in to change notification settings - Fork 370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatically synchronize data to the server #2804
Comments
See also: #136, perhaps #635 or this talk for ideas.
I think triggers are well-suited for this. You don't need them for inserts (just set the CREATE TRIGGER tbl_update AFTER UPDATE ON tbl WHEN old.is_sync BEGIN
UPDATE tbl SET is_sync = false WHERE tbl.id = old.id;
END;
CREATE TRIGGER tbl_delete INSTEAD OF DELETE ON tbl WHEN NOT old.is_delete BEGIN
UPDATE tbl SET is_sync = false, is_delete = true WHERE tbl.id = old.id;
END; You can create these triggers in drift files, or manually by overriding
You could keep a separate table storing all pending changes and populate that with triggers. The benefit is that this avoids having to add a |
Thank you, I have some ideas now.👨🏻💻 |
Can I call the methods in Dart when the table data is modified? |
May I ask how to use TRIGGER? I couldn't find any relevant examples. |
I can use it now, Drift files are really convenient! 🥳🥳 void addWatcher() {
Stream<List<TodoItem>> stream = db
.select(
db.todoItems,
)
.where((tbl) => tbl.isSync.equals(false))
.watch();
subscriptionTodo = stream.listen((todos) {
uploadToServer(todos);
});
} I am using a timer to loop now, but I feel that this method is a bit lacking.
|
I've done it now, and it's okay to write it this way.👇 void addWatcher() {
Stream<List<TodoItem>> stream = (db
.select(
db.todoItems,
)
..where((tbl) => tbl.isSync.equals(false)))
.watch();
subscriptionTodo = stream.listen((todos) {
uploadToServer(todos);
});
} Do you have any good advice for syncing data to the server? 🤔 |
You either need to write a migration or re-install the app, yes.
First, I think you may want to avoid running multiple synchronization tasks at the same time: void addWatcher() {
Stream<List<TodoItem>> stream = (db
.select(
db.todoItems,
)
..where((tbl) => tbl.isSync.equals(false)))
.watch();
subscriptionTodo = stream.listen(null);
subscriptionTodo.onData((todos) async {
subscriptionTodo.pause();
try {
await uploadToServer(todos);
} finally {
subscriptionTodo.resume();
}
});
} Apart from that I do't reaally know how to give specific advice since this very much depends on the architecture you have in your server and which protocols you use. |
@simolus3 Thank you! |
Is your feature request related to a problem? Please describe.
My application requires a feature that can also be used offline, and Drift happens to meet my needs.
I currently have the following questions👇
Describe the solution you'd like
Take corresponding actions when there are changes to the table data, such as inserting, updating, and deleting.
To determine whether the data has been uploaded, my method is to add the "is_sync" root "is_delete" field in the table. When the data is uploaded to the server after creation and modification, the success rate is "is_sync=1". When deleting a piece of data, the success rate is "is_sync=0, is_delete=1". Then, I perform an API operation to delete it. Once successful, the data is directly deleted
The current challenge is how to watch the data changes in the table and take corresponding actions.
Can this function be achieved?
Or are there any other solutions?
I don't have a solution at the moment, I hope to get your help! 🙋
Thank you! 🥳
The text was updated successfully, but these errors were encountered: