Are ActiveAndroid .save() operations executed on the main thread or asynchronously? - android

Im using the ActiveAndroid library and I have read the entire information (very minimalist and insufficient unfortunately)
There is no mention whether the .save() operation is executed syncrhonously.
If it is asynchronous, how do I "listen" for it to end before proceeding?
http://www.activeandroid.com/ - this is the documentation I read

If you have a look at the source code of the Model class, you'll see that the save method does not do any thread handling:
public final Long save() {
final SQLiteDatabase db = Cache.openDatabase();
final ContentValues values = new ContentValues();
for (Field field : mTableInfo.getFields()) {
/* ... */
}
if (mId == null) {
mId = db.insert(mTableInfo.getTableName(), null, values);
}
else {
db.update(mTableInfo.getTableName(), values, idName+"=" + mId, null);
}
Cache.getContext().getContentResolver()
.notifyChange(ContentProvider.createUri(mTableInfo.getType(), mId), null);
return mId;
}
Saving thus occurs synchronously.

Related

Android SQL query not recognizing first entry

Buckle up folks, this is a weird one. I'm currently working on an android app that involves storing and retrieving data in an sqlite database. I was going through the app and testing some of the basic features to make sure everything worked, and lo and behold I found a bug in retrieving data from my database. When a user inputs their very first entry to the app, everything works as expected, the values get processed and stored. However, when I go back and attempt to access that data using SELECT * FROM history; I get a cursor that returns true when I call data.moveToNext(), yet when I loop through it using while(data.moveToNext()) { //get values and add to a List<> } the while loop never gets executed.
I've looked at the contents of the database after moving the file to my computer and opening the database using this db browser and I can see my entry.
Here's the method that I call to get all the points from my database:
List<PointValue> getAllPoints() {
List<PointValue> points;
Cursor data = rawQuery("SELECT * FROM history");
if (data.moveToNext()) {
points = new ArrayList<>();
while (data.moveToNext()) {
System.out.println("Looped");
long timestamp = data.getLong(data.getColumnIndexOrThrow("timestamp"));
int level = data.getInt(data.getColumnIndexOrThrow("level"));
points.add(new PointValue(timestamp, level));
}
} else {
return null;
}
data.close();
if (points.size() == 0) {
return null;
}
return points;
}
The rawQuery method looks like this:
private Cursor rawQuery(String sql) {
SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery(sql, null);
}
When I tried debugging this on my own, the size of points is 0 even though I know that there's at least one point in the database. Thoughts? The class containing all of my sql related stuff extends SQLiteOpenHelper
EDIT:
Here's the solution suggested by #Isaac Payne (still doesn't work):
public List<PointValue> getAllPoints() {
List<PointValue> points = new ArrayList<>();
Cursor data = rawQuery("SELECT * FROM history");
while (data.moveToNext()) {
long timestamp = data.getLong(data.getColumnIndexOrThrow("timestamp"));
int level = data.getInt(data.getColumnIndexOrThrow("level"));
points.add(new PointValue(timestamp, level));
}
data.close();
if (points.size() == 0) {
return null;
}
return points;
}
The issue is that when you call data.moveToNext() in the if statement you are moving to the first entry, then you call moveToNext() again in your while loop moving to the second non-existent entry. Try removing the if statement
Add data.moveToFirst() before if loop.
Cursor data = rawQuery("SELECT * FROM history");
//add this line
data.moveToFirst();
if (data.moveToNext()) {

Understanding Memcache

I am a newbie android developer. I am trying to build a app engine based application. I could make it work on for now but I realised I need to use memcache to optimize database accesses. But I have a bad time understanding some basic concepts. So my questions are as follows:
Is memcache programming only about app engine? Is there nothing to do with android side? (I mean if any coding needed at android application?)
I used JPA to program my app engine app. Can I use low level API for memcache?
I got this example on a book but it usses many HTTP references. Is this type of example usable for android app also or is it only for websites' usage?.
public class ETagCacheServlet extends HttpServlet {
private static final long serialVersionUID = 4308584640538822293L;
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
MemcacheService cache = MemcacheServiceFactory
.getMemcacheService();
String cacheKey = request.getRequestURI() + "." + "etag";
String result;
if (!cache.contains(cacheKey) ||
!cache.get(cacheKey).equals(request
.getHeader("If-None-Match"))) {
String etag = Long.toString(System.currentTimeMillis());
response.setHeader("ETag", etag);
cache.put(cacheKey, etag);
result = "Loaded into cache at " + (new Date());
response.getWriter().write(result);
} else {
response.setStatus(304);
}
}
}
Do you know any source that has a working sample app or something?
Maybe you laugh reading these questions but I really cannot figure these things out. Thanks in advance.
Edit: I tried to add memcache to my code unsuccessfully, can you take a look at the code please?
#SuppressWarnings({ "unchecked", "unused" })
#ApiMethod(name = "queryDesire")
public CollectionResponse<Desire> queryDesire(
#Nullable #Named("cursor") String cursorString,
#Nullable #Named("limit") Integer limit,
#Nullable #Named("first") Integer first,
#Nullable #Named("name") String name){
EntityManager mgr = null;
Cursor cursor = null;
List<Desire> execute = null;
try {
String keyDesire = "mem_" + name;
List<Desire> memDesire = (List<Desire>) memcache.get(keyDesire);
if (memDesire == null) {
mgr = getEntityManager();
Query query2 = mgr.createQuery("select i from Desire i where i.ctgry = :name ");
if (cursorString != null && cursorString != "") {
cursor = Cursor.fromWebSafeString(cursorString);
query2.setHint(JPACursorHelper.CURSOR_HINT, cursor);
}
if (limit != null) {
query2.setFirstResult(first);
query2.setMaxResults(limit);
}
execute = (List<Desire>) query2.setParameter("name", name).getResultList();
cursor = JPACursorHelper.getCursor(execute);
if (cursor != null)
cursorString = cursor.toWebSafeString();
for (Desire obj : execute)
;
CollectionResponse.<Desire> builder().setItems(execute)
.setNextPageToken(cursorString).build();
memcache.put("mem_cache", queryDesire);
}
return CollectionResponse.<Desire> builder().setItems(execute)
.setNextPageToken(cursorString).build();
}
finally {
mgr.close();
}
}
Memcache is used on the server-side, i.e. App Engine. It is often used to speed up the responses from the server to the client, but it is not related to the client code. In other words, no changes are necessary on the client side if you use Memcache on App Engine side.
Yes, you can use low-level API for Memcache.
See response to question 1. Memcache can be used regardless of how an app communicates with the server.
Memcache is used in many different ways, so you may need to post a specific question, and we may be able to help. Meanwhile, here is an example from my code. Time zones are frequently required for my app, and they never change. So it makes sense to use Memcache to speed up the response.
private static final MemcacheService memcache = MemcacheServiceFactory.getMemcacheService();
public static ArrayList<String> getTimeZones() {
ArrayList<String> timeZones = (ArrayList<String>) memcache.get("time_zones");
if (timeZones == null) {
// This method reads time zones from a properties file
timeZones = prepareTimeZones();
memcache.put("time_zones", timeZones);
}
return timeZones;
}

