I don't have a lot of experience writing Android apps. For fun I am writing an app that will upload my call logs to my server. This entire app is running as a Service. The Service (starts when the phone is booted) is the one that registers the ContentObserver, which then calls my custom CallLog
class. I use ContentObserver
to listen for content change events. Unfortunately, the ContentObserver is called multiple times when I e.g. dial a number.
For that reason, I have function that I call after a successful upload (I use Retrofit) called markAsUploaded()
. This function creates a RealmObject
called CallLogUploaded
(which is different from my regular CallLog
model). This CallLogUploaded
simply has one identifier which is the dateTime
of the call, which should be unique enough. Then, when I am iterating through the list of all the call logs, I check every single call log against a isDataUploaded()
function, which does a Realm query and checks to see if there is already a call log with that dateTime stored in the database (realm). In theory, it should work.
However, I have noticed that it does not always work. It seems that very often my data is stale. When I do realm.isAutoRefresh()
, it returns false (although I swear it returned true once). In my isDataUploaded
function, even when I do a findAll()
on the Realm, I do not see all of my data - but the data definitely did hit the markDataAsUploaded
function.
Here's my code - it's in Kotlin but should be easy to understand:
val callLogCall = service.sendCalLLogs(childId, dataToUpload)
callLogCall.enqueue(object : Callback<Void> {
override fun onResponse(call: Call<Void>, response: Response<Void>) {
if (response.isSuccessful) {
Log.i(AppConstants.LOG_TAG, "Call log data uploaded successfully!")
this@CallLogData.markDataAsUploaded(dataToUpload)
} else {
Log.w(AppConstants.LOG_TAG, "Call log data upload failed")
}
}
override fun onFailure(call: Call<Void>, t: Throwable) {
Log.w(AppConstants.LOG_TAG, "Call log data upload error (onFailure) called")
}
})
// This function simply stores a Realm model for all the data that has been uploaded to the server
private fun markDataAsUploaded(dataToUpload: List<CallLog>) {
realm = Realm.getDefaultInstance()
for (data in dataToUpload) {
realm.beginTransaction()
val callLogUploaded = realm.createObject(CallLogUploaded::class.java)
callLogUploaded.callDate = data.callDate
realm.commitTransaction()
}
}
// This function checks to see if the data is already uploaded.
private fun isDataUploaded(callLog: CallLog) : Boolean {
return realm
.where(CallLogUploaded::class.java)
.equalTo("callDate", callLog.callDate)
.count() > 0L
}
// Gets the call logs - not the entire function
for (call in callLogs) {
val callLog = CallLog()
callLog.id = call.id
callLog.callDate = Utilities.getTimestampAsSeconds(call.callDate)
if (this.isDataUploaded(callLog)) {
continue
}
callLog.name = call.name
callLog.number = call.number
}
I am very new to Realm and fairly new to Android development, so I would appreciate any help you can give me. Thanks!