Abdullah Jibaly
Abdullah Jibaly

Reputation: 54830

Is an Android select query cursor modified by a delete query?

I am trying to find a way to notify a dependent component when certain rows are deleted, so can I issue a select query, obtain the cursor, then issue the delete or will that modify the original cursor? Is there a better approach altogether? This is what I'm referring to:

Cursor c = builder.query(db, projection, selection, selectionArgs, groupBy, having, sortOrder);
db.delete(table, selection, selectionArgs);
while (cursor != null && cursor.moveToNext()) {
    final String name = cursor.getString(0);
    // send notifications
}
c.close();

Upvotes: 3

Views: 1409

Answers (2)

Ian Warwick
Ian Warwick

Reputation: 4784

If you use a content provider to access your database then you can use ContentObservers which are designed exactly for this purpose:

When you retrieve data through a content provider you do so with a URI and it is this URI that you can use to notify ContentObserver's that the cursor has changed:

getContentResolver ().notifyChange (URI, null)

See the javadocs for notifyChange(...) for more info:

I am afraid to say that you would have to write your own solution for this if you do not use content providers to access your data.

Upvotes: 1

kdehairy
kdehairy

Reputation: 2730

the cursor is a data structure detached from the database. that is, modifying the database by whatever means, does not affect your already fetched cursor.

Update

This appeared to be wrong! .. sorry!

see the comments for details. thanks to @Selvin.

Upvotes: 2

Related Questions