delight-im / Android-DDP

[UNMAINTAINED] Meteor's Distributed Data Protocol (DDP) for clients on Android
Apache License 2.0
274 stars 54 forks source link

Callback not Firing when subscribing after Method Call. #77

Closed camilomv17 closed 8 years ago

camilomv17 commented 8 years ago

Hi,

I'm trying to subscribe to a collection after calling a method that returns an specific ID, necessary for that subscription. The problem is that my callback is not firing OnDataAdded, but the subscription succeeds, I don't know Why. Here's my code:

    MeteorSingleton.getInstance().call("requestService", new Object[]{params}, new ResultListener() {

        public void onSuccess(final String s) {
            if(s!= null){
                        MeteorSingleton.getInstance().subscribe("singleService", new Object[]{s}, new    SubscribeListener() {
                            public void onSuccess() {
                            }

                            public void onError(String s, String s1, String s2) {

                            }
                        });
            }
        }
        public void onError(String s, String s1, String s2) {

        }
    });

Please Help. I'm Stuck here. Thanks a lot!

daupawar commented 8 years ago

Print onError and check what returns if you are publishing your collection with parameter then try to publish collection without parameter and check it works well.

camilomv17 commented 8 years ago

Hi,

Thanks for your response. I've printed errors but nothing happens there, also tried without parameters, and nothing. The method call is returning correctly the ID, also check that.

Curious thing is that if I call the subscription Outside in another part of code (Like Activity OnCreate or something) with hardcode parameter, it works perfectly.

camilomv17 commented 8 years ago

Hi,

I tried again without parameters, and it works well. What can be the problem with the parameter?

Thanks.

daupawar commented 8 years ago

That's good that your problem is solved What is your code for publish?

ocram commented 8 years ago

@daupawar Thanks a lot for helping out!

@camilomv17 Please try the code below instead of your code and report everything that is logged for Android-DDP#77 in your debug console:

System.out.println("Android-DDP#77: Calling `requestService` with parameter `"+params+"`");
MeteorSingleton.getInstance().call("requestService", new Object[] { params }, new ResultListener() {

    @Override
    public void onSuccess(final String s) {
        System.out.println("Android-DDP#77: `requestService` succeeded with result `"+s+"`");
        if (s != null) {
            System.out.println("Android-DDP#77: Subscribing to `singleService` with parameter `"+s+"`");
            MeteorSingleton.getInstance().subscribe("singleService", new Object[] { s }, new SubscribeListener() {

                @Override
                public void onSuccess() {
                    System.out.println("Android-DDP#77: `singleService` succeeded");
                }

                @Override
                public void onError(final String s, final String s1, final String s2) {
                    System.out.println("Android-DDP#77: `singleService` failed: "+s+", "+s1+", "+s2);
                }

            });
        }
    }

    @Override
    public void onError(final String s, final String s1, final String s2) {
        System.out.println("Android-DDP#77: `requestService` failed: "+s+", "+s1+", "+s2);
    }

});

Instead of System.out.println, you may use another debug solution, of course.

One thing, since you named your variable at the top params: This is only one parameter (e.g. a string or an integer), right? If you wanted to pass multiple parameters, you had to separate multiple variables by comma.

Another thing you can try is wrapping the whole MeteorSingleton.getInstance().subscribe(...) call inside the following:

new Handler().runOnUiThread(new Runnable() {

    @Override
    public void run() {
        // TODO code goes here
    }

});
camilomv17 commented 8 years ago

Hi Friends,

Finally I found the issue. Thanks a lot for your help! Maybe this could be helpful for you guys.

The problem was caused by the JSON Based communication of DDP. When I was getting the return value from the method, I wasn't parsing it as JSON, just taking the response as a String (Because I was returning just an ID of a recently created Mongo document), This response was received as JSON, like this: "\"the_id\"". the issue was the duplicated "". I simply take them out from the id and I worked.

ocram commented 8 years ago

@camilomv17 Thanks a lot for the explanation! That's exactly the same issue as described in https://github.com/delight-im/Android-DDP/issues/71 -- we will definitely do something about it!