In my application I try to synchronize books with the MBaaS Backendless. Therefor I generate a List to save all book titles inside after getting them from Backendless. I get the correct data from the Server (without duplicates) but my for-loop is starting too early, so there is no data to iterate through. How can I easy achieve to finish the filling of the List first before starting my for-loop? This is my code:
final List<String> all_books_without_duplicates = new ArrayList<String>();
// queryId = Id of the user
Backendless.Data.of(Books.class).find(queryId, newLoadingCallback<BackendlessCollection<Books>>(this, getString(R.string.loading_books), true) {
@Override
public void handleResponse(BackendlessCollection<Books> booksBackendlessCollection) {
Iterator<Books> booksIterator = booksBackendlessCollection.getCurrentPage().iterator();
while (booksIterator.hasNext()) {
Books booksonline = booksIterator.next();
final String book_title = booksonline.getBookTitle();
// avoid duplictes
if (!all_books_without_duplicates.contains(book_title)) {
all_books_without_duplicates.add(book_title);
Log.d("added book title:", book_title);
}}}});
Log.d("for-loop elements: ", String.valueOf(all_books_without_duplicates));
for (int x=0; x<all_books_without_duplicates.size(); x++){
final String book_title_value = all_books_without_duplicates.get(x);
String whereClause = "booktitle LIKE '%" + book_title_value + "%'";
QueryOptions queryOptions = new QueryOptions();
queryOptions.setRelated(Arrays.asList("book"));
BackendlessDataQuery query = new BackendlessDataQuery(queryOptions);
query.setWhereClause(whereClause);
// get all book-ids from Backendless where title is x
Backendless.Data.of(BookIds.class).find(query, new LoadingCallback<BackendlessCollection<BookIds>>(MainActivity.this, getString(R.string.loading_books), true) {
@Override
public void handleResponse(BackendlessCollection<BookIds> bookIDsBackendlessCollection) {
super.handleResponse(bookIDsBackendlessCollection);
Iterator<BookIds> bookIDsSyncIterator = bookIDsBackendlessCollection.getCurrentPage().iterator();
while (bookIDsSyncIterator.hasNext()){
BookIds book_ids = bookIDsSyncIterator.next();
String book_id_be = book_ids.getObjectId();
// get all book-ids from SQLite-DB
DatabaseHelper db=new DatabaseHelper(getApplicationContext());
final List<String>bookIds_sql = db.getAllBookIds();
// save new book in SQLite if it lacked so far
if(bookIds_sql.contains(book_id_be)){
// book already exists
}
else{
saveNewBookFromBackendless(book_id_be);
}
}}});}