Is long loop ANR-safe?

What kind of DB are you inserting into?

Look for a DB with Bulk Insert capabilities, like SQLite.