Database insertion taking too much time- android sqlite

I am trying to insert around 2800 records into the sqlite database, it is taking 150 sec, which is way too much! Could anyone please tell how to optimize this insertion.
public void createVariantEntry(ArrayList<ArrayList<String>> str) {
InsertHelper ih = new InsertHelper(Database, VARIANT_TABLE_NAME);
final int varid = ih.getColumnIndex(VARIANT_ID);
final int varmakeid = ih.getColumnIndex(VARIANT_MAKE_ID);
final int varmodid = ih.getColumnIndex(VARIANT_MODEL_ID);
final int varname = ih.getColumnIndex(VARIANT_NAME);
final int varposteddate = ih.getColumnIndex(VARIANT_POSTED_DATE);
for(int i=0;i<1253;i++)
{
ih.prepareForInsert();
ih.bind(varid, str.get(i).get(0));
ih.bind(varmakeid, str.get(i).get(1));
ih.bind(varmodid, str.get(i).get(2));
ih.bind(varname, str.get(i).get(3));
ih.bind(varposteddate, str.get(i).get(4));
ih.execute();
}
for(int i=1255;i<str.size();i++)
{
ih.prepareForInsert();
ih.bind(varid, str.get(i).get(0));
ih.bind(varmakeid, str.get(i).get(1));
ih.bind(varmodid, str.get(i).get(2));
ih.bind(varname, str.get(i).get(3));
ih.bind(varposteddate, str.get(i).get(4));
ih.execute();
}
ih.close();
}
a great boost in performance will be gained when using transactions.
try {
SQLiteDatabase db = MySQLiteOpenHelper.getWritableDatabse();
ContentValues values = new ContentValues();
db.beginTransaction();
while ( more_data_to_insert ) {
// put the data in 'values'
values.put("col_1", data_1);
values.put("col_2", data_2);
// ...
values.put("col_n", data_n);
// Insert the row into the database.
db.insert("table_name", null, values);
}
db.setTransactionSuccessful();
} catch ( SQLiteException e ) {
// handle your sqlite errors
} finally {
db.endTransaction();
}
and don't use InsertHelper. its deprecated now.
Here are some general tips that might help you:
You can bulkInsert or applyBatch using ContentProviders to do a bunch of operations in one go:
How to use bulkInsert() function in android?
You can use transactions to speed things up as well:
Android Database Transaction
In some cases DatabaseUtils.InsertHelper has been known to provide faster inserts than the normal sqlite insert:
http://www.outofwhatbox.com/blog/2010/12/android-using-databaseutils-inserthelper-for-faster-insertions-into-sqlite-database/
After this, You'll have to do some benchmarking and optimize for your specific situation analyzing performance vs data integrity tradeoffs etc. Good luck.

