
Hi, my name is Yura. And I, like many of you, are a programmer. As you know, programmers are not just software developers, but creators. But, unfortunately, in everyday work it is difficult to create something new (especially when you are busy with mobile development). And it often gives rise to the desire to collect your Frankenstein (well, or a bicycle, if you so desire), at least in your free time. For example, I always wanted to make my Frankenstein, who would do the main routine work for me in an Android application.
And now I have recently, finally, found some free time for this business (
note by the author: here you can just be happy for me )! After reading various articles about what is now fashionable, or it became fashionable many years ago, I collected for myself an exemplary list of what my friend will consist of. And this is what he
has learned from me:
- Walk! He goes to the Internet, gets data from there, and brings to him
- Remember! If necessary, he will remember what he took on the Internet, maybe even process this data first.
- And in general - to perform several tasks at the same time! Moreover, do not bother about the fact that suddenly our monster decides, for example, to dig
')
How many different elements I had to lump together so that my monster, named
FunLib, came to life! Here such stars as retrofit, eventbus, jobqueue gathered ... But let's be in order! So, who is responsible for what in my brainchild:
Going to the Internet
We go to the Internet using
Retrofit +
OkHttp . This allows you to do all the work quickly, accurately and with the support of a large community. Agree, it is very convenient to make a request for service in a similar way:
public interface GithubApi { @GET("/search/repositories?sort=stars&order=desc") SearchResult search(@Query("q") String query); }
And how cool it turns out, right for the Evangelist! After all, the development is really not at the level of implementation, but, literally, at the interface level! In general, a lot of things can be added to the request, customize it in different ways. But since this is not the essence of the article, I leave it to your independent study. Well, or it would be great if someone wrote how he uses these libraries coolly, especially if his moves are not trivial and not covered in
off. guideMemory in DB
ORMLite is responsible for its
memory . As they say, the taste and color of all the markers are different. I like this solution the most. Especially, to the advantages of this library, I attribute this:
- From the database, you can get ready-made objects, and not a raw data set (even with data on secondary keys)
- You can get the cursor by performing an ORM request
- You can get the cursor by performing the usual sql query, with all the ensuing features
- This solution will surely be easily understood by anyone who will accompany / read your code in the future.
Here, as usual - annotate the necessary classes and fields like
@DatabaseTable
and
@DatabaseField
, respectively. Then we can calmly add, or, if necessary, get the data in the database:
private void saveRepositories(List<Repository> repositories) throws SQLException { Dao<User, ?> userDao = getDbHelper().getDao(User.class); Dao<Repository, ?> repositoryDao = getDbHelper().getDao(Repository.class); for (Repository repository : repositories) { userDao.createOrUpdate(repository.getOwner()); repositoryDao.createOrUpdate(repository); } }
PS: unfortunately, while FunLib is not able to comfortably notify loaders by URI ... Yes, and its activations with fragments do not provide functionality for convenient work with loaders, but everything is in
your hands! Pull-requisitions are waiting for you. Well, or wait for me to do it: P
Brains for parallel work
For this important process, a tool called the
Priority Job Queue was chosen. Why haven't we made our own service? Yes, everything is simple - I was interested to use this library =) Yes, and the manifest remains clean. And, not least, this solution
quite acceptably fits neatly into our Central Nervous System (a little below is said about it). But FunLib and I sat down, thought, and decided that the Job, from which the Path team offers us to inherit, is too monstrous. It will be hard for me to give my baby some commands. Well, or he will not be so easy to understand them. Therefore, there was made a toddler named
FunJob
, who can just do the work we need in the method
public abstract T doJob() throws Throwable
.
An example of work. Attention! Monster is dangerous! Look only desperate brave! public class SearchJob extends FunJob<List<Repository>> { private String mQuery; public SearchJob(int id, String query) { super(id); mQuery = query; } @Override public List<Repository> doJob() throws Throwable { SearchResult searchResult = ((GithubApi) getApi()).search(mQuery); List<Repository> repositories = searchResult.getRepositories(); return repositories; } }
Central Nervous System or simply DI
For such an important task as the delivery of the database, the Internet, and other information to the brain and back to the robot body, a component called
Dagger , which implements the
Dependency Injector , was used. Fortunately, our brains can do an injection in Job when Job is ready to be executed! Therefore, FunJob, when it does its work, can confidently access the database, the API, and generally knows that it is him! After all, it is so important to associate brains with the body!
And to send the command from the brain to the body was used a thing called
EvenBus . Initially I wanted to use
Otto , but, for some unknown reason, FunLib disliked him, and I quickly refused it. Do not worry - EventBus also copes with its task! The body always gets the right commands on time!
Ready Steady Go!
At this point, I decided to take a halt, complete my story and get a fitback from you. And after that I will continue to develop my crumbs further.
In the
example, you can clearly see how to manage the work of FunLib - all you need is:
- inherit from FunApp, set the path to the API, the interface that works with the API, and your DbHelper class
- inherit from FunActivity / FunFragment, and there it is already convenient to start the work, get access to the API and the database
- do what you need! ;)
P.S
Perhaps, having read about loaders, you will think that I, at last, will decide to fasten to the ContentProvider application. But no, I do not plan to do this, because I think not casual off. The guide
says : you don’t In my practice, I have not yet had to make such an application that would provide data to other applications. And do not throw tomatoes at me because of this! All of us come across different tasks.
It is also sad that you need to independently monitor what kind of ID for the FunJob has been set. But nothing - it will be corrected, and it will become another treat!
Who missed the link to the repository, catch:
https://github.com/senneco/FunLibSee you in the comments and pull requests: D