I do not have not created any code yet on this topic so I will just ask a question.
I am making an application in which user will insert multiple paths, and in those paths/folders I will be searching for N biggest files in every folder separately. I was thinking that I should use new Thread for every path but I am not sure if this is a good practice or good idea in general.
One little subquestion - should I use TreeSet(RedBlack tree) to keep files in some kind of order or B-Tree would be better?
EDIT: By new Thread I mean using multiple threads with AsyncTask or something like that
Related
I am currently learning android programming and creating an app that will store some integers representing user choices (values inserted several times a day, must be displayed in the results activity) and steps data collected Google Fit HISTORY Android APIs, also displayed in the results activity. I am looking for the most efficient way to store this data. I know that it might be possible to insert the custom data types in the GOOGLE fit database. However, I am not sure if it is a good idea if the app mostly works offline, and it needs to immediately represent only a small set of results, for example, the values inserted in the last 2 weeks, with step counts. On the other hand, I am not sure if it is ok to have two databases storing the data.
My apologies if the question sounds a bit too amateur, I am doing my best to find an optimal solution in terms of performance.
Thank you for your answers.
So, to give you my opinion and answer (mainly opinion)
Android has 3 ways (mainly) for storing data:
Files
Online database/API
Local database
for this specific scenario you have listed, wanting the data to be available offline, you should probably be looking at using Room: https://developer.android.com/training/data-storage/room, as it supports storing primitive types without having to write any type converters, you can store models and custom data as well, it uses very basic SQL (because it's a wrapper for the older Sqlite database methods) and is part of android (not an external 3rd party library). Room also requires most operations to be done off of threads, instead of main threads and this will improve your performance as well (also has support for livedata/rxjava to observe straight onto any changes as they happen)
However, as I told this user here:
Should i store one arrayList per file or should i store all my arrayList in the same file?
When starting out, don't worry about the best way for doing something, instead, try something out and learn from it, worrying about the best solution now is rather pointless, either way, happy learning and coding :P
I am very new to ORMs and presently trying to implement green Dao to manage by DB operations.
I want few good examples to work upon to understand its working but it seems I get stuck after DaoGeneration.
After this process I am able to see my generator files inside the desired directory. Can anyone tell me what to be done next. Or please suggest me some good documentation.
This is a good place to start. It's the examples provided by greenDAO themselves.
https://github.com/greenrobot/greenDAO/tree/master/DaoExample/src/main/java/de/greenrobot/daoexample
You can notice that except for NoteActivity.java, all other java files were generated by the DaoGenerator.
Some common practice:
These lines can be put in your Application scope. Means you can have one session for the whole application life cycle, according to this answer from greendao
Save your Query to reuse to gain performance.
Some Common errors:
When deleting record from DB, make sure to detach it from session. Otherwise the object is still in cache and next time you want to get record by ID and you thought this is already overwritten by a new object. Nope.
I have a bunch of little files in my assets which need to be copied to the SD-card on the first start of my App. The copy code i got from here placed in an IntentService works like a charm. However, when I start to copy many litte files, the whole app gets increddible slow (I'm not really sure why by the way), which is a really bad experience for the user on first start.
As I realised other apps running normal in that time, I tried to start a child process for the service, which didn't work, as I can't acess my assets from another process as far as I understood. Has anybody out there an idea how
a) to copy the files without blocking my app
b) to get through to my assets from a private process (process=":myOtherProcess" in Manifest)
or
c) solve the problem in a complete different way
Edit:
To make this clearer: The copying allready takes place in a seperate thread (started automaticaly by IntentService). The problem is not to separate the task of copying but that the copying in a dedicated thread somehow affects the rest of the app (e.g. blocking to many app-specific resources?) but not other apps (so it's not blocking the whole CPU or someting)
Edit2:
Problem solved, it turns out, there wasn't really a problem. See my answer below.
I suggest you to create a separate thread to do the work. Or, more simple, an AsyncTask!
Sorry for this, it turns out you actually can use the assets in a child process. I've got no idea why it doesn't work the first time I tried it. So the answer to my question is actually (b). Create a child process for the Intentservice, access the assets through getApplicationContext().getAssets() and there you go. It now runs satisfiable fast. Thanks for trying to help.
I am trying to process multiple file writing commands (to seperate files) without hanging the UI.
To put my question in context, imagine the following
My main application looks like a file manager. It currently sees 10 files each of about 5MB in size. (Don't worry about how this list works etc.)
When I select one file item, I want it to immediately start copying/duplicating the file onto another location on the SD card. Typically this should take a few seconds
I want to be able to select a second, or a third etc. file immediately after the first. At the end of everything, all the files which I selected will be duplicated. So I could, for example click 5 files within 5 seconds, but all the copying actions takes a minute.
At this moment two options have come to my mind:
The first is to simply put the file writing commands of each file in a seperate thread. Pseudocode will look like this
Onclick
new Thread()
write file
If this works, there can be scenarios where I have 10 threads running simultaneously, writing to 10 seperate files. I would like to know if anyone has done this before, and what I should be looking out for
The second option, of course, is if there is already a certain data structure/known methods that can address this problem. Some sort of a pending queue system that gets larger as I add requests, but gets smaller as the system writes the data away.
I'm not absolutely sure how the SD-card works, but I can tell you that trying to write in parallel to a single hard disk is a bad idea because it will actually slow down the performance compared to a sequential write.
You may want to try using an ExecutorService and measuring the performance with different thread counts but I'm afraid you will end up having to implement a queue with a single thread taking the queued files and writing them one by one.
I would create an AsyncTask class that simply copies a given file over. Each time a file is selected, create a new instance of that class for the selected file. The thread management built into Android for AsyncTask is well balanced and should handle this use case nicely. It will be easy to provide feedback for progress and completion using the built-in AsyncTask methods.
I think the classes in java.util.concurrent are what you need; specifically the Executors class to create a ThreadPoolEecutor. It has the benefit of accepting as many tasks as the user clicks but limiting the number of threads to some limit you specify. (Spawning threads without limit can be a problem, slowing down not only each other but the UI as well.)
Background:
My CSS360 group is attempting to create an Android application that will include an auto-complete search feature. The data that we'll be searching consists of around 7000 entries, and will be stored in a SQLite database on the phone itself. The most obvious approach would be to do a linear search of the database following every character that the user types, and then return a list of suggestions which are potential alphabetic extensions of the user's query. However, this seems like it would be pretty inefficient, and we've been looking for better alternatives. In another one of my classes today, my instructor briefly discussed the trie data structure, and mentioned that it's often used to store entire dictionaries. Entries into a trie can be retrieved in logarithmic time (as opposed to linear time for a regular old array), so this seems like a great tool for us to use! Unfortunately, we're in waaaay over our heads on this project already, and none of us really have a clue how to make this happen. All any of us have ever coded to date are basic console applications to teach us programming basics. We're all attempting to learn the Android platform in a week's time by watching YouTube videos, and differing the database stuff to the one guy in our group who has any SQL experience whatsoever. We could seriously use some pointers!
Questions:
When creating a trie, is it possible to have the entire structure pre-populated? IE: generate a line of code for every node used, so that the entire structure will already be in memory when the program starts? My thinking here is that this will save us the overhead of having to regenerate the entire trie from the database every time the program starts. If so, is there an easy way to get these thousands of lines of code into our program? IE: Some sort of script which converts the database files into a giant text file of java commands which can be copied and pasted into Eclipse?
Will there be a considerable amount of overhead if we search the database directly instead of using some sort of internal list or data structure? Should we be copying the names out of the database and searching them inside the program for our auto-complete function?
If this proves too technically difficult for us, and we have to resort to a regular linear search, will the performance be noticeably affected?
Our current plans are to run the auto-complete function each time the user enters a character, and then wait for the function to return before allowing them to continue typing. The only programs any of us have written so far function synchronously like this. What would we need to know to make this function asynchronously? Considering our novice abilities, and the requirements that we're already having to meet, would this be too technically challenging for us?
sqlite should be able to serve this auto-complete functionality reasonably well. I'd recommend using their internal indexes over re-implementing the wheel. If you need to do the latter, then sqlite is probably not going to help you after you've done that work.
If you want substring searching, then full text search is probably your best bet.
If you only want to complete the beginning of the word, then just using their vanilla indexes should be more than enough. If performance is a problem, then just wait until they type three characters before doing the query. Set a limit on your results for snappy responses.