0
votes

There's more than 15 items in my azure database table called Events.

I've tried to run most of the commands found on https://docs.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-android-how-to-use-client-library such as :

List<Events> results = eventsTable.execute().get()

and

List<Events> results = eventsTable.select("Events").execute().get();

and

List<Events> results = eventsTable.top(20).execute().get();

to return all the row items in the table. The queries seem to run on the last row of the table only and returns the last row or nothing at all when query is executed.

Though the ToDoItem Quickstart from Azure works perfectly with all the queries - which is odd.

Here's some of the code

ArrayList<Events> events = new ArrayLists<Events>();

private void EventsFromTable() {

    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
        @Override
        protected Void doInBackground(Void... params) {

            try {
                final List<Events> results = EventsTable.execute().get();

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        for (Events event : results) {
                            Events ev = new Events(event.getName(), event.getVenue(), event.getDate());
                            events.add(ev);
                            System.out.println("size is " +events.size());
                            <======This returns "size is 1"======>
                        }
                    }
                });
            } catch (final Exception e){
                createAndShowDialogFromTask(e, "Error");
            }

            return null;
        }

    };
    runAsyncTask(task);
}

Might any one know what the matter is?

Thanks

1

1 Answers

0
votes

According to your code, the variable events seems to be a public shared instance of ArraryList in your Android app, so I don't know whether exists the case which multiple threads access it concurrently. The implementation of ArrayList class is not synchronized, please see here.

So please use the code below instead of the code ArrayList<Events> events = new ArrayLists<Events>(); when you shared the variable between UI thread and data async task thread.

List<Events> events = Collections.synchronizedList(new ArrayLists<Events>());

And I think it's better for copying data retrieved from table via addAll method, not add method for each, as the code below.

@Override
public void run() {
    events.addAll(results);
}