In my application I'm inserting user like below
realm.beginTransaction();
realm.copyToRealmOrUpdate(user);
realm.commitTransaction();
I want to display and message if data inserted correctly. How to check if data insertion works successfully or not.
I know if I use realm.executeTransactionAsync() method I can get callback for onSuccess() and onError(). But It does not make any sense to insert one object asynchronously just to get access in to onSuccess().
The line realm.copyToRealmOrUpdate(user); will return an object of type user which is managed by realm signifying that it got inserted. If that object is null that would imply that the transaction did not succeed in which case it would have crashed with an exception.
Related
Problem:
I am using Room Persistence Library and so far everything is working fine except that there is a data from select query which I need synchronously as I am calling it from a Periodic Job (Work Manager's Worker). I have defined the return type to be LiveData as I am also accessing it for display purposes in UI and so observers are great for that but now I also need the same data in Job.
Code Snippet
#Query("SELECT * from readings ORDER BY date, time ASC")
LiveData<List<Reading>> getAllReadings();
Tried
I have tried the getValue() method in LiveData but it returns null as the data is not loaded in LiveData while making the query.
readingDao().getAllReadings().getValue() // returns null
Possible Solution
There is only one solution that I can think of which is to duplicate the getAllReadings query with a different name and return type (without LiveData) but I don't think this is a clean approach as it increases duplication of code just to get a synchronous return type.
Please let me know if there is any other solution or perhaps some way to synchronously access data from LiveData variable.
You can allow main thread query when you initialize Room DB, but it's clearly not desirable. This will give you the synchronous behavior but will block user interface. Is there a specific reason you want this to be synchronous?
The reason why getValue() is returning null is because Room is querying data asynchronously. You can attach an observer or a callback function to get result when the query is finished. You can display the result to the UI or chain another call for sequential operation etc from there.
I use RxJava to wrap my query request for asynchronous query but I you can also use AsyncTask.
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.
Let's say I have this object in db
{
value:60
status:PLAYING // could be PLAYING or FINISHED for simplicity
}
and I want to listen this object, or want to be notified when status becomes FINISHED
Currently following is not working
myRef.child("status").equalTo("PLAYING").addValueEventLisener...
it just triggers onDataChange regardless of status and returns snapshot value as null
On the other hand, if I omit PLAYING and use as following:
myRef.child("status").addValueEventLisener...
onDataChange is triggered as usual and snapshot value is not null
I have tried orderByChild it is not helping or I am doing things wrong.
That is an orderByValue() query:
myRef.child("status").orderByValue().equalTo("PLAYING")
In one of my parse subclasses I have a method like this:
public void updateCheckpoint(String checkpoint, boolean checked) {
if (checked) {
addUnique(checkedCheckpoints, checkpoint);
} else {
removeAll(checkedCheckpoints, Arrays.asList(checkpoint));
}
}
This update is immediately followed by a pinning to the local datastore.
Even when adding a ProgressDialog to ensure that the pinning has completed between updates, then sometimes the Operation is invalid after previous operation is thrown.
Is there a more robust way of doing array operations?
In the Parse documentation under arrays it says
"Note that it is not currently possible to atomically add and remove items from an array in the same save. You will have to call save in between every different kind of array operation."
When you both add and remove from your parse object, it will throw the error "Operation is invalid after previous operation". To avoid this error first add the new values to the array, save the object, remove the old values, then save again.
Initially, the value for an array on the dashboard reads (undefined). If you manually change it to (null) and try to add to it. It will throw this error.
I have started using Parse library recently for Android app. I want to store user contacts using saveEventually and then use the ParseRelation to relate the same to user. As there are multiple contacts mapped to user, I am using below code to handle my save functionality.
ParseRelation relation = ParseUser.getCurrentUser().getRelation(relationshipName);
for(int entityIndex = 0; entityIndex < entities.length;entityIndex++) {
...
entity[entityIndex].saveEventually(); relation.add(entity[entityIndex]);
...
}
ParseUser.getCurrentUser().saveEventually();
Here I am using saveEventually() for each valid entity (ParseObject) and then adding the same to relation. Later once all the objects are added to ParseRelation, at the end I am calling saveEventually() for ParseUser to store all the relationship to Parse.
Is this approach right? I am getting below exception at relation.add(entity[entityIndex]);
All objects in a relation must have object ids.
It seems this suggest some network connectivity issue and ParseRelation is not getting unique objectId for each ParseObject, but I was assuming that this saveEventuall() will handle this scenario well with ParseRelation
Kindly suggest. I am using Parse library version 1.1.11
Thanks.
Any object that's added to a ParseRelation must be saved first. The saveEventually call is non-blocking, so it's unlikely that the object will already have been saved by the time execution reaches the very next line when it's added to a ParseRelation.
Since you need to make sure the object is saved first, you should use saveInBackground instead of saveEventually. Then make sure to add the saved object to the relation from inside saveInBackground's SaveCallback. This will ensure that the object has been saved before being added to the relation.