Calendar Instances provider source code?

I am looking through the android calendar source code for the Instances Content Provider to see how it works and gets populated.
The reason being is that I am trying to replicate its workings in my app but I have not found where it gets populated from in the source.
I know the Instances database cannot be written to but somewhere it has to be getting written to since its getting populated. I just want to see how they do some of the calculations for the values.
The only thing I can find about the Instances is this but that does not tell me what I want to know and just tells me the the query and the uri's not the code behind the values.
does anyone know where it is?
The code that handles Content Provider URIs that start with content://com.android.calendar/instances/* is in packages/providers/CalendarProvider/src/com/android/providers/calendar/CalendarProvider2.java.
From that file it is possible to look up further implementation details. For example, a lot of the initialization code, like CREATE TABLE calls, is in CalendarDatabaseHelper.java in the same project.
I hope this helps!
Instances are being populated in "updateInstancesLocked" method in CalendarInstancesHelper.java. Also please check "performInstanceExpansion" method in the same file.
Link to the above mention method in GrepCode
I have provided the snippet of the method below
/**
* Updates the instances table when an event is added or updated.
* #param values The new values of the event.
* #param rowId The database row id of the event.
* #param newEvent true if the event is new.
* #param db The database
*/
private void updateInstancesLocked(ContentValues values,
long rowId,
boolean newEvent,
SQLiteDatabase db) {
// If there are no expanded Instances, then return.
MetaData.Fields fields = mMetaData.getFieldsLocked();
if (fields.maxInstance == 0) {
return;
}
Long dtstartMillis = values.getAsLong(Events.DTSTART);
if (dtstartMillis == null) {
if (newEvent) {
// must be present for a new event.
throw new RuntimeException("DTSTART missing.");
}
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Missing DTSTART. No need to update instance.");
}
return;
}
Long lastDateMillis = values.getAsLong(Events.LAST_DATE);
Long originalInstanceTime = values.getAsLong(Events.ORIGINAL_INSTANCE_TIME);
if (!newEvent) {
// Want to do this for regular event, recurrence, or exception.
// For recurrence or exception, more deletion may happen below if we
// do an instance expansion. This deletion will suffice if the exception
// is moved outside the window, for instance.
db.delete("Instances", "event_id=?", new String[] {String.valueOf(rowId)});
}
String rrule = values.getAsString(Events.RRULE);
String rdate = values.getAsString(Events.RDATE);
String originalEvent = values.getAsString(Events.ORIGINAL_EVENT);
if (isRecurrenceEvent(rrule, rdate, originalEvent)) {
// The recurrence or exception needs to be (re-)expanded if:
// a) Exception or recurrence that falls inside window
boolean insideWindow = dtstartMillis <= fields.maxInstance &&
(lastDateMillis == null || lastDateMillis >= fields.minInstance);
// b) Exception that affects instance inside window
// These conditions match the query in getEntries
// See getEntries comment for explanation of subtracting 1 week.
boolean affectsWindow = originalInstanceTime != null &&
originalInstanceTime <= fields.maxInstance &&
originalInstanceTime >= fields.minInstance - MAX_ASSUMED_DURATION;
if (insideWindow || affectsWindow) {
updateRecurrenceInstancesLocked(values, rowId, db);
}
// TODO: an exception creation or update could be optimized by
// updating just the affected instances, instead of regenerating
// the recurrence.
return;
}
Long dtendMillis = values.getAsLong(Events.DTEND);
if (dtendMillis == null) {
dtendMillis = dtstartMillis;
}
// if the event is in the expanded range, insert
// into the instances table.
// TODO: deal with durations. currently, durations are only used in
// recurrences.
if (dtstartMillis <= fields.maxInstance && dtendMillis >= fields.minInstance) {
ContentValues instanceValues = new ContentValues();
instanceValues.put(Instances.EVENT_ID, rowId);
instanceValues.put(Instances.BEGIN, dtstartMillis);
instanceValues.put(Instances.END, dtendMillis);
boolean allDay = false;
Integer allDayInteger = values.getAsInteger(Events.ALL_DAY);
if (allDayInteger != null) {
allDay = allDayInteger != 0;
}
// Update the timezone-dependent fields.
Time local = new Time();
if (allDay) {
local.timezone = Time.TIMEZONE_UTC;
} else {
local.timezone = fields.timezone;
}
computeTimezoneDependentFields(dtstartMillis, dtendMillis, local, instanceValues);
mDbHelper.instancesInsert(instanceValues);
}
}
Hope this helps!!!
Let me know if you are expecting something different

