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

Implement support for request priorities #666

Merged
merged 1 commit into from
Sep 23, 2014
Merged

Implement support for request priorities #666

merged 1 commit into from
Sep 23, 2014

Conversation

lucasr
Copy link
Contributor

@lucasr lucasr commented Sep 23, 2014

Not sure if you've considered something like this in the past but here it is. This patch implements request priority support in Picasso. This feature can be especially handy when you have UIs with lots of images and you want to have tighter control over which images should load first. This is inspired by Volley's API.

It adds a new API in RequestCreator to define the request priority:

Picasso.with(context)
       .load("http://example.com/image.jpg")
       .priority(HIGH)
       .into(someImageView);

Priority can be LOW, NORMAL, HIGH, or IMMEDIATE. BitmapHunters compute their priority in the executor by taking the highest priority of its attached requests. For example, if a BitmapHunter has two attached Actions, one LOW and and one HIGH priority, its priority will be HIGH.

@JakeWharton
Copy link
Collaborator

We have, but I think those might be too many priorities. This is the same problem as log levels. Everyone wants a lower log level or a higher log level because they abuse them.

I wanted only two, but I can probably be convinced for three. I don't think I can be convinced of four.

@@ -69,6 +74,7 @@

BitmapHunter(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats, Action action,
RequestHandler requestHandler) {
this.sequence = SEQUENCE_GENERATOR.addAndGet(1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

incrementAndGet()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@lucasr
Copy link
Contributor Author

lucasr commented Sep 23, 2014

@JakeWharton Fair enough. Let's start with just LOW, NORMAL, HIGH then. I think using only two priorities feels too "polarizing". Three gives enough flexibility for more complex cases.

@lucasr
Copy link
Contributor Author

lucasr commented Sep 23, 2014

Updated branch:

  • Update BitmapHunter priority when requests get attached/detached. getPriority() is now simple getter.
  • Moar tests.
  • Moar API docs.

@JakeWharton
Copy link
Collaborator

👍 👍 👍

One of the more challenging things I'd like to figure out is a way to limit the number of threads on which LOW priority requests can execute. Something like half the threads (and then rounded down). I'd like to be able to execute 100 fetch() requests, for example, but still allow NORMAL and HIGH requests to start without having to wait.

JakeWharton added a commit that referenced this pull request Sep 23, 2014
Implement support for request priorities
@JakeWharton JakeWharton merged commit c21d17f into square:master Sep 23, 2014
@felipecsl
Copy link

👍 well done @lucasr

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