Checking if Realm is empty? - android

I'm trying to use Realm to store a local database of objects. The app checks if the current session is first load and if so populates the local database with an api call. But, if the database is not empty, I would like to use the data already available. To do this, I need to know whether the database is empty or not.
I found this issue on github, but they dont provide a workaround:
https://github.com/realm/realm-java/issues/766
So how should this be done?

If you scroll down that issue page, you can see Realm.isEmpty() is added. :)

Add a realm.isEmpty() method that returns true if there is no objects
in the Realm. It is just a utility method, but fits nicely with the
Realm object store abstraction.
use realm.isEmpty()

I used
if(realmResults.isEmpty()) {action...}
or
if(realmResults.isNullOrEmpty()) {action...]
there's false but action..
What's wrong!? ...T0T

Related

ObjectBox lazyList behaviour

I'm not sure I understand from the documentation how should I use the Lazy List.
What the different between findLazy() and findLazyCached() the function description is exactly the same.
Should I make a find() query first time and just then use findLazy()?
Example of using:
Box<FastCacheData> box = box.boxFor(FastCacheData.class);
LazyList<FastCacheData> build = box.query().build().findLazy();
What the different between findLazy() and findLazyCached() the function description is exactly the same.
They both return a LazyList, which will only load the member objects as they're each accessed. The difference between the two is that the cached version will cache the object so that further accesses won't result in extra loads - the non-cached version will load a fresh object every time.
Should I make a find() query first time and just then use findLazy()
It's a question of when you want the loading to happen. If you want the whole thing loaded when the find() call is made, use the find() call. Else if you want to defer the loading to when you access the data, use the findLazy() call.

Avoid deleting children objects - Realm

I have the following scenario:
One Dose has one or more Nutrients and when the dose is deleted I would like that the nutrients stay in database. Is there any annotation that allows to do that?
Thank in advance!
If you delete a Dose object, only the links get invalidated, but the Nutrient classes that belong to it stay in the database when you call dose.deleteFromRealm().
So basically that's actually the default use-case.

Fetch all objects having a specific empty RealmList property

Today I moved to Realm 0.83 and it is nice that we have null support but I have a problem.
I want to fetch all the stores that have empty products list inside. So far it worked if I used isNull() on the RealmQuery but since the update I get a crash like: Illegal Argument: RealmList is not nullable.
As it states in the crash, I cannot do this anymore because a RealmList is a Required field from now on so it can't be empty.. ok, that is nice but what can I use on the RealmQuery to fetch the models that I want?
Thank you!
The issue has been resolved by realm. You can now use isEmpty and isNotEmpty in the query builder for all RealmList properties.
Unfortunately, there is no option for doing that exact query anymore in 0.83.0. We think the improved isNull semantics are better, but it is very unfortunate that it is breaking current behaviour. I have created an issue for adding support back for this and hope to have it resolved very soon: https://github.com/realm/realm-java/issues/1601.
Right now you will have to work around it by manually iterating your data to find all objects that match your criteria.

SimpleDB - how long after insert until item is available to be read?

I have a Fragment, and once the user presses OK, an Item is added to my database and its ID is added to the ArrayAdapter. Immediately after, the adapter tries to draw the view, but the first time it tries to get its attributes, it returns a null HashMap (it gets drawn properly the following times).
Is there a way to make sure the item is in the table before trying to get its attributes?
Even putting the attribute retrieval into a while loop until it returns a not-null HashMap doesn't work so it doesn't look to be an issue of time.
You need to do Select or GetAttributes with ConsistentRead=true as Amazon SimpleDB supports two read consistency options: eventually consistent read and consistent read. Eventually consistent read is default. For more detail please refer doc. link
Try using AsynTask.
Add item to database in doInBackground.
Read it in postExecute.
You are done.

Run application/activity at first time

I need to recognize first launch of my application or activity.
At this time I need to get some information from server create local database and save info to it. What is the best way to do this?
Create any preferences for example FirstLaunch and set true \ false to it.
Check whether my database exists or not.
Something else?
PS. All server calls must be into one transaction. Ormlite supports transactions?
Thanks.
For the "create database at first run"-purpose, you should use an SQLiteOpenHelper, which offers you the onCreate()-method that is called when:
[...] the database is created for the first time.
The Database-file itself will be created for you (you don't have to do this manually). In this method, you can then perform actions like populating your database with standard entry's.
If you want to populate the database with informations you get from your server, there might be a problem when there is no Internet-connection available.
In this case, I would check if there is a connection available:
If there is, get your informations.
If not, show a Toast or some other notification to inform the user.
To determine if your Database has be populated with the standard entry's, you can use the database-version which is also provided by the SQLiteDatabase-class:
When you first create your Database-object, you call
SQLiteOpenHelpers constructor and pass it 0 as the Database
version.
If you successfully populated your database, you use
setVersion()-method to alter it to 1.
Later in the onOpen()-method, which is called when the
database is opened, you can check if the database was populated by
using the getVersion()-method.
If it is populated, call the super-method to open it.
If not, try populating it.
Further more, the getReadableDatabase() / getWritableDatabase()-methods should be called off the main-thread anyways because:
Database upgrade may take a long time, you should not call this method
from the application main thread, including from
ContentProvider.onCreate().
So getting the informations from the Internet can take place in the onCreate() and in the onOpen()-method (if it wasn't successful at the first try). You can (for example) use a Service to do this.
If you want to solve this problem with database:
Create database with MyDatabasaVersion table and store your version in a single row, for example db_version default value is 0. First time when the application starts you check the db_version if 0 you need to start the syncronisation, after it is finishing set the db_version to 1.
The easiest way should be sharedpreferences. you can call it everywhere form the application context and you can put boolean values in it.
Here are all Android storages.
you should try first option Create any preferences for example FirstLaunch and set true \ false to it.

Categories

Resources