Android: SQLite transactions when using ContentResolver

The goal: refresh database from XML data
The process:
Start transaction
Delete all existing rows from the tables
Per each main element of parsed XML insert row into main table and get PK
Per each child of the main element insert record into 2nd table providing FK from the previous step
Commit transaction
Pretty standard stuff as far as db operations. The problem is that CRUD operations are not done within ContentProvider but rather using ContentResolver so the insert for example looks like resolver.insert(CONTENT_URI, contentValues). The ContentResolver API doesn't seem to have anything pertained to transaction and I cannot use bulkInsert since I'm inserting in 2 tables intermittently (plus I want to have delete inside the transaction as well).
I was thinking of registering my customized ContentProvider as listener by using registerContentObserver but since ContentResolver#acquireProvider methods are hidden how do I obtain the right reference?
Am I out of luck?
I've seen that in the source code of Google I/O application, they override ContentProvider's applyBatch() method and use transactions inside of it. So, you create a batch of ContentProviderOperation s and then call getContentResolver().applyBatch(uri_authority, batch).
I'm planning to use this approach to see how it works. I'm curious if anyone else has tried it.
It is possible to do transaction based multi table inserts rather cleanly since Android 2.1 by using ContentProviderOperation, as mentioned by kaciula.
When you build the ContentProviderOperation object, you can call .withValueBackReference(fieldName, refNr). When the operation is applied using applyBatch, the result is that the ContentValues object that is supplied with the insert() call will have an integer injected. The integer will be keyed with the fieldName String, and its value is retrieved from the ContentProviderResult of a previously applied ContentProviderOperation, indexed by refNr.
Please refer to the code sample below. In the sample, a row is inserted in table1, and the resulting ID (in this case "1") is then used as a value when inserting the row in table 2. For brevity, the ContentProvider is not connected to a database. In the ContentProvider, there are printouts where it would be suitable to add the transaction handling.
public class BatchTestActivity extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<ContentProviderOperation> list = new
ArrayList<ContentProviderOperation>();
list.add(ContentProviderOperation.
newInsert(BatchContentProvider.FIRST_URI).build());
ContentValues cv = new ContentValues();
cv.put("name", "second_name");
cv.put("refId", 23);
// In this example, "refId" in the contentValues will be overwritten by
// the result from the first insert operation, indexed by 0
list.add(ContentProviderOperation.
newInsert(BatchContentProvider.SECOND_URI).
withValues(cv).withValueBackReference("refId", 0).build());
try {
getContentResolver().applyBatch(
BatchContentProvider.AUTHORITY, list);
} catch (RemoteException e) {
e.printStackTrace();
} catch (OperationApplicationException e) {
e.printStackTrace();
}
}
}
public class BatchContentProvider extends ContentProvider {
private static final String SCHEME = "content://";
public static final String AUTHORITY = "com.test.batch";
public static final Uri FIRST_URI =
Uri.parse(SCHEME + AUTHORITY + "/" + "table1");
public static final Uri SECOND_URI =
Uri.parse(SCHEME + AUTHORITY + "/" + "table2");
public ContentProviderResult[] applyBatch(
ArrayList<ContentProviderOperation> operations)
throws OperationApplicationException {
System.out.println("starting transaction");
ContentProviderResult[] result;
try {
result = super.applyBatch(operations);
} catch (OperationApplicationException e) {
System.out.println("aborting transaction");
throw e;
}
System.out.println("ending transaction");
return result;
}
public Uri insert(Uri uri, ContentValues values) {
// this printout will have a proper value when
// the second operation is applied
System.out.println("" + values);
return ContentUris.withAppendedId(uri, 1);
}
// other overrides omitted for brevity
}
All right - so this does not dingle aimlessly: the only way I can think of is to code startTransaction and endTransaction as URL-based query requests. Something like ContentResolver.query(START_TRANSACTION, null, null, null, null). Then in ContentProvider#query based on the registered URL call start or end transaction
You can get the implementation of the content provider object itself (if in the same process, hint: you can control the provider's process with multiprocess="true" or process="" http://developer.android.com/guide/topics/manifest/provider-element.html) using ContentProviderClient.getLocalContentProvider () which can be casted to your provider implementation which can provide extra functionality like a reset() that closes and deletes the database and you can also return a custom Transaction class instance with save() and close() methods.
public class Transaction {
protected Transaction (SQLiteDatabase database) {
this.database = database;
database.beginTransaction ();
}
public void save () {
this.database.setTransactionSuccessful ();
}
public void close () {
this.database.endTransaction ();
}
private SQLiteDatabase database;
}
public Transaction createTransaction () {
return new Transaction (this.dbHelper.getWritableDatabase ());
}
Then:
ContentProviderClient client = getContentResolver ().acquireContentProviderClient (Contract.authorityLocal);
Transaction tx = ((LocalContentProvider) client.getLocalContentProvider ()).createTransaction ();

Categories

Resources