Execute Realm Query against multiple model classes - android

I am using Realm database. I have an average of five models. I want to call all the models when I search. Is it possible?
ClassOne
ClassTwo
ClassThree
ClassFour
ClassFive
When querying is not one by one as follows.
RealmResult<ClassOne> list = realm.where(ClassOne.class).equelsTo("key", "a").findAll();
what kind of query I can make calls in all models?

you can't do this because every query return a RealmResuts of the type used on "Where"
The documentation says:
Returns a typed RealmQuery, which can be used to query for specific objects of this type
https://realm.io/docs/java/latest/api/io/realm/Realm.html#where-java.lang.Class-

Related

Firebase Firestore OR query (Android Studio)

I've been trying to find a query for almost 2 days now
I want to search id (current user id) from the document 4 fields (customer1,customer2,customer3,customer4)
Here is the firestore document picture
tried this query
final Query userQuery = collectionReference
.whereEqualTo("customer1",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer2",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer3",firebaseAuth.getInstance().getCurrentUser().getUid())
.whereEqualTo("customer4",firebaseAuth.getInstance().getCurrentUser().getUid());
but this only shows up if the current ID is present in all 4. Is there any easier way to do this.
You can do that by using a field that is an array containing the uids you want to test, and then applying array-contains on it. In your case:
In your case:
customer: [customer1, customer2, customer3, customer4]
collectionReference
.where("customer ", "array-contains", firebaseAuth.getInstance().getCurrentUser().getUid())
Firestore does not support logical OR queries among mulitple fields. So, what you're trying to do is not possible with a single query using the database structure you have now. You would have to perform multiple queries and merge the results in the client.
If you want to be able to use a single query, you will have to change your database. One option is to put all the customers into a single array field and use an array-contains query to find a customer in that field.

ORMLite auto-mapping of results to entity in custom query

I need to perform complex query with joins - seems to be too complex to use QueryBuilder, so I'll SQL my way around. I know however, that result of this query will be a list containing only a single entity type. Is there a way to leave mapping of results to ORMLite? Usually I parsed results myself, but in this case entity contains a lot of fields and I really don't want to go into parsing those from List<String[]>...
... that result of this query will be a list containing only a single entity type. Is there a way to leave mapping of results to ORMLite?
There certainly is. If you take a look at the "raw query" section of the documentation, you can see that it talks about the use of the RawRowMapper class. To quote from the docs:
You can also map the results into your own object by passing in a RawRowMapper object. This will call the mapping object with an array of strings and allow it to convert the strings into an object. The DAO provides a default RawRowMapper that can be gotten from orderDao.getRawRowMapper() that knows how to convert the string array into the object.
So for this you would call dao.queryRaw(...) with the RawRowMapper arg which maps from an array of strings. There are other dao.queryRaw(...) methods including:
queryRaw(String, DatabaseResultsMapper, String...) which allows you to map an object directly from the database results.
queryRaw(String, DataType[], RawRowObjectMapper, String...) which maps from an array of objects if you specify the result types.

Not know exception with firebase filters

This code:
var query: Query = store.collection(COLLECTION_USERS)
query = query.whereGreaterThanOrEqualTo("age", filter.startAge).whereLessThanOrEqualTo("age", filter.endAge)
query = query.whereGreaterThanOrEqualTo("start_date", filter.startDate).whereLessThanOrEqualTo("end_date", filter.endDate)
query = query.whereEqualTo("sex", filter.sex)
return query.get()
throw exception: java.lang.IllegalArgumentException: All where filters other than whereEqualTo() must be on the same field. But you have filters on 'age' and 'start_date' how to fix?
Official documentation of Firestore says that range filters on different fields are forbidden. So Firestore allows to chain multiple where() methods to create more specific queries but only on the same field.
To achieve what you want, you need to query your database twice, once for each fiter because you cannot use both methods in the same query.
Another way to make it happen would be to store a special flag that might fit the query, although in real world applications it will almost impossible to store every single way a user might query the data.

Firebase query implementation

If we query data from Firebase on a key for a child node, does it downloads the whole child node and filter data in the application or it downloads the query specific data? i.e. the filtered data
String myUserId = getUid();
Query myTopPostsQuery = databaseReference.child("user-posts").child(myUserId).orderByChild("starCount");
myTopPostsQuery.addChildEventListener(new ChildEventListener() {});
myTopPostsQuery will sort data according to starCount and I will receive data in the addChildEventListener() I want to know that whether this data is being filtered inside my app after receiving or my app downloads only the filtered data from the Firebase.
If you use a filter in your query you will download the query specific data without any operation executed on the client side.
Keep in mind that:
You can only use one order-by method at a time. Calling an order-by method multiple times in the same query throws an error.
You can combine multiple limit or range functions. For example, you can combine the startAt() and endAt() methods to limit the results to a specified range of values.
For any other info take a read here
You get the data already filtered. When you use a query, let say, limitToLast(10) you get only those 10 elements. This is happening also in your case with orderByChild("starCount"). In your SnanpShot will find only those filtered elements. Please visit official doc for more details.
Hope it helps.
Based on the code you pasted here, your query will just retrieve all the posts for the database path user-posts/<id> with an ordered manner which means that there is not filter. But still you will get back all the available posts under the path you are querying. It can be a "semi-filter" as it will find only the posts which include starCount field
The best thing is to filter during your query in order to retrieve back exactly what is needed and not everything as you are doing right now, imagine that this list of posts can be really big so you will have a big issue with performance later.
Read the following section here about sorting & filtering.
https://firebase.google.com/docs/database/admin/retrieve-data#orderbychild
Apart from that consider to add an index in userId field for speeding up your query.

Changing the order in a ListActivity

I'm writing a simple database app on Android. The main activity is just a list of products - a ListActivity using a CursorAdapter. How is the best way to go about changing the order of the list (is sort by Price or ProductID or something). I'm guessing I need to execute a new query to get a new cursor - or can I recycle things?
There are two options base specialized on your app:
How many record in your db ( If your database too large, new query may take long time)
Your app usual query to database to get data
New query is simple better if your database not too large and your app not usual query to get data. In otherwise try create custom adapter
A cursor adapter - perfect! That means you're getting the list through a query. Just change the sort order. All commands that perform queries have an optional "sort order" parameter.

Categories

Resources