For an app I am working on I needed a MergeCursor to use search in a meaningful way. I needed to use two combine two cursors from different sources and I was going to merge them. The only major difference between the code for both cursors was the projection map I used to transform the db-specific column names to those the SearchManager expects.
So when I merged both cursors I got the following error message:
java.lang.IllegalStateException: Observer android.database.MergeCursor$1@44ef69f0 is already registered.
WTF? This message was not helpful. My code doesn't add any observers at all. So why then is an
Observer already registered?
The reason of course was in my code. I used a
MergeCursor that merges two CursorWrapper objects. Being the lazy guy that I am I simply copied the code for the first cursor and pasted it right below itself to finally merge both. And boy, was I bitten by the copy/paste-bug. For the copied code I renamed everything - but forgot to rename the cursor to be wrapped. And this was what caused this weird message to appear.
AbstractCursor inherits from Observable. So my cursor also was an
MergeCursor uses an anynomous DataSetOberserver implementation that it wants to register for each
Cursor to merge. Since I did accidentally use the same cursor twice, also the same observer (the one within
MergeCursor) was registered twice. Thus the exception.
I made a simple mistake in that I didn't update the variable name in one place - and hunted around for way too long. We all know that copy/paste is bad. But, alas, we sometimes forget to act accordingly. Let's hope I learned my lesson this time 🙂
Did you stumble upon anything comparable? Any stories you want to share? Let us know in the comments below.