I have Content provider and sync adapter in my project.
In my project first i'm going to fetch data from server and then i'm storing it in my database sqlite.
Now i'm coming to my issue. Actually i want to change the server data according to the data modified in the sqlite.
For example: if i delete data in my database i also want to delete data in my server database. For this purpose my sync adapter should know what operation is performed like delete or insert.
Another way of doing this is comparing my whole local sqlite database with the data in the server when the sync adapter is invoked.
I don't want that approach. Is there any way to know who is invoking sync adapter or what operation is performed in content provider from sync adapter?
The term "who is invoking sync adapter" seems not valuable, as long as that would give you no useful information. Instead, you most probably would like to pass some params at the time when someone is asking for a sync (via requestSync()).
ContentResolver.requestSync(Account, String, Bundle) accepts an extra Bundle that will be passed to your SyncAdapter's onPerformSync():
// a code that initiates sync
ContentResolver.requestSync(ACCOUNT, AUTHORITY, someBundle);
...
// your implmementation of SyncAdapter
#Override
public void onPerformSync(
Account account,
Bundle extras, // acquire information from this bundle
String authority,
ContentProviderClient provider,
SyncResult syncResult) {
/*
* Put the data transfer code here.
*/
...
}
Related
I want to implement chat between two users in mobile, I'm using android studio and store the messages in AWS(amazon) MySQL database.
I manage to read messages every time user starts the chat activity like so:
void onCreate() {
readHistoryMessagesFromLocalDataBase();
readMessagesFromGlobalDataBase();
}
The first function is used to read the history messages from the local database of Android using SQLite.
The second function is used to read any new messages waiting on the global database of Amazon using MySQL.
This works perfectly, but how can I receive messages in real time while I'm already in the activity without reloading or refreshing?
Is there any data change listener to achieve this goal?
Really hoping someone may be able to point me in the right direction for this one. We have an app that uses Realm to manage our data locally on the device.
Through general use there will be data inputted to the device and then this will be attempted to be sent to the live service we have on our api. However due to the location of the users we cannot be sure if this has been uploaded before we re-download the data the next time the app is opened.
We need it to work just like Git in a way. You can't pull without first committing your changes. But instead of committing we are pushing up.
We believe the live data should take priority here so if there is a change on live it should get pulled down, but if there is something null on live but we have it locally then we should change that.
Is there something we can use to code this functionality, can't believe we are the first to face this issue. Either that or a flow for how we should get the data before the user can start working.
An example:
We are looking to save the imei number that the user enters. This will save to the realm db at the time and attempt to update the live database. If this is not possible when the app is restarted and the imei numbers are pulled we don't what that to be possible until the data we have locally that is missing on live to be pushed.
Imei.java (Realm model)
public class Imei extends RealmObject {
#PrimaryKey
public int id;
public int deviceId;
public String imei;
}
When we get a response on the fetch:
#Override
public void onResponse(JSONObject response) {
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.create();
ImeiResponse imeiResponse = gson.fromJson(response.toString(), ImeiResponse.class);
for (Imei imei : imeiResponse.imeis()) {
addToDB(imei);
}
}
And then we use addToDb, which is where I would imagine that some kind of check would take place as if we were able to pull then we have a good enough connection to push the data we have locally
public void addToDB(Imei imei)
{
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
realm.copyToRealmOrUpdate(imei);
realm.commitTransaction();
}
Just in case someone stumbles across this question we did eventually find a solution.
Any time we need to save data we are adding it into a request queue table and then attempting to run each of the requests. If they pass they are removed, if they fail they are deleted.
Every time we then go to pull data we check that requests queue first and run if there is anything in there, if they continue to fail you then don't pull.
This seemed the best solution for us. Hope it helps
Now, I save data in datastore of google cloud.
module-backend
#Entity
public class ComprarProducto {
#Id
private String producto;
private boolean comprado;
public ComprarProducto() {
}
public ComprarProducto(String producto) {
this.producto = producto;
this.comprado = false;
}
...
}
Example data of CompraProducto
This data is displayed in a listview. When I add one item, the list is updated and saved in datastore.For save data I use AsyncTask in app.
Then, If I add one item in device1, I want to see that listview as both device1 as device2 is updated, but I don't know how to do it.
I use too GCM (google cloud messaging) and I've got to send a message to devices but only this. I want to see the data update in the device 2 without taking any action.
I want to see the data update in the device 2 without taking any
action.
This is not possible without somehow notifying from the backend server that new data has been saved to datastore. Sending a push notification to device2 (or all other devices or whatever) would definitely be the correct way to do it. What you do is have the push notification tell device2 to query datastore.
If you really don't want to use GCM you COULD have device2 just query datastore every couple minutes but that is not recommended. The correct way to do it is to use GCM to give your device2 a "tickle" to indicate that there is new data in datastore and to query accordingly.
Not sure what kind of app you are building but these are some options.
i have an android app that gets data from mysql database, now i want to implement refreshing capabilities such that when new entry is entered int the database then everyone having the app will be able to see the new entry after sometimes even without restarting the app again. how can i do that.?
i have tried to use
Intent i = getIntent();
finish();
startActivity(i);
but it does not do what i want as it refresh the activity but does not load new entries.
You have to implement the code to fetch the new data from the db and update the entries in your onResume() method of this activity for your code to work.
If by entries, you are using a ListView, you could also use adapter.notifyDataSetChanged() to refresh the data without having to finish and restart the entire activity. (Taken from this post)
In your Activity you will be calling a API to get data from mysql Database. Now you need to call that API after a fixed interval of time to get data from mysql database again.
If new entry is added into the database you need to send push notification to all users about the updation. You can also implement automatic refresh when such notification is received by the users.
I'm new to server side.
I'm creating a database app for my company that stores links to all our marketing videos. Each entry is a url(to video), description, industry etc.
I already have the front end somewhat set up in HTML/JavaScript. Using a local XML source file, it populates a list with video names, and has text fields for all props of the video item.
Here's my question:
How do I handle updating my view when I send the form data (new entry) to the back end?
Should I insert a new entry based on local data?
Should I wait for the response from the server, and if success then update view based on local data?
Or, same as above, but update view based on back end data?
The goal is to make sure my view always reflects the state of data on the back end.
(Back end is Java / Google App Engine)
When using AJAX:
There is a callback function included in it, which triggers asynchronously when the response of the request comes back from the server.
In this function you can call your page update functions to execute on the page updating processes.