I want to write a callback from sqlite database to android activity. Currently i am inserting some data from the webservice in to db and then set it to listview.
I am running a service in the background which will continuously check for the new data. If there is new data it will update the database.
My prob is how to define the callback onupdate from sqlite to activity.
Any help will be appreciated.
You should be able to achieve the result with observer pattern.
Your activity will implement an custom interface like RefreshableOnDBUpdateTrigger
in which you will re-load data from DB using standard DB querying mechanisms.
In your DB update class, you can call RefreshableOnDBUpdateTrigger.update() method which is essentially the method of your activity instance which fetches updated data from DB.
Refer observer pattern for implementation details.
This would require user-defined functions, which are not supported by the Android database framework.
You have to call these callbacks explicitly in your database access code.
Another option to the interface callbacks would be to use an Event Bus. The DB code fetches data and then posts an event with the data. Any registered classes would receive the event.
Related
I've just begun to learn RxJava and I'm a little bit lost.
My scenario is the following: A local NoSQL key-value database where I store some data. The problem is that the endpoint can add data to this DB and also the user can delete some of it.
I'm displaying this data as an on-screen list (RecyclerView).
I'd like to know what is the best approach to always know what's the most up to date data from the DB in a single place so I can update the UI accordingly.
What you're looking for is a way to create an Observer, meaning to transform DB changes events to Observable.
So you will have 2 kind of streams:
One that act on the DB and changes data (update/delete) triggered upon various events (push/cloud/user clicks) , those changes will trigger DB change event, that in it's turn, will emit events on an Second stream that represent DB changes event.
Then in your UI, you can react to the changes in Rx way (responding to the stream of DB changes events).
In order to create the DB changes Observable, you need to learn about hot to create Observable from async events (can be done using Subjects, or if you are integrating with some DB and you have DB changes events, you can 'wrap' it with Observable using fromEmitter(), you can learn more about it from this blog:
https://medium.com/yammer-engineering/converting-callback-async-calls-to-rxjava-ebc68bde5831#.z1cj7ayhr
One way to approach it would be to put a data service between clients and the data store. Make that data service an Observable. Allow anyone who is interested in being notified when data changes to register with the data service.
I have created a quite nice implementation of a Broadcast/BroadcastReceiver where I am pulling down information via a Service from a Web API, Broadcasting the result of the received data and then changing the UI when the OnReceive function is called in the Activity which updates the UI.
The way I am updating the UI though is by passing the 'id' of the row to the database and then pulling the data out again.
This doesn't seem very optimal as I have to access the database twice. Once to save and once to retrieve. How can I optimise this? How can I optimise the process of updating the UI without having to go to the database again?
Options which I have researched:
Send a Parceable/Serializable object in the Intent when Broadcasting
Saving the retrieved data in a static class and using that data when onReceive is called.
Thank you in advance.
To me is diffucult to understand which the right workflow is to download json data and show them in a ListView.
Currently i am using an AsyncTask to download and parse data that is shown using an ArrayAdapter. The problem is that AsyncTask doesn't survive to activity restarts.
So I am wondering if services are a better solutionbut how to pass data to the ArrayAdapter? Should I always use the db as middle layer to store and retrieve data?
So which is better? AsyncTask in retained fragment or service using db?
Thanks
I use a service to do http data retrieval and storage into sqlite db. Once the service has stored the data (or if there is a problem) I then fire a broadcast. My Fragments / Activities listen for these defined broadcasts and then act appropriately.
I find that this is a very clean solution and avoids the problems of leaking asynctask references on activity teardown / rotation etc.
The most simple solution would be to return your current AsyncTask in onRetainNonConfigurationInstance or use a Fragment with setRetainInstance(true).
You can access the AsyncTask in inCreate with getLastNonConfigurationInstance
It actually depends on the requirement of what exactly the Activity needs and how long can the data be displayed without hitting the service again.
One approach to the above mentioned problem is, hit the service get the json data and store it in database. Now fetch the data from database and display it in the Activity. You can now implement a service to refresh the data when required. The service will simply hit the web service and update your db content as and when required.
I am working on an Android application and I have a question. I have a listener class that runs on back ground periodically and get data from my server. I want to add that data into a data structure in the main thread. In this case, I am not touch the main U.I. but I was wondering if I should use a handler to add the data into the data structure in the main thread. Or can I just set the data structure as static and access from the listener class to insert the data. Which way should I do? Thanks in advance.
One way to do that (but there are others) is to use a list view and a cursor (it means you should use a database).
When you receive data from server (in your background thread), you add them to the database.
On the UI thread, you register a ContentObserver to be notified when data is added. When you're notified, you just have to requery
If you don't want to use a database, you can then send a Broadcast (see BroadcastReceiver) in which you can add data.
I've been watching Virgil's presentation at the Google I/O on REST-heavy applications.
Google I/O 2010 - Android REST client applications
Though the notepad tutorial makes database calls directly from the UI layer, Virgil suggests making database calls in a Service.
At the moment, my Activity's onCreate method uses an extended ContentProvider to access an SQLite database to query a Cursor to attach to the Activity's ListView.
I'd like to move this code into a Service. Easy enough. My question is, what is the appropriate way to transfer the Cursor back to the UI layer? I've seen many questions posed and there always seems to be someone suggesting there is a more appropriate way to do it. What is that way?
More specifically, I so far gather that the Activity should register as some sort of listener. When the Cursor is retrieved in the Service, a notification is set to the UI layer to access it. Where does the Service shove it so the Activity can grab it?
Also, my considered architecture is to have an extended Service, which is called by Activities. Inside this Service, database transactions will be made through the use of an extended ContentProvider, any listening Activities will be notified, and threads will be launched to hit web services. So, I have 1 extended Service, 1 extended ContentProvider and several extended Threads for different web services. Does this seem acceptable?
I am simply using the managedQuery call inside my Activity to get the Cursor. It's a fairly light operation and I don't think it will hold up the UI.
I've created a Service which then calls a web service to find new data. If new data is found, it is placed in my database and sendBroadcast is called. My Activity has a registered BroadcastReceiver which hears the broadcast and calls requery() on the Cursor.
Have you consider implementing Loaders?
I have an application which uses Intent services to fetch data from a server and store
them using a ContentProvider. Then the fragment or activity which needs the data implements
LoaderCallbacks<Cursor> (onCreateLoader, onLoadFinished and onLoaderReset).
At onCreateLoader I instantiate a CursorLoader object and return it while at onLoadFinished I make sure that the data from the Cursor are displayed in my Fragment/Activity.
Check the official loaders guide.