androidparse-platformparse-serverparse-android-sdk

Problems with the ParseLiveQuery


I made a parse query and now I need to implement the live query. But my query has a List<ParseObject> and I can see that here in this liveQuery method it is different.

        public void onEvent(ParseQuery<ParseObject> query, final ParseObject object) {

ParseQuery is what this method has -- please refer to my previous post) to see that.

This is the code suggested in the documentation:

if (parseLiveQueryClient != null) {
ParseQuery<ParseObject> parseQuery = new ParseQuery("Message");
parseQuery.whereEqualTo("destination", "pokelist");
SubscriptionHandling<ParseObject> subscriptionHandling = parseLiveQueryClient.subscribe(parseQuery);

subscriptionHandling.handleEvent(SubscriptionHandling.Event.CREATE, new SubscriptionHandling.HandleEventCallback<ParseObject>() {
    @Override
    public void onEvent(ParseQuery<ParseObject> query, final ParseObject object) {
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            public void run() {
                EditText pokeText = findViewById(R.id.pokeText);
                numPokes++;
                if(numPokes == 1) {
                    pokeText.setText("Poked " + numPokes + " time.");
                }
                else {
                    pokeText.setText("Poked " + numPokes + " times.");
                }
            }
        });
    }
});}

i made this and did update my list of messages. Any suggestions? I'm new in parse and am pretty confused!

 ParseQuery<ParseObject> query = ParseQuery.getQuery("Conversaciones");
        queryChat = ParseObject.createWithoutData("Chat", idChatSeleccionadoAppUsuario);
        query.whereEqualTo("ChatId", queryChat);
        query.include("Usuario");
        query.include("ChatId");
        SubscriptionHandling<ParseObject> subscriptionHandling= App.parseLiveQueryClient.subscribe(query);
        subscriptionHandling.handleEvent(SubscriptionHandling.Event.CREATE, new SubscriptionHandling.HandleEventCallback<ParseObject>() {
            @Override
            public void onEvent(final ParseQuery<ParseObject> query, final ParseObject object) {
                Handler handler = new Handler(Looper.getMainLooper());
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        listaDeMensajes.add(object);
                    }

                });

                mMessageAdapter = new MessageListAdapter(getContext(), listaDeMensajes);
                mMessageRecycler.setAdapter(mMessageAdapter);
                queryFromChat();

            }
        });

Solution

  • I solved my problem this is how ...

    First make the subscription like this.

    private void  changeChatLive(){
        listaDeMensajes.clear();
        ParseQuery<ParseObject> query = ParseQuery.getQuery("Conversaciones");
        // Connect to Parse server
        SubscriptionHandling<ParseObject> subscriptionHandling = parseLiveQueryClient.subscribe(query);
    
        // Listen for CREATE events
        subscriptionHandling.handleEvent(SubscriptionHandling.Event.CREATE, new
                SubscriptionHandling.HandleEventCallback<ParseObject>() {
                    @Override
                    public void onEvent(ParseQuery<ParseObject> query, ParseObject object) {
    
                        Handler handler = new Handler(Looper.getMainLooper());
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                mMessageAdapter.notifyDataSetChanged();
                                mMessageRecycler.scrollToPosition(0);
                            }
    
                        });
                    }
                });
    }
    

    Then i called the method here and voila the liveChat running. NOTE:Call the method where you need your live query to update!.

                    queryConversaciones();
                    mMessageAdapter.notifyDataSetChanged();
                    **changeChatLive();**