1

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?

Joozd
  • 501
  • 2
  • 14
  • Did you trying performing them concurrently? – m0skit0 Sep 10 '19 at 20:14
  • As in: each in their own thread? I'll try that! – Joozd Sep 11 '19 at 16:43
  • Maybe 70 threads is too much. You might want to use Kotlin coroutines instead. – m0skit0 Sep 12 '19 at 08:12
  • Putting it all in its own thread didn't make things faster. (I used anko's `doAsync`), I now put the deleting in its thread and the writing in its thread. Speeds things up somewhat, but I am still hoping there is a way to just submit it all at once so I only have a single interaction with the `contentProvider`... – Joozd Sep 12 '19 at 19:15

0 Answers0