With realm 4.0, createQuery method from RealmQuery class is deprecated. Now, how can I encapsulate a query and pass it to another method?
realm.where(MyObject.class) returns a query.
But if you check the breaking changes, this is also mentioned there.
RealmQuery.createQuery(Realm, Class), RealmQuery.createDynamicQuery(DynamicRealm, String), RealmQuery.createQueryFromResult(RealmResults) and RealmQuery.createQueryFromList(RealmList) have been removed.
Use Realm.where(Class), DynamicRealm.where(String), RealmResults.where() and RealmList.where() instead.
Related
I am trying to use RoomDatabase in my Android App. And I am using LiveData to be able to refresh my changes automatically inside my fragment.
The first time I am running my app I am getting the data from the API, creating my RoomDatabase and storing my data.
The second time I run my app I want to check if my DataBase is not empty. But while using LiveData: the following code is returning null.
AppDatabase.getInstance(getContext()).getRecipeDao().getAllRecipes().getValue();
I have read that "if the response is an observable data type, such as Flowable or LiveData, Room watches all tables referenced in the query for invalidation".
How to check if my RoomDatabase has data or is empty?
So after implementing myself I found that you need to do a few things:
Make sure you have an Observer for changes to the LiveData
You need to call observeForever(Observer<T> observer) unless you are using a LiveCyclerOwner then use that instead with: observe (LifecycleOwner owner, Observer<T> observer)
Finally, there is an interesting note on getValue():
Returns the current value. Note that calling this method on a
background thread does not guarantee that the latest value set will be
received
So to reiterate, I think your approach does not work.
You will need to create some type of separate check rather than use a method that returns a LiveData class as noted since it does not guarantee the latest value set is received by calling getValue().
I would recommend something super simple in the end such as adding a new method to your Dao
#Query("SELECT * FROM recipes LIMIT 1")
Recipe getAnyRecipe();
and do this check looking for null to see if anything exists in the recipes table.
I'm trying to do something like:
val barcodes = arrayOf("123", "456", "789")
realm.where(Product::class.java).in("barcode", barcodes).findAll()
However "in" is a Kotlin function and I can't access the in(String filedName, String[] values) method of RealmQuery object.
Currently I have a java class that does the job and returns the result, but I was wondering is there a more graceful workaround for this?
As stated in the Escaping for Java identifiers that are keywords in Kotlin:
Some of the Kotlin keywords are valid identifiers in Java: in, object,
is, etc. If a Java library uses a Kotlin keyword for a method, you can
still call the method escaping it with the backtick (`) character
For example:
realm.where(Product::class.java).`in`("barcode", barcodes).findAll()
Im very statisfied with SugarOrm for Android, but I ran into an issue. I'm using it with GSON for Json serializations and I want to get rid of SugarRecord's id attribute. I know I should use #Table annotation and later exclude specific field from serialization using #Expose, but after annotating class with #Table I cannot use .save(), delete(),... methods on the object, as it is the case extending SugarRecord. I don't know how to persist objects using #Table annotation.
I found the documentation here very limited.
The document hasn't been updated for the annotation based persistence yet. The methods save(), delete() will be available as static methods on SugarRecord class.
So instead of doing this:
object.save()
You'd be doing this:
SugarRecord.save(object)
Check out some tests here to understand better.
https://github.com/satyan/sugar/tree/master/example/src/test/java/com/example/sugartest
I tried update(Object), update(PreparedUpdate), executeRaw(String), updateRaw(String), and even tried directly using the helper class then using getData(String query). But my table's foreign field is still not updated. Any thoughts why this is happening?
I found out that I am using the method update(Object) on other methods while passing an outdated instance of the object I am trying to update. So once I use the actual update(PreparedUpdate) method, I am reverting the object back to its original state as some other method is using update(outdated Object)
I want to get the name of an opened database, for which I have a reference via a SqliteOpenHelper. No problem with API level 14 (getDatabasename). But I need it to work with API level 10 (hard requirement--this is for a class I'm taking).
The only idea I've come up with so far is storing the database name myself for future use--either in the class in which I need it or as a member of a subclass of SqliteOpenHelper. Is there a better way? Thanks.
getDatabaseName() returns the exact same name that you provided in the SqliteOpenHelper constructor, so the best is probably to store it in a member variable of a subclass. It will be more flexible if later you have another project where you have the same problem.
but dont you already have the database name since your opening it and passing it to the constructor of SqliteOpenHelper ? So just subclass SqliteOpenHelper and create the method getDatabaseName() yourself and let it do what you want.
this seems to be more about object oriented principals. Let me know if you need code or if im mistaken.
I grepped the code . the function your looking for simply stores the variable you set in the constructor:
public String getDatabaseName() {
return mName;
}
so again make your own
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.2.1_r1.2/android/database/sqlite/SQLiteOpenHelper.java#SQLiteOpenHelper.getDatabaseName%28%29