Skip to content
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

upsertAll() fails to save results while upsert() will #290

Open
timothyrobb opened this issue Feb 4, 2023 · 1 comment
Open

upsertAll() fails to save results while upsert() will #290

timothyrobb opened this issue Feb 4, 2023 · 1 comment

Comments

@timothyrobb
Copy link

Hi there, I'm a Rails developer just starting out with Flutter and your SQFEntity lib with a test project.

Using:

  sqfentity: ^2.3.0
  sqfentity_gen: ^2.3.0

I'm pulling in JSON data from an authenticated API via custom networking code, then simply trying to save that data into the db.

Here's the execution code with some comments:

    var json = await NetworkManager.authedGet('support_items');

    var test = await Task().select().toList();
    print("Tasks before upsert: ${test.length}");

    var tasks = await Task.fromMapList(json);
    print("Tasks to save: ${tasks.length}");
    //// Below properly saves records
    for (var task in tasks) {
      task.upsert();
    }
    //// Below doesn't change anything
    // var result = await Task().upsertAll(tasks, continueOnError: true);
    // print(result.commitResult?.join('\n'));
    // print(result);

    test = await Task().select().toList();
    print("Tasks after upsert: ${test.length}");

What I want/expected the correct code to be:

    var json = await NetworkManager.authedGet('support_items');

    var tasks = await Task.fromMapList(json);
    await Task().upsertAll(tasks);

The result using the commented out upsertAll() is the following:

flutter: Tasks before upsertAll: 0
flutter: Tasks to save: 28
flutter: null
flutter: Result: OK! Successful
flutter: Tasks after upsertAll: 0

And when using the for loop with upsert() I get:

flutter: Tasks before upsert: 0
flutter: Tasks to save: 28
flutter: Tasks after upsert: 28

Considering the result.commitResult is null when using upsertAll(), and it only gives these if it could create a new batch, it looks like this might be a problem with the batching?
Specifically, this conditional here: https://github.com/hhtokpinar/sqfEntity/blob/master/sqfentity/lib/sqfentity.dart#L399
It looks like if the batch couldn't be generated, it just returns a true result?

That doesn't seem correct to me. If it can't generate a new batch, then it should wait for that batch to finish before creating a new one. Or throw an exception. Or at the very least, return a failed result.

@timothyrobb
Copy link
Author

...well, after debugging this for the past four hours, it's of course only 10min after I post this issue that I find a workable solution.
However, the root bug is still there (reporting that it succeeded when it didn't do anything).

When I extracted the above code out of my method I was calling from initState(), it works.
i.e:

// NetworkManager
...
  static Future<bool> updateSupportItems() async {
    var json = await NetworkManager.authedGet('support_items');

    var tasks = await Task.fromMapList(json);
    var result = await Task().upsertAll(tasks);
    return result.success;
  }
...

// Widget
...
  void fetchItems() async {
    setState(() => _loading = true);
    var test = await Task().select().toList();
    print("Tasks before upsert: ${test.length}");

    await NetworkManager.updateSupportItems();

    test = await Task().select().toList();
    print("Tasks after upsert: ${test.length}");

    updateItems();
  }

  @override
  void initState() {
    super.initState();
    updateItems();
    fetchItems();
  }
...
flutter: Tasks before upsert: 0
flutter: Tasks after upsert: 28

🤷‍♂️
No idea why it couldn't create a batch where it originally was, but should definitely get some sort of error feedback from that, rather than claiming it succeeded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant