I have a location service in my app. For debug purposes, i want to log (to text file) some events like "new coordinate", "service onDestroy", "service onStartCommand", "coordinate sent to backend", and so on.
But im facing with a problem. The log file gets 350+ new lines each day .. so.. in 3 days i have a file with 1000 lines.
My idea is to maintain only the 3 (or N in that case) last days and delete the content that was written 3+ days ago.
But:
I don't want to check in every write if there are old lines to be removed
I don't want to set an alarm that fires every 3 days to erase the old data.
Can you please tell me if you know another efficient way to handle this situation?
Without a way to index into the text file, there's really no way to solve it without reading each line of the file (up to a certain point), parsing it and finding the date.
Don't keep a file. Keep a database. Have one of the columns be "created time". Then you can easily delete the rows that with a created time older than some threshold.
getContentResolver().delete(
yourUri,
"created_time < ?",
new String[] { System.currentTimeIllis() - Integer.toString(TimeUnit.DAYS.toMillis(3)
);
(Or something, please test your own SQL ...)
As a side note, a 1,000 line text file is nothing. Reading and parsing it is not significant unless you are doing it often. If you read and trimmed the file 1x per day, no problem.
Another solution would be to rotate the files (log --> log.1, log.1 --> log.2, ..., log.n-1 ---> log.n). This of course doesn't set a hard limit on the size of any particular file.
Related
I'm really confused here ... looks like a really silly mistake but I don't know what's happening. Here's little snippet of my code :
if (tempDeltaDeviation > standardDeltaDeviation) {
Log.e(TAG, "handleMessage: plus $tempDeltaDeviation : $standardDeltaDeviation")
scaleUpAnimation(deltaAnimStep, "Delta", binding.deltaImg)
}
Really basic stuff, right ? Well checking the logs I can see :
handleMessage: plus 1.57756888292539E14 : 7.8364593205657E13
Dunno but last time I was checking 1 was much smaller than 7 so why app enters if statement ?
1.57756888292539E14 is in fact larger than 7.8364593205657E13.
These numbers are represented in scientific notation which is used to work with very small or very large numbers. 1.57756888292539E14 means: 1.57756888292539 * 10^14. By increasing the number after E by one we actually increase the resulting number 10 times. By increasing it by 6, we increase the resulting number million times (10^6 = 1000000).
Making things simple, your numbers are really:
157756888292539 (1.57756888292539 * 100000000000000)
78364593205657 (7.8364593205657 * 10000000000000)
As you can see, the first number is actually bigger.
I am currently working on android application that can track income/outcome of a budget. In one of my database is named Transaction consist of:
Amount
Type (income/outcome)
Date
since this is my first application I'm not really confident with my code, especially in the database. And now I'm making a trigger that can calculate percentage of outcome in a month. Can you check it,if my code is right or not, or not efficient. Here is tiny part of my Trigger code.
In my code I want to calculate percentage of total outcome from the first of a month until the current date .
"CREATE TRIGGER Calc"+
"AFTER INSERT"+
"ON" +transaction+
"FOR EACH ROW" +
"WHEN (SELET * FROM amount.transaction
WHERE (strftime ('%d','now') - strftime('%d','start of month'))
HAVING SUM(amount.transaction WHERE type.transaction IS "outcome") /SUM(amount.transaction WHERE type.transaction IS "income") * 0.01 )"
is it select * from amount.transaction needed? so the code can be much simpler?
0.01 there is 100%
sorry its so messed up since I'm just start in making android and I'm not really well with database. If you have any suggestion please tell me.
Thanks before
I agree with commentors, I don't think you need to use triggers ahead of on-the-fly SELECT statements.
You do need to know that quotations " are literal and when concatenating strings, you need to manually add spaces.
I am beginner and trying to write some calculations with App Inventor 2.
I am trying to write a code to calculate Net present value.
The formula of NPV = - investment + CF/(1+i)power up by years of investment, which means if years of investment are > 1 the second part of formula will repeat until it reached the number of years.
I successfully code the formula for one year that works correct, but have problem with the "repeating" the second part powered by number of years.
I tried to declare years as variable to use it as powering number but think something is wrong with it.
In my opinion I need to split the powering number somewhere to memory and then increase it by 1 until the required number. However have no clue how to do it.
Can anyone help?
Screenshot of the blocks
Following the calculation from the NPB Calculator,
this is converted into blocks the following
Note: for a better clarity and to avoid such long calculation blocks as in your screenshot, I used External Inputs instead of Inline Inputs, which is the default. You can switch that from the context menu after doing a right mouse click onto one of the calculation blocks.
EDIT: screenshot updated for changing cashflows using a list.See also
How to work with Lists by Saj and
How to work with Lists and Lists of lists (pdf) by appinventor.org
im very new to Java and Android Programming and am currently trying to write my first Android App.
The App shows 3 Dices, and depending on what combination the dices Show, the App says "yes" "no" or "ultra yes".
My Problem is, when hitting the Button to Re-Roll, sometimes the Pictures of single Dices do not get Updated and then the Answer is wrong (i hope it is somehow clear what i mean)
In this Screenshots you can see what i mean. The first Picture everything is correct (2 dices showing the same numbers means a "No"). Then i hit the Reroll button again and somehow the Pictures didn't get updated.. This occures Randomly, sometimes it works correct 10+ times.
http://image-upload.de/image/gbYhNq/ce3ef664bd.png
http://image-upload.de/image/j8jSTv/97ed7d0f7e.png
The Code of the APP:
http://pastebin.com/360DwFcg
thanks in Advance from a newbie ;)
Your random dice number generation is wrong. It creates a random number from one to seven.
This is probably a long shot, but since your answer is "ultra yes", then all three dice are the same value. So maybe you just got a lucky 7, 7, 7, in which case no images are updated. Just change your random generation to
(int) (Math.random() * 6 + 1)
I am storing data on an interval basis (gps location) and I dont want the DB to swell up so I defined a MAX number of lines it can go up to and then it simply deletes the oldest line every time I insert a new one.
Now a database expert looked at my code and he says its way not efficient because deleting a row from the database is the most time/memory/procedures consuming action and I should avoid it at all cost.
He says I should instead, run-over the oldest line (update) after I reach MAX.
(so it goes top to bottom every time)
That means I need to save a separate "header" table to save my current pointer to the oldest line and update it on every insert (i don't want to lose it if the app crashes) ..
does it really more efficient ? any other ways to do this more efficiently?
Turning your database table into a ring buffer is silly.
If you really want to use this approach...
Don't use a database just use a data file and IO
In your data file each record will be a fixed size
[Time Stamp][Latitude][Longitude]
You can use string formatted or binary representations of your data it doesn't matter as long as they are fixed size.
----------gps.dat----------
[Ring Pointer]
[Time Stamp][Latitude][Longitude]
[Time Stamp][Latitude][Longitude]
...
[Time Stamp][Latitude][Longitude]
Ring Pointer is the binary representation of a long integer
When you first create the file you'll set its size to the size of an LONG_INTEGER_SIZE + (MAX_RECORDS * RECORD_SIZE)
When you want to add a record:
Read [Ring Pointer] from the beginning of the file
Write [Ring Pointer] + 1 to the beginning of the file (so people don't get confused keep the [Ring Pointer] variable the same just write the new value back to the file)
Go to location LONG_INTEGER_SIZE + (([Ring Pointer] % MAX_RECORDS) * RECORD_SIZE)
Write your new record at that location