2

my question is on parse.com queries for Android and how to set a timeout if queries are taking too long to respond.

For example, I have a query where I am getting a list of strings from parse.com. If this query takes too long to be received from parse.com (say, ten seconds), I'd like the user to be able to cancel the query (with an on-screen pop-up, for example). Instead, the app crashes after 30+ seconds of waiting.

So, is there a way to set my own timeout for queries, then handle them appropriately?

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
h_k
  • 1,674
  • 1
  • 24
  • 46

3 Answers3

1

Https is the protocol for connections with parse.

Http(s) allows full control of the following:

Socket timeout

getConnection timeout

connectionRequest timeout

In order to manipulate the headers, i know that with parse.com you can use the Rest API and then do anything u want with the headers in the builder....

public void create(int method, final String url, final String data) {
    this.method = method;
    this.url = url;     
    this.data = data;
    if(method == GET){
        this.config = RequestConfig.custom()
            .setConnectTimeout(6 * 1000)
            .setConnectionRequestTimeout(30 * 1000)
            .setSocketTimeout(30 * 1000)                
            .build();
    } else{
        this.config = RequestConfig.custom()
                .setConnectTimeout(6 * 1000)
                .setConnectionRequestTimeout(30 * 1000)
                .setSocketTimeout(60 * 1000)                
                .build();           
    }
    this.context = HttpClientContext.create(); 

If you use only android sdk, then you will need docs at parse.com to figure out how ( or whether possible ) to set the http connection config listed above.

Robert Rowntree
  • 6,230
  • 2
  • 24
  • 43
1

My solution was to use RxJava Observable.timer(long delay, java.util.concurrent.TimeUnit unit) method.

I declare a RxJava Subscription field, and then init it to an Observable.timer(20, TimeUnit.SECONDS) call, just before any Parse.InBackground() code.

Inside the Observable.timer() method, I invoke another method that'll throw a Java Exception within a try{...} block, and then handle the thrown exception within the following catch {...} block. What this does is have the Observable.timer() call invoke the exception-throwing method as soon as the set time (e.g. 20 seconds in the example above) is exhausted. By handling it in the catch {...} block, you can show a dialog/alert informing user that the operation has timed out.

Here's a code snippet showing this:

Subscription timeoutWatcher;
public void loginWithEmailAndPassword(@NonNull String email, @NonNull String password) {
    timeoutWatcher = Observable.timer(10, TimeUnit.SECONDS).subscribe(aLong -> {
        // Login timed out. Notify user to check Internet connection is chooppy.
        throwNetworkException("Timeout! Your Internet connection is unstable and preventing your sign in from completing on time. Please try again.");
    });

    ParseUser.logInInBackground(email, password, (user, e) -> {
        // if Parse operation completes before timeout, then unsubscribe from the Observable.timer() operation
        timeoutWatcher.unsubscribe();
            if (user != null) {
                // Hooray! The user is logged in.

            } else {
                // Signup failed. Look at the ParseException to see what happened.
            }
        });
    }
}

private void throwNetworkException(String exceptionMessage) {
    try {
        throw new NetworkErrorException(exceptionMessage);
    } catch (NetworkErrorException e) {
        // show alert dialog
    }
}

Not the neatest piece of code, but it works for me.

Ugo
  • 587
  • 8
  • 12
0

Unfortunately, there is no way to specify a timeout for Parse requests.

Instead, you can catch the timeout exception & take necessary action

try{
   ...
}catch (ParseException e) {
    String mesg = e.getMessage();
    if(mesg!= null && mesg.contains("java.net.SocketTimeoutException")){
       // Do something here...      
    }
}

Note: If you are using inBackground Parse methods, then you need to check for the exception in the callback method.

satyadeepk
  • 351
  • 3
  • 9