Recyclerview and realm. Should I use a background thread for realm? - multithreading

I am using a recyclerview in which I show data using realm database.. I am asking myself, shall I use a background thread for realm? I mean I am using a recyclerview and as far asaI I know realm is powerful and fast..should I use a background thread anyway?
Thanks.

I'd recommend using a background thread just to prevent blocking of the UI thread.
Despite how powerful and fast Realm is, and trust me I personally know how good a library it is, it is generally good practice to extract the database data in a background thread.
One approach I would personally take is using RxJava. First subscribe to a custom observable that pulls the data and then simply populate the recycler view after it's completed.

Related

Is threading is necessary to use in SQLite based Delphi app?

I am very much beginner in Delphi development. I want to make desktop application that include Sqlite database and browser. The application is mostly based on CRUD operations but when ever user update data into database the change will be displayed on browser on another panel of the application. it also generates printable pdf. The pdf is generated by C# code[NReco Library]. Is I have to use Multi Threading in to my application to make my ui responsive in this case.
SQlite3 has very low response time. A simple SELECT is less than a few microseconds in practice, since it is an embedded database, and no network layer is involved.
No need to implement background threading with SQLite3.
For demanding content, e.g. reading a lot of data, you could:
Use proper paging (e.g. read 10K rows each time);
Call Application.ProcessMessage every now and then;
Add a timeout in the loop.
You should design your app just as if it was a client app. Too much data is killing the data, both for the UI and the User. Only get what you need to for proper display.
It's probably not necessary, but it is always a good idea to run long running tasks on a separate thread. Delphi makes that really easy so it should be of no concern.
Alternatively, you can use techniques like periodically calling Application.ProcessMessages() to keep your UI responsive.

Minimal multithreaded transaction with Hibernate

I'm using Hibernate in an embedded Jetty server, and I want to be able to parallelize my data processing with some multithreading and still have it all be in the same transaction. As Sessions are not thread safe this means I need a way to get multiple sessions attached to the same transaction, which means I need to switch away from the "thread" session context I've been using.
By my understanding of the documentation, this means I need to switch to JTA session context, but I'm having trouble getting that to work. My research so far seems to indicate that it requires something external to Hibernate in the server to provide transaction management, and that Jetty does not have such a thing built in, so I would have to pull in some additional library to do it. The top candidates I keep running across for that generally seem to be large packages that do all sorts of other stuff too, which seems wasteful, confusing, and distracting when I'm just looking for the one specific feature.
So, what is the minimal least disruptive setup and configuration change that will allow getCurrentSession() to return Sessions attached to the same transaction in different threads?
While I'm at it, I know that fetching objects in one thread and altering them in another is not safe, but what about reading their properties in another thread, for example calling toString() or a side effect free getter?

Caliburn Micro 2 EventAggregator PublishOnBackgroundThread

Can anyone explain any reason why and when should I use PublishOnBackgroundThread instead of PublishOnUIThread.
I cannot find any use cases for usage PublishOnBackgroundThread and I am not sure what method should I use?
It really depends on the type of the message you're publishing.
If you're using the EventAggregator to surface a message from a low laying service back the UI then PublishOnUIThread makes the most sense since you'll be updating the UI when handling the message. The same applies when you're using it to communicate between view models.
Conversely sometimes it get used for view models to publish events that an underlying service is listening to (rather than the view model depending on that service).
That service may perform some expensive work which makes sense to happen on a background thread. Personally I'd have gone in the background service to push that work onto a background thread but different people want different options.
Ultimately the method was included for completeness.

Making code in Liferay Model Listeners Asynchronous (using concurrency)

The Problem
Our liferay system is the basis to synchronize data with other web-applications.
And we use Model Listeners for that purpose.
There are a lot of web-service calls and database updates through the listeners and consequently the particular action in liferay is too slow.
For example:
On adding of a User in liferay we need to fire a lot of web-service calls to add user details and update other systems with the userdata, and also some liferay custom tables. So the adding of User is taking a lot of time and in a few rare cases the request may time-out!
Since the code in the UserListener only depends on the User Details and even if there is any exception in UserListener still the User would be added in Liferay, we have thought of the following solution.
We also have a scheduler in liferay which fixes things if there was some exception while executing code in Listeners.
Proposed Solution
We thought of making the code in UserListener asynchronous by using Concurrency API.
So here are my questions:
Is it recommended to have concurrent code in Model Listeners?
If yes, then will it have any adverse effect if we also update Liferay custom tables through this code, like transactions or other stuff?
What can be other general Pros and Cons of this approach?
Is there any other better-way we can have real-time update to other systems without hampering User-experience?
Thank you for any help on this matter
It makes sense that you want to use Concurrency to solve this issue.
Doing intensive work like invoking web services etc in the thread that modifies the model is not really a good idea, apart from the impact it will have on user experience.
Firing off threads within the models' listeners may be somewhat complex and hard to maintain.
You could explore using Liferay's Message Bus paradigm where you can send a message to a disconnected message receiver which will then do all the intensive work outside of the model listener's calling thread.
Read more about the message bus here:
Message Bus Developer Guide
Message Bus Wiki

Lifetime of an OrganizationServiceContext - CRM Dynamics 2011

This is more of a non-tech question.
We intend to use OrganizationServiceContext with Linq as opposed to calling OrganizationServiceProxy.
My question is: what should the lifetime of the context be? Should it instantiated once per method or can you keep it around for the life of the web application using a singleton approach?
What would the pros/cons be? Any advice?
Thanks in advance
You should never keep a datacontext around for the life of a web application. The application lifecycle is managed outside of your code.
There is also a world of pain around saving changes when other users are saving at the same time. Datacontexts should always be managed only for the life of the request and running save changes should never save bits and pieces from other people's request as they are processing.
If you want to reduce reads, then use caching.
If you want to manage concurrency use transactions with a unit of work.
Just to expand a little on Gats' answer, which is entirely correct, we create new context objects pretty much for each separate method we have. Even for Silverlight, where we know we're running for one user at a time, managing what is in the context at any time is just too painful just to avoid creating a new context object.

Resources