Order query for Azure Sync table - android

I am using Azure services for android.
I am using syncTable and i am getting data from the service.
But i want to order the coming data .
Anyone knows how to do it?
following is the code :
//fetching data from syncTable
mToDoTable= mClient.getSyncTable("ToDoItem",ToDoItem.class);
i am getting all data on button click like following :
private void initElementsWithListeners() {
btnTableData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toDoDao.getAllDataFromTable(mToDoTable);
}
});
but i want the data coming from the service should be ordered by "fieldName" for the sync table. how should i do that ?
Thanks in advance.

#swanandvaidya, According to the tutorial, you can create a Query for your MobileServiceSyncTable to order data by a specified field, please see the code below.
mToDoTable = mClient.getSyncTable("ToDoItem", ToDoItem.class);
// Offline Sync
final List<ToDoItem> results = refreshItemsFromMobileServiceTableSyncTable();
private List<ToDoItem> refreshItemsFromMobileServiceTableSyncTable() throws ExecutionException, InterruptedException {
//sync the data
sync().get();
Query query = QueryOperations.add()
.orderBy("<fieldName>", QueryOrder.Ascending);
// or using `QueryOrder.Descending`
return mToDoTable.read(query).get();
}

Related

Check if firebase query (for firebase options) returns empty (does not exist)

I am using a filter query for a firebaseRecyclerAdapter (firebaseOptions).
Since I am using a searchView the recycler updates with every typed letter, if the query exists.
The problem is that I would like to clear the recycler if the query does not exist.
How can I add a check to see if the query is successful or not?
I am implementing a search, if the query exists I populate the recyclerview, if the query does not exist I want to clear the recyclerview.
public void fireSearch(String queryInput) {
String start = queryInput.toUpperCase(), end = queryInput.toLowerCase() + "\uf8ff";
Log.d("myTag", start + " " + end);
firebaseQuery = databaseReference.orderByKey().startAt(start).endAt(end);
searchRecyclerView.setVisibility(View.VISIBLE);
FirebaseRecyclerOptions<BusinessDetails> options =
new FirebaseRecyclerOptions.Builder<BusinessDetails>()
.setQuery(firebaseQuery, BusinessDetails.class)
.setLifecycleOwner(this)
.build();
}
If the query has no results, the FirebaseRecyclerViewAdapter will already clear the existing data. There is no need for you to handle this case yourself.
If you want to do some additional work when there is no data, you can override the onDataChanged method in your adapter class.
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(options) {
// ...
#Override
public void onDataChanged() {
// Called each time there is a new data snapshot. You may want to use this method
// to hide a loading spinner or check for the "no documents" state and update your UI.
// ...
}
#Override
public void onError(DatabaseError e) {
// Called when there is an error getting data. You may want to update
// your UI to display an error message to the user.
// ...
}
};
Also see the FirebaseUI documentation on data and error events, where I got the above code from.

Retrieve Data using Multiple keys Firebase Realtime Database

I have a list of Keys in a Array List . I want to retrieve data only from those keys but at a same time.
DatabaseReference mDBRef;
List<String> keys = new ArrayList<>();
I tried this with loop but the result coming in Model class is repeated 2 times.
for (int i= 0;i<keys.size();i++)
{
String id = keys.get(i);
Log.d("Keys",id);
mDBRef = FirebaseDatabase.getInstance().getReference().child("Gyms").child(id);
mDBRef.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(final DataSnapshot dataSnapshot) {
dataSnapshot.getKey();
gyms = dataSnapshot.getValue(Gyms.class);
if (gyms != null)
{
Log.d("Names",gyms.getName());
Toast.makeText(NearMeActivity.this, ""+ gyms.getName(), Toast.LENGTH_SHORT).show();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
The Firebase Realtime database doesn't support loading items based on an array of values. You'll have to load each item with a separate call.
While this may convolute your code, it is not nearly as slow as most developers think. This is because Firebase pipelines the requests. For a longer explanation of that, see my answer here: http://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786
firebase real time database doesn't support multiple matches. You can see Firestore which is NoSQL and provide some flexibility.
See Firestore: https://firebase.google.com/docs/firestore/
You have to use custom search solution like Elastic Search.

Receiving notification once all read operations are successfully performed

I have a question about some functionalities of Firestore. I am trying to occupy an array of data obtained through multiple read operations from the Firestore database. Is there a way for me to be notified when all the data are successfully read and stored in my array? This is particularly an issue because read operations are not finished in the order that they are called. Here are some code that illustrates my problem:
/* My array to insert the data read from the Firestore database */
String[] my_array = new String[3];
/* A method that will be called to initialize our array */
private void initArray(String doc_one, String doc_two, String doc_three) {
initSingleIndex(0, doc_one);
initSingleIndex(1, doc_two);
initSingleIndex(2, doc_three);
}
private void initSingleIndex(final int index, String doc_id) {
/* We perform our read operation here */
question_ref.document(doc_id).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
#Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
my_array[index] = documentSnapshot.getString("some_field");
}
});
}
My current implementation is to keep a global counter, which will be used to keep track of how many read operations were successfully carried out. I am also wondering whether the onSuccess() callbacks can be fired concurrently, since this will then lead to data corruption (i.e. the classic problem of incrementing values concurrently).
Any help or suggestion will be appreciated.

Where can I write Parse Cloud Code functions for android?

Is it possible to write a cloud code function directly into android studio? If not where could I write one? I can't find it on my parse dashboard
thanks
Cloud code is only written in the express module of your app inside cloud/main.js file, you can create cloud functions there and call them from your android app.
Example:
Parse.Cloud.define("getPosts", function(request, response){
var query = new Parse.Query("Posts");
//TODO: query constraints here
query.equalTo("key",request.params.text);
query.find().then(function(results){
response.success(results);
});
});
and you can call this function from android as below:
public static void getPosts(String text, final onSearch onSearch) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("text", text);
ParseCloud.callFunctionInBackground("getPosts", hashMap, new
FunctionCallback<List<Post>>() {
#Override
public void done(List<Post> object, ParseException e) {
//TODO: use search results...
}
});
}
you can see other cloud functions and parameters in the docs: Cloud Code Guide

FireBase Android Need 1 value saved under a single user

I have the following data structure on firebase for the user MF0qeRA4p7djfjgXxqwFOck3m6p02. I want to get the value of item3 to populate a single field into the User interface on an Android App. I have been looking through samples on Stackoverflow, but all I have found are outdated and do not work with the current version of firebase. I'm new to firebase completely and this is my first app on android. I've got the oncreate user method to populate the users email address and add the 4 item fields, but retrieving the data I'm completely lost and I am not sure where to even begin.
-Users
---MF0qeRA4p7djfjgXxqwFOck3m6p02
------item1:"1"
------item2:"2"
------item3:"3"
------item4:"4"
According to what I can identify is, you are facing problem retrieving data from this reference. Here is the code:
final DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("Users");
databaseReference.child("MF0qeRA4p7djfjgXxqwFOck3m6p02").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, Object> map=(Map<String, Object>)dataSnapshot.getValue();
String item3=(String)map.get("item3");
display(item3);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Hope this helps.
You can create a custom model and inside you can insert elements. Something like this:
public class Item {
private List<Object> ojects;
}
There you can save instance of Item on database. In this case you have more controll. Other case is to use push() method, that will generate a new encoded key, something like this:
mDatabase.child("items").push().put(new Object());

Categories

Resources