Rokeder
Rokeder

Reputation: 23

How to get collection from Meteor server with Android DDP?

Let me to start explain my problem. There is repository with some explanations, but there are no methods how to get collection or json file from Meteor server(only insert). Also author did not explain properly methods onDataChanged, onDataAdded etc.

public class Login extends Activity implements MeteorCallback{

public static Meteor mMeteor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    mMeteor = new Meteor(this, "some_socket_it_doesn't_matter");
    mMeteor.setCallback(this);
}

}


public class ListOfElements extends ListFragment implements MeteorCallback{

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String subscriptionId = Login.mMeteor.subscribe("notifications");
    Log.d("Log", subscriptionId);
}

}

I didn't understand how i have to use subscription or how to get collection from server. Why there are only insert methods in github repository and no get? I really have no idea how make the code to get collection, use subscribe and so on. There are no any understandable explanations in the network. Please, can you help me with this by explaining how to realize getting, subscribing in this code.

Upvotes: 2

Views: 689

Answers (1)

caw
caw

Reputation: 31487

There are two special things about Meteor: It works asynchronously and it has been designed specifically for real-time applications. Thus it has a few different concepts for retrieving data and for some other tasks.

In a synchronous application, you would just call insert(...) and immediately get the method's return value, e.g. a boolean value for success/error or a numeric value for the number of rows that have been inserted.

You would call get(...) and immediately receive a collection of rows as the method's return value.

But in Meteor, everything is asynchronous. This means that you get the results not immediately, but a few (milli)seconds later, in a callback method.

When you call insert(...), this is not so important, as you have noticed. You just call this method and often forget about the result, i.e. you don't wait and check for the result because insertions are usually successful. But this method is still asynchronous and you could (and sometimes should) listen for the result which will arrive a few (milli)seconds later, again.

When you want to call get(...), this would be possible in theory, with the important point again being that it's asynchronous. So you would say "get me all chat messages from the last 5 minutes". There would be no result or return value, as usual, but the result would arrive a short time later, asynchronously, in a callback method that you define. This is what onDataAdded(...), onDataChanged(...) and onDataRemoved(...) are for.

Now it's not clear, yet, why you can't call get(...) and wait for data to arrive in those methods.

The answer to that question is Meteor being designed for real-time applications. This is why you can't say "get me all chat messages from the last 5 minutes". Instead, you have to say "I want to subscribe to all chat messages from the last 5 minutes and always be updated about changes".

So, in Meteor, you subscribe to data sets instead of requesting them via get(...).

All in all, this means the following:

  1. If you want to get some messages, you subscribe to your data set that holds those messages.
  2. When the initial rows are sent (!) and whenever new rows are added to the collection, you receive those in your onDataAdded(...) callback. When rows are modified, you receive those changes in your onDataChanged(...) callback. And, finally, when rows are deleted, you are informed about those deletions in your onDataRemoved(...) callback.
  3. When you don't want to get updates for your data set anymore, you unsubscribe from that set. This is optional.

With the Android-DDP library in your Android application, it translates to the following:

  1. final String subscriptionId = mMeteor.subscribe("chats");
  2. public void onDataAdded(String collection, String docID, String json) { ... }
  3. mMeteor.unsubscribe(subscriptionId);

As you can see, what you have to learn is really Meteor and not the library Android-DDP. Meteor has some new concepts that one has to understand. But when you know how Meteor works, translating those things to Android-DDP is really simple and only a matter of looking up the method names.

Upvotes: 1

Related Questions