I made an app that keeps track of a bunch of events, and puts them into a users calendar. It can get up to 60-70 events (in my case, not a software restriction) and it turns out that removing 60 events and inserting 60 new ones takes up to a minute on my phone. I am wondering if there is a way to do this faster.
I followed the basic guide on https://developer.android.com/guide/topics/providers/calendar-provider to insert and remove events in a calendar, which works nicely, but slow.
fun deleteEvents(eventsList: List<Event>){
val DEBUG_TAG = "DELETERT"
eventsList.forEach {
if (it._id != null) {
val eventID: Long = it._id
val deleteUri: Uri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventID)
val rows: Int = context.contentResolver.delete(deleteUri, null, null)
Log.i(DEBUG_TAG, "Rows deleted: $rows")
}
}
}
fun insertEvents(eventsList: List<Event>, sharedPreferences: SharedPreferences){
eventsList.forEach{ event ->
if (event.start_time.isNotEmpty()) { // should not be empty, prevents crashes if it is
// check event type and if that is to be calendarized:
var putItIn = true
// (...do something which might change putItIn to false)
if (putItIn) {
val values = ContentValues().apply {
put(CalendarContract.Events.DTSTART, event.startInstant)
put(CalendarContract.Events.DTEND, event.endInstant)
put(CalendarContract.Events.TITLE, event.description)
put(CalendarContract.Events.DESCRIPTION, "${event.notes}\n" + IDENTIFIER )
put(CalendarContract.Events.CALENDAR_ID, activeCalendar!!.calID)
put(CalendarContract.Events.EVENT_TIMEZONE, "UTC")
put(CalendarContract.Events.EVENT_LOCATION, event.extra_data)
}
context.contentResolver.insert(CalendarContract.Events.CONTENT_URI, values)
}
}
else Log.d(event.description, event.event_type)
}
}
Above functions are called with a list of Events (which is homemade class that holds the info to be put in the events in the calendar and some other stuff).
Like I said, it can take up to a minute to take care of the whole bunch, which seems slow. Can I speed this up?