Source:
<code>protected static void migrateV4FormRecords(Context c, SQLiteDatabase db) { SqlStorage<FormRecordV4> oldStorage = getFormRecordStorage(c, db, FormRecordV4.class); Vector<FormRecord> newRecords = new Vector<>(); for (FormRecordV4 oldRecord : oldStorage) { FormRecord newRecord = new FormRecord( oldRecord.getStatus(), oldRecord.getFormNamespace(), oldRecord.getAesKey(), oldRecord.getInstanceID(), oldRecord.lastModified(), oldRecord.getAppId()); String instanceUri = oldRecord.getInstanceURIString(); Cursor cursor = c.getContentResolver().query(Uri.parse(instanceUri), null, null, null, null); <START> if (cursor != null && cursor.getCount() > 0) { <END> cursor.moveToFirst(); newRecord.setDisplayName(cursor.getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.DISPLAY_NAME))); newRecord.setFilePath(cursor.getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.INSTANCE_FILE_PATH))); newRecord.setCanEditWhenComplete(cursor.getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.CAN_EDIT_WHEN_COMPLETE))); cursor.close(); } newRecords.add(newRecord); } db.execSQL("DROP TABLE IF EXISTS " + FormRecord.STORAGE_KEY); TableBuilder builder = new TableBuilder(FormRecord.class); db.execSQL(builder.getTableCreateString()); SqlStorage<FormRecord> newStorage = getFormRecordStorage(c, db, FormRecord.class); for (FormRecord newRecord : newRecords) { newStorage.write(newRecord); } c.getContentResolver().delete(InstanceProviderAPI.InstanceColumns.CONTENT_URI, null, null); }</code><technical_language>quick note technically cursor operations wrapped in try{}closes</technical_language>

Target:
protected static Vector<Uri> migrateV4FormRecords(Context c, SQLiteDatabase db) { SqlStorage<FormRecordV4> oldStorage = getFormRecordStorage(c, db, FormRecordV4.class); Vector<Uri> migratedInstances = new Vector<>(); Vector<Pair<FormRecord, Uri>> newRecords = new Vector<>(); for (FormRecordV4 oldRecord : oldStorage) { FormRecord newRecord = new FormRecord( oldRecord.getStatus(), oldRecord.getFormNamespace(), oldRecord.getAesKey(), oldRecord.getInstanceID(), oldRecord.lastModified(), oldRecord.getAppId()); Uri instanceUri = Uri.withAppendedPath(Uri.parse(oldRecord.getInstanceURIString()), oldRecord.getAppId()); Cursor cursor = null; try { cursor = c.getContentResolver().query(instanceUri, null, null, null, null); if (cursor != null && cursor.getCount() > 0) { cursor.moveToFirst(); newRecord.setDisplayName(cursor.getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.DISPLAY_NAME))); newRecord.setFilePath(cursor.getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.INSTANCE_FILE_PATH))); newRecord.setCanEditWhenComplete(cursor.getString(cursor.getColumnIndex(InstanceProviderAPI.InstanceColumns.CAN_EDIT_WHEN_COMPLETE))); } } finally { if (cursor != null) { cursor.close(); } } newRecords.add(new Pair<>(newRecord, instanceUri)); } db.execSQL("DROP TABLE IF EXISTS " + FormRecord.STORAGE_KEY); TableBuilder builder = new TableBuilder(FormRecord.class); db.execSQL(builder.getTableCreateString()); SqlStorage<FormRecord> newStorage = getFormRecordStorage(c, db, FormRecord.class); for (Pair entry : newRecords) { newStorage.write(((FormRecord)entry.first)); migratedInstances.add(((Uri)entry.second)); } return migratedInstances; }