I am making an android app and facing this similar issue I am sending data my SQL server using FCM which received in onMessage function in my app, but at the time of receiving the data I am not sure weather that specific screen exist or not? maybe user was on another screen at that time.
And along with this there will be number of different data objects for different screens? How can I make sure that data will properly update the existing data I know how to update data so please don't think about that, problem is how to call update functions of different screen which may or may not exist at that time.
I have spent more than 3 days on this solution but can't figure out. Please any good suggestion could prove a lot of help.
Related
I'm currently working on an app with React Native using Firebase as backend. I won't be too specific on the details, but, basically, the app has to constantly fetch for new data uploaded, so I added this part in my main code:
useEffect(() => {
db.ref('rooms').on('value', (data) => {
if (startRoom.id) {
ping = (data.val());
setRoom(() => (data.val())[startRoom.id]);
}
});
},[ping]);
This way, every time new data are added in the object "rooms" in my db, the app is able to update all variables linked to it. This process however can cause some huge slowdowns and performance issues: indeed, when there are a lot of data uploaded by different users, or when data are uploaded very quickly (let's say, uploads are handled by user clicks, so very quick clicks can cause the db to go nuts), the app is constantly refreshing the page even several times per second, causing the slowdowns I was talking about.
So my idea is: is there anyway I can limit the number of fetches the firebase ref method does over time? Like, if the database is uploaded seven times in a second, it only catches the last one, ignoring the uploads which, for example, happened in a very short time. In other words I wonder if there is a way to limit the "sensitivity" of the ref method or the useEffect method of React Native.
Thank you very much!
If updates are happening multiple times a second, I think you should change your approach.
I think you should instead of updating your app with the data the moment it changes, store the changes in to an array changes, and set one interval that run for every, says 3 seconds, check the changes array. If there is data in it, use it to update your app and then clear the array. This way you can make sure your app only update at a fixed rate even if there is a lot of changes
what's the data type of ping? If it's an object, your useEffect will run every time because of the way JavaScript checks for equality on objects (they check reference I believe).
A way around this is to use useDeepCompareEffect or lodash's isEqual. Or just use a primitive value as the dependency for your useEffect.
I want to achieve the following:
I receive a notification via FCM with a specific id in the data properties. I get the id both when my app is in foreground and in the background/closed...so far so good.
Now I want to do the following:
Make a volley request to e.g. /books/id to get the corresponding book informations.
Save those informations persistently in a Room database
Display them in a RecyclerView
Scenario 1: App with RecyclerView is in foreground:
As soon as I receive a new id via the FCM notification data, I want my RecyclerView to append another list item with the newly fetched book informations.
Scenario 2: App is in background - notification gets clicked:
When the notification gets clicked, the MainActivity with the RecyclerView gets loaded, showing the newly fetched book informations.
Scenario 3: App is in background - notification gets swiped away:
After some time the notification was swiped away the user re-opens or restarts the app. The MainActivity with the RecyclerView and the newly fetched book informations is shown.
My problem:
I don't quite know how to structure my code to accomplish this.
I tried making the volley request inside of FCM's onMessageReceived() but that resulted in an error and I don't know where and how I could do that otherwise. I tested the volley request in another project and it worked there.
Currently I try to learn how to work with the Room database.
I would really appreciate if someone can give me an insight of how to accomplish something like that. I really need to get this to work soon.
Thanks in advance! :)
EDIT 1:
I dont't get the error inside of the onMessageReceived anymore. I receive the correct response with the given book id.
Now I want to save these informations into the Room database and reflect the change in my RecyclerView. I think I'm going to use androids architecture components like here:
android architecture components example
I think that's exactly what I need. However, I still don't quite understand how to deal with it and structure my code accordingly.
Scenario 1: You may save it in DB, or just do broadcast and save it for instance in Activity then display a list.
Scenario 2: You should keep all display data in DB. After click on notification when RecyclerView is in background, you should handle the intent that pass through android.intent.category.LAUNCHER of intent-filter and put some info there to make a request and update the list.
Scenario 3: Finally, just do a request, save in DB, then display all in the list. This is a good approche, it takes extra time, but you always keep it updated.
Also read about efficient hot swap data with DiffUtils
I am developing an SMS app. I am getting messages from the Android Database every time user opens the App.
On a device with around 100 messages, it works fine. But with a device with 10,000 messages the app takes around 20 seconds to display the list after getting all the information.
To solve this, I am thinking about three possible options here:
Should I consider saving all the data to my own database? Will it make it faster?
Should I get some messages first, and then the rest in the background?
Any other option you might suggest.
Thanks in advance.
Below are the answers of your question
1. Should I consider saving all the data to my own database? Will it
make it faster?
No, don't do that. This will not make it faster. Same response you will get.
2. Should I get some messages first, and then the rest in the
background?
You can do this, but there is no need to do this to get all messages in background.Because User can see limited number of messages and chances are he will not see all messages down the bottom. So it will be useless to get those in background (untill or unless there is a business requirement)
Any other option you might suggest.
Yes, you need to implement pagination and need to keep track how many messages are needed to be loaded first time and while user scrolls then get more messages. In this case you need to keep track your own about how many messages you have already loaded and how many more you want to load. TO achive this, you will be required to implement PULL TO REFERESH mechanism of android. And a lot of tutorials can be found over the web.
Hope that answers your question.
So, I want to learn this synchronization strategy instead of just using the simpler MessageAPI, but am really struggling with how to successfully implement this.
My project is like this: I make queries to download a small amount of text from an API, via my phone. I will make these queries every so often, haven't really decided on how often just yet. The data will update the watch, which should hold onto the last data received. After that first download occurs, I send data using a DataMap, to the Android Watch. I only send that once, because I believe that sets up a channel to continually send updates when ready. If that is wrong, please correct me.
My main question is this: what if the Android phone's app closes? Then the data object goes to null, and gets sent to the Watch as null? Or, should I send an object from a long-running service or shared preferences on the Android phone, so that the object is never null?
Think of the Data Layer as more of an event system, i.e., you update your data and you're notified on the other side when the data is updated (created, changed, or deleted). You don't have to worry about if the Activity is killed after that. Even if the data was 'deleted', you would be notified it was deleted.
On the Wear device, you would listen for the changes via a Service or Activity and update UI, DB, etc. accordingly.
It probably make sense to read through this Android training guide. (It isn't too long.) The Handling Data Layer Events section is probably the most useful.
Is there a way to cause my app to update itself at exactly midnight every night? I need the new content to be displayed on the app right when it hits midnight. I have an idea of how to accomplish this, but if it isn't in another thread and is in the onCreate and the app is running in the background next time it is opened it would just display the previous info and not the updated?
I could also use help accomplishing this same thing with iPhone as well.
I will clarify a bit. So all the information that is to be displayed on the app will be in the app already. I simply want the content (whats displayed) on the app to randomize and then display the new group of content only once per 24hours or at exactly midnight. Hope that makes it more clear.
Android:
You can set pre-determined times to update with AlarmManager
You can look at a snippet here: Android: How to use AlarmManager
iPhone:
With iPhone you probably have to download the content whenever you re-open the app.
Can't you just have the app update the content upon launch, or when entering the foreground in the appDelegate.
This question is very vague - but if I understand the requirements correctly you will need to serve the application's content dynamically via a content server (or some type of a CDN). In this case there could be various scenarios.
In the easiest possible implementation, you could have the application be powered by data (XML, JSON, etc...) from something like Amazon S3 and have logic within the application to know how to fetch the correct data depending on the current day.
This wouldn't be extremely difficult to implement, but it would require building some type of cross-platform framework that reads the same kind of data for each application.
Is the content available before midnight?
If so, can't you have the app download it in the background beforehand and then make it available exactly at midnight?
If not, there's surely going to be some delay anyway.
app can not update itself at least in iOS apps.