When I make an app, I always wonder when is the best moment to load data from a remote database.
If the app ask for a login screen, then:
Is it better to launch every data from remote DB at his time, and then use them in the app? This way, the app is much faster after the login screen (no more queries needed to retrive some data)
Is it better to load data only when we need to use them (for example, display some data from DB into the app). This way, the login is much faster, but the app can be slower than the first case.
What do you guys think?
It depends on the scenario, but I would go with option 2: First, the user is granted the access and you retrieve the data from your backend when needed.
Imagine a very simple scenario where the user logs in to see a list of products he can work with (add and remove). The solution would consist of 2 screens:
A login screen to manage user access
product list screen to work with products
For me, in a good design each screen has its own resposibility, and each screen should query only the data that it is going to manage.
The login screen responsibility would be only to perform the login and then navigate to the product screen.
The product screen responsibility would be retrieve the products, show them and store them when the user has ended the editings.
If your login screen queries the product data to pass it to the product screen, you are coupling the login screen and the product data.
Anyway, if you have a set of static data that can be used by several screens (for example product categories), you can query them the first time you need them and store them in a cache for further accesses.
Another scenario could be if there can be conectivity problems. In this case the best solution could be to download a set of data that the user can work with, edit them and them upload them to the backend (taking into account the possible concurrency issues).
If you want to logIn your user you have to call the remote DB to authenticate the user. I do not see where you are able to do this without the remote db.
Otherwise you are correct that you should only query the DB if you want to show the data you get.
Related
I'm working on an android cooking app (using java) and the homepage has a recycler view populated with recipes which users can like (similar to FaceBook posts). The like button is a checkbox, what is the best way to save the state of the like checkbox for every recipe, so when the user signs out of the app and sign in again they will not like the same recipe more than one time.
Is using SharedPreference a good idea in this situation?
**im using MySql as a database and firebase is not used.
You could definitely do it using SharedPreferences - but I believe this is more ideally designed for 'preferences' of an application rather than 'application' behaviour state.
You might want to have a look at : https://developer.android.com/topic/libraries/architecture/saving-states?authuser=1
Here you will see some options of how to serialize and persist UI states to memory. I have not actually used something like this before - in the past only using SharedPreferences.
You could of course also create your own storage method, or solution but why bother when ones already exist.
Since you are already using a MySQL database, your application is already grabbing the rows from the DB to show them, so why not add another column called 'checked' which is a boolean type. Then when you get your recipes you get the 'checked' variable, and if it is true, then set your UI state of the checkbox to checked, otherwise false :)
The benefit of this approach is that if your app became 'hybrid' and you wanted a website for it too - the data on the database becomes centralised, meaning your persisted user state is the same on mobile as it is web experience, which is a nice benefit!
i have a question that i couldn't find a satisfying answer.
i have a "Add Product" screen, this product has zero or many images. In this same screen there is the upload option.
My question is: how can i upload pictures to the product, if i don't have it`s id yet? since it is not saved on database yet.
Is there any best practice for this situation?
Thanks in advance
I'm just listing down my points on how to handle it:
Handle both at once - Send the Picture and Details of the product at once on save/add button click using enctype=”multipart/form-data” (Using spring)
Now you can handle in your code. Perform validations.
If it passes all validations, then upload the image/save the image.
Send the url/filepath to the Object/DB where product is getting saved.
Drawback:
Say user has already uploaded the files and is filling the product details. In which case, We're still waiting for user to enter all the details of the Product and then upload.
The final upload during save is going to take time. (Second option can overcome this)
Upload Images First as in when they're available and then Product on save -
Upload the Image first to the server (As in when user adds an image, keep uploading).
Let Server save all these images at a temp-place and return a UUID number to uniquely identify these images.
On save/add action, let client send all those UUID's list that it got from server during upload time. Now server can identify what all images can be moved from temp directory back to central server or associate the images with Product.
You can have another Cron Job or SessionTimeoutListener or A call from client when user backs out of Product Page without save or A combination of all 3 to remove those images which are not associated with any product for say last X minutes (Session timeout minutes)
Drawback:
Needs a lot of changes and post-processing/clean-up overhead.
Handle Product first then Image -
Push product details first to server on save and server return the productId that got saved.
Now client makes another call to server with productId and Images.
Drawback :
We're relying on client to handle transactions, which is bad.
Inconsistency with data might grow.
The time taken for the entire process might grow compared to other 2 approaches.
PS : I feel the second opinion to be good. This is just my opinion.
I have a user table & a child table Post containing user's posts. A user has a large number of posts just like twitter posts.
I need to fetch user posts on the UI, load more posts as the user scrolls down the page just like facebook/twitter do. I am using hibernate as the ORM framework & MySql as db. I looked into pagination found two primary ways of achieving that
1. setFirstResult();
setMaxResults();
2. ScrollableResults
I have two questions
A. Which way of pagination would be more appropriate & efficient to achieve this? I've read that ScrollableResults is more efficient than setFirstResult but it keeps the connection open for the entire pagination process.
B. As the user's profile page is loaded ajax call is fired to display the user's posts everytime, it's like a certain default content in the page. So do I need to implement a second level cache in order to avoid db hits every time the page loads?
If the dataset is large then choosing ScrollableResults will be
better.
If the users will be accessing your application with low bandwidth
and mostly will be retrieving only first or specific page say from page1 to page2
then go with setFirstResult() and setMaxResults().
I have web application based on jsp and spring mvc where i need resolve this task :
The user must be able to add new instances of the main entity using wizard dialog. The wizard consists of 3 steps:
On the first step there must be a form which allows filling main entity’s fields, including association with the entity related as many-to one (it’s recommended to use drop-down field). The form should contain fields of different types: text, number, date, radio button, etc. Some fields should be required and some are not.
Example: input name, surname, birth date, phone, number of kids, select gender (radiobutton), department (drop-down), etc.
On the second step user fills additional attributes, including association with the entity related as many-to-many with the current one.
Example: associate employee with skills that (s)he has (checkboxes), add some note (textarea).
On the third step all the fields from previous 2 steps should be displayed as read-only fields. The user should confirm saving this data into database. After the user confirms saving, the data should be saved into database, and user should be redirected to the page with the list of objects.
How can i transfer and hold information without using sessions(Http session, session scope)?
You need to keep state across multiple server interactions. There are several possibilities, in general factors such as the size of the state data to be retained influence our decisions.
It sounds like you have some small number of hundreds of bytes here, so you're not particularly constrained by size - a few Megabytes would be more of a challenge.
First possibility, keep it all in the browser in JavaScript variables, no actual need to send anything to server. This is typical of a modern dynamic Web UI, where the server serves up data rather than pages. Sounds like you're in a multi-page world so discount this option.
Second, just put some data (possibly encrypted, in a cookie) effectively the browser is keeping the data for you, but it's shared across the pages.
Third use Http Session state - you case does sound very much like a typical candidate for a session. Why do you want to avoid it? Depending upon your server's capabilities this approach may not give great resilience behaviour (if the state is on one server instance then all requests for a session must be served by the same server). Note that HTTP Session and EJB Session Beans are not the same thing, HttpSessions are lighter weight.
Use a custom session "database" - maybe literally a SQL database maybe something lighter. For larger scale data entry cases, where a user may take 10s of minutes to complete many pages this may be the best option - the user's work is saved should they need to break off and resume later. It's more development work and you need to look at housekeeping too, but it's sometimes the best option.
In summary: be very clear why you reject the "obvious" HTTP session technique, in terms of simplicity it's where I'd start.
I have to develop a application with the following scenario:
1--User create a profil (name,lastname,address....)
2--The information are inserted in the database
3--The administrator should receive an e-mail to be notified by the new profil ,if the administrator dont
approve the profil he will delete it.
Is there a way to send automatically an e-mail when a row is inserted in the database?
I'm using java.
If you are doing this through a common interface, then I'd suggest putting a seperate call next to the insert routine (between Java and database).
I heard you can do a whole lot of things with triggers in MS SQL, but I was told to keep my hands away from triggers. Made by the devil to make a day turn bad. People that has succesfully mastered triggers will tell you otherwise.