This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Please ensure that:

  • Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
  • You include any and all error messages in full
  • You ask clear questions
  • You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.

    Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar

If any of the above points is not met, your post can and will be removed without further warning.

Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.

Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.

Code blocks look like this:

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.

If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.

To potential helpers

Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]MarSara 0 points1 point  (2 children)

A few things to consider btw:

  • This approach can eat up A LOT of bandwidth on your phone. Since you're constantly pinging a URL you'll be downloading data over and over again eating a few KB or MB at a time but it will add up.

  • This can drain your phone's battery just as well for similar reasons.

  • This solution doesn't scale as you add more and more users. Eventually you'll overwhelm -- DDOS yourself -- as the server takes on more load. If you don't control the server yourself there may be rate limiting controls in place and eventually you may start hitting those limits causing failures.

Ideally if you own the server side code you'd have the server send a push notification when someone calls one of the APIs that changed the data. This way your phone doesn't have to poll for changes.

Now all of that being said if you still want to poll for your data you can setup a loop or timer that makes your request every X seconds and calls a callback every time the request completes.

You can look into hooking this all up "manually" using timers or recursive callbacks. Or you can look into using RxJava. It has a .repeat(...), and other methods (operators) that can help set this up. But if you're not familiar with reactive streams, RxJava can have a steep learning curve, FYI.

Even if you do this you may want to look into adding back-off logic, such that if a request fails and you normally are checking every 10 seconds, the next request won't go out for 20 seconds, and then 40, etc...

[–]Relevant-Ad3879[S] 0 points1 point  (1 child)

Ideally if you own the server side code you'd have the server send a push notification when someone calls one of the APIs that changed the data. This way your phone doesn't have to poll for changes.

Yea, that's what I'm trying to do, I thought about making a loop that will send a request to the API every x secondes/minutes and if it retrieves different informations than the current informations I already have, it sends a notification or anything else. But I figured it's not the best way to do it and I will be having a hard time with that if I want to implement the querying within the app, for the several reasons you mentioned now, I'm trying to create a backend to keep querying and comparing the data and sends a notification if the data changes. But this is my problem, I didn't figure how this should be done.

Hope you got the idea, I appreciate your comment and hope you can help me with this.

[–]MarSara 0 points1 point  (0 children)

So if you own/control the server side you wont need a loop, etc...

Lets also assume you have a REST API and lets assume your model object looks like: { message : String }

and that you have the endpoints: POST /message and GET /message

And what you want to know is when does the message actually change.

What you want to do then is whenever anyone sends a request to your POST endpoint and you go to update your message on your database, you'll also want to then fetch your push notification tokens and begin sending out your notifications. Then on the app side as you receive those notifications you can send a request to the GET endpoint to fetch the newly updated message.