Skip to content

Allow multiple find/count operations on queries. Expose fromNetwork and isRunning #661

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

Merged
merged 4 commits into from
Jun 1, 2017

Conversation

natario1
Copy link
Contributor

@natario1 natario1 commented May 8, 2017

This:

@natario1 natario1 changed the title Allow multiple find/count operation. Exposing fromNetwork and isRunning Allow multiple find/count operations on queries. Expose fromNetwork and isRunning May 8, 2017
@codecov
Copy link

codecov bot commented May 8, 2017

Codecov Report

Merging #661 into master will decrease coverage by 0.32%.
The diff coverage is 74.07%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #661      +/-   ##
============================================
- Coverage     52.88%   52.56%   -0.33%     
+ Complexity     1676     1673       -3     
============================================
  Files           131      131              
  Lines         10150    10106      -44     
  Branches       1408     1405       -3     
============================================
- Hits           5368     5312      -56     
- Misses         4340     4353      +13     
+ Partials        442      441       -1
Impacted Files Coverage Δ Complexity Δ
Parse/src/main/java/com/parse/ParseQuery.java 73.92% <74.07%> (-3.85%) 80 <9> (-3)
...se/src/main/java/com/parse/ParseKeyValueCache.java 45% <0%> (-2%) 16% <0%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ff7c712...9f4083a. Read the comment docs.

@natario1
Copy link
Contributor Author

Any thoughts about this?

@natario1 natario1 force-pushed the allow-multiple-find branch from 1edb3d4 to 83f1bd0 Compare May 16, 2017 12:42
/* package */ ParseQuery<T> fromNetwork() {
checkIfRunning();

public ParseQuery<T> fromNetwork() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, fromNetwork is just the opposite of fromPin?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Before this being exposed, if you had a ParseQuery and called query.fromPin() or query.fromLocalDatastore() there was no way to switch back to network from the same query object. This PR allows that and also allows calling find() multiple times from the same object.

}
}
}

/**
* Cancels the current network request (if one is running).
*/
//TODO (grantland): Deprecate and replace with CancellationTokens
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we do what this comment suggests? Deprecate and replace with cancellation tokens?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t know what that means.. We should overload the signature of find, findInBackground, count & countInBackground to allow a CancellationToken ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we would have it where each of those operations would return a cancellation token, which you could then use with .cancel(token) which would cancel that specific call. But, I think that goes beyond the scope here and can be addressed in a different PR if desired. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get your point, but find / count operations already have a return type, how would we pass back a CancellationToken the moment one calls find?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now the cancellation is handled by the SDK itself. I think this suggestion means using the CancellationTokenSource/CancellationToken to transfer the cancellation handler to Bolt. But it is a big work, bunch of related files need to be modified. Another PR to do this job is preferred.

https://github.com/BoltsFramework/Bolts-Android#cancelling-tasks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On top of that , if they wanted to deprecate query.cancel(), they probably meant to have the developer get / pass a CancellationToken and call source.cancel() so the token is notified. See also the comment here.

The same comment is left in ParseFile e.g. here. We might set up a PR that addresses this in both classes. But the only way I see this possible is overloading methods, like

CancellationTokenSource source = new CancellationTokenSource();
query.findInBackground(callback, /* nullable */ source.getToken());
// At any moment..
source.cancel();

It would be useful because ParseFile and with this PR ParseQuery allow multiple concurrent operations, so passing a token to each lets the developer cancel only one operation. If you think it’s needed I’m going to work on it (separately), with the plan

  • overload methods to accept a token
  • deprecate cancel() and have it fallback to cancelAll()
  • create cancelAll() that cancels all currently running task

So the developer either passes a cancellation token to a single operation or uses cancelAll if he doesn’t care.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be implemented internally first, and keep the developer's original behavior. Replace the Task<Void> cancellationToken in ParseQueryController with CancellationToken cancellationToken.

https://github.com/parse-community/Parse-SDK-Android/blob/master/Parse/src/main/java/com/parse/ParseQueryController.java

interface ParseQueryController {
  <T extends ParseObject> Task<List<T>> findAsync(ParseQuery.State<T> state, ParseUser user,
      CancellationToken cancellationToken);
  <T extends ParseObject> Task<Integer> countAsync(ParseQuery.State<T> state, ParseUser user,
      CancellationToken cancellationToken);
  <T extends ParseObject> Task<T> getFirstAsync(ParseQuery.State<T> state, ParseUser user,
      CancellationToken cancellationToken);
}

Then re-implement all the relevant files and pass cancellationToken to Bolt Task methods (ex. onSuccessTask).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I’ll do that just internally for queries and files then. 👍 going to open a separate PR though, do you think this can be merged?

@natario1 natario1 force-pushed the allow-multiple-find branch from 83f1bd0 to bc94bcc Compare May 17, 2017 10:04
@natario1
Copy link
Contributor Author

I made this consistent with ParseFile so it will be easy if later we want to implement CancellationTokens.

@hermanliang
Copy link
Contributor

Does ParseQuery need a method to reset constraints (ex. query.reset())?
Because some query constraints are nonreversible (ex. whereEqualTo(key, value)).
It probably helpful when multiple find queries using different constraints.

@natario1
Copy link
Contributor Author

That should clear all constraints (order, where etc.) just like new ParseQuery() ? It would be great to have it per-key, reset(key) to undo a key constraint (maybe revert like parse object) but I’m not sure it’s possible. I’ll take a look.

@hermanliang
Copy link
Contributor

Yeah, I just think about the concept of "query group". Developers can use a single ParseQuery instance to do multiple query tasks with different constraints concurrently. Then, they can cancel all tasks using query.cancel() at specific app life cycle (ex. onPause).

@natario1 natario1 merged commit e4e53b1 into parse-community:master Jun 1, 2017
@natario1 natario1 deleted the allow-multiple-find branch June 1, 2017 08:00
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

Successfully merging this pull request may close these issues.

3 participants