Reputation: 22028
I'm trying out ormdroid as an ORM tool for Android. I have a list of Objects of the class Favorite that I want to persist, every now and then I save a List of those to disk with ormdroid.
Those Favorites that the user rmeoves from the List should be removed from the disk too of course so I thought of first deleting all Favorites from the disk, then saving the List again:
List<Favorite> oldFavs = Entity.query(
Favorite.class).executeMulti();
for (Favorite f : oldFavs) {
f.delete();
Log.d("ORM", "Deleting: " + f.getTitle());
}
and then saving my favorites again:
for (Favorite f : myFavorites) {
f.save();
Log.d("ORM", "Saving: " + f.getTitle());
}
From putting a lot of Log.d() in my code I found out the following:
After deleting all Favorites from the disk, a query with ormdroid indeed gives back a List of size 0. But when I save my Favorites again, some of them (those that were already there in the old record) are updated, not newly created from the f.save() method, but still not available to a query.
Is there anyone actively using ormlite that can help me out here or someone that can verify that the code from the delete() method of ormlite actually and definitely deletes this record from the database:
public void delete() {
if (!mTransient) {
SQLiteDatabase db = ORMDroidApplication.getDefaultDatabase();
db.beginTransaction();
try {
delete(db);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
db.close();
}
}
Upvotes: 1
Views: 441
Reputation: 11
I also had some problems deleting entities with ORMDroid, it seems like the deletions are carried out AFTER the insertions, even though the code is in correct order. This might not be an issue with ORMDroid though, but some SQLite quirk that I can't get my head around right now. Anyway, I was able to bypass the problem this way for the moment, just switching the order of deletion/insertion really:
// First create the list of items to delete.
// Don't delete them just yet though.
List<Favorite> oldFavs = Entity.query(Favorite.class).executeMulti();
// Insert new ones
for (Favorite f : myFavorites) {
f.save();
}
// Delete the old ones
for (Favorite f : oldFavs) {
f.delete();
}
Upvotes: 1