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

is there any way to know when onDataAdded get the last document? #65

Closed cristianhoyos66-zz closed 8 years ago

cristianhoyos66-zz commented 8 years ago

I'm sorry if this is a dumb question, but I don't get with a good solution.

I'm doing this:

@Override
     public void onDataAdded(String collectionName, String documentID, String fieldsJson) {
         try {
             JSONObject obj = new JSONObject(fieldsJson);
              String title = obj.getString("title");
              service = new Service(title);
              serviceList.add(service);
              adapter = new ServicesAdapter(serviceList);
              rvPendingServices.setAdapter(adapter);
          } catch (JSONException e) {
              e.printStackTrace();
            }
       }

rvPendingServices is a RecyclerView that is setted with an adapter and one List. The problem here is that i'm calling several times rvPendingServices.setAdapter(adapter); because I dont know when onDataAdded get the last document on fieldsJson var.

I'm also looking for a way that I can get collectionSize at least, to do a counter and to compare collectionSize with counter and when these are equal to can call rvPendingServices.setAdapter(adapter); but I don't find anything

Thank you.

ocram commented 8 years ago

Thanks for your question!

Generally, I'm not sure if you really have to call new ServicesAdapter and rvPendingServices.setAdapter every time, regardless of this library. Often, you can just add items to your existing adapter or the list that backs the adapter and the UI will update automatically.

You only get data when you have subscribed first, right?

And thus the SubscribeListener should do what you need. Whenever you subscribe to data, that listener should be fired when the initial batch of data has been sent completely. The usage is as documented in the README:

mMeteor.subscribe("my-subscription", new Object[] { arg1, arg2 }, new SubscribeListener() { });

Can you try this?

cristianhoyos66-zz commented 8 years ago

Thanks for aswering,

I thought SubscribeListener but new SubscribeListener() { } has these methods:

mMeteor.subscribe("my-subscription", new Object[] { arg1, arg2 }, new SubscribeListener() { 
    @Override
     public void onSuccess() {
     }

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

and onSuccess does not return anything and on the documentatio I don't find how to get the first data with SubscribeListener

ocram commented 8 years ago

@cristianhoyos66 The idea was rather something like this:

private List<Service> mServiceList;
private ServicesAdapter mServicesAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    // ...

    mServiceList = new ArrayList<Service>();
    mServicesAdapter = new ServicesAdapter(mServiceList);
    rvPendingServices.setAdapter(mServicesAdapter);

    // subscribe to get the entries
    mMeteor.subscribe("my-subscription", new Object[] { arg1, arg2 }, new SubscribeListener() {

        @Override
        public void onSuccess() {
            // all entries have been received and thus we can show them now
            mServicesAdapter.notifyDataSetChanged();
        }

        @Override
        public void onError(String s, String s1, String s2) {
            System.out.println("There was an error: "+s+", "+s1+", "+s2);
        }

    });

    // entries start being sent now and will be received in `onDataAdded` ...
}

@Override
public void onDataAdded(String collectionName, String documentID, String fieldsJson) {
    try {
        JSONObject obj = new JSONObject(fieldsJson);
        String title = obj.getString("title");

        Service service = new Service(title);
        mServiceList.add(service);
    }
    catch (JSONException e) {
        e.printStackTrace();
    }
}

Does that make sense?

cristianhoyos66-zz commented 8 years ago

@mwaclawek Thank you so much, you are helping me with this.