Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Settings to deal with newer settings & add callback threads #450

Merged
merged 15 commits into from
Apr 20, 2024

Conversation

Daeda88
Copy link
Contributor

@Daeda88 Daeda88 commented Dec 20, 2023

Final one for now:

This one updates Firestore/Database to provide settings. This includes fixes for deprecated Persistency settings (though not on Java as that is not tracked yet) and adds support on iOS/Android to set custom callback dispatchers. Otherwise, Firebase internally still goes to the main thread even when running in a background thread.

@Daeda88
Copy link
Contributor Author

Daeda88 commented Jan 3, 2024

@nbransby this is now tracking master, which means the JVM split for firestore is tracked. This PR can now be properly reviewed

@Daeda88
Copy link
Contributor Author

Daeda88 commented Apr 3, 2024

Updated tracking again @nbransby

fun reference(path: String): DatabaseReference
fun reference(): DatabaseReference
fun setPersistenceEnabled(enabled: Boolean)
fun setSettings(settings: Settings)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just
fun setSettings(persistenceEnabled: Boolean = false, persistenceCacheSizeBytes: Long? = null)
What the point of making everyone write the boilerplate
database.setSettings(FirebaseDatabase.Settings.createSettings(persistenceEnabled = true, persistenceCacheSizeBytes = 2048))
?

@nbransby
Copy link
Member

Looking at the official android sdk, the methods are defined as follows:
https://firebase.google.com/docs/reference/android/com/google/firebase/database/FirebaseDatabase#setPersistenceCacheSizeBytes(long)
Any reason not to match this?

@nbransby
Copy link
Member

nbransby commented Apr 13, 2024

This PR inspired me to write a section on compatibility with the official firebase android sdk here: https://github.com/GitLiveApp/firebase-kotlin-sdk?tab=readme-ov-file#compatibility-with-the-official-firebase-android-sdk

Can we apply those points to this PR?

@Daeda88
Copy link
Contributor Author

Daeda88 commented Apr 14, 2024

Yep, will track/correct when I have the time

@Daeda88
Copy link
Contributor Author

Daeda88 commented Apr 15, 2024

Changes now track the Android API. I did leave the LocalCacheSettings as a sealed class as opposed to an interface, so it can both be constructed through a direct constructor as well as through builders as they exist on Android.

@Daeda88 Daeda88 requested a review from nbransby April 15, 2024 10:52
Comment on lines 74 to 76
fun setCallbackQueue(callbackQueue: dispatch_queue_t) {
ios.callbackQueue = callbackQueue
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets not start adding coverage for platform specific APIs, let users use .ios

Comment on lines 59 to 83
@Deprecated("Use dev.gitlive.firebase.firestore instead", replaceWith = ReplaceWith("setSettings(FirebaseFirestore.Settings.create())"))
fun FirebaseFirestore.setSettings(
persistenceEnabled: Boolean? = null,
sslEnabled: Boolean? = null,
host: String? = null,
cacheSizeBytes: Long? = null,
) {
settings = firestoreSettings {
this.sslEnabled = sslEnabled ?: true
this.host = host ?: FirestoreSettings.DEFAULT_HOST
this.cacheSettings = if (persistenceEnabled != false) {
LocalCacheSettings.Persistent(cacheSizeBytes ?: FirestoreSettings.CACHE_SIZE_UNLIMITED)
} else {
val cacheSize = cacheSizeBytes ?: FirestoreSettings.CACHE_SIZE_UNLIMITED
val garbageCollectionSettings = if (cacheSize == FirestoreSettings.CACHE_SIZE_UNLIMITED) {
GarbageCollectorSettings.Eager
} else {
GarbageCollectorSettings.LRUGC(cacheSize)
}
LocalCacheSettings.Memory(garbageCollectionSettings)
}
}
}

@PublishedApi
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving this to an extension will make it a breaking change, why not leave it in the class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FirebaseFirestore is expect/actuals so it cant have default implementation. Omving to the same NativeWrapper pattern used elsewhere now

Copy link
Member

@nbransby nbransby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't comment on every change needed to comply with https://github.com/GitLiveApp/firebase-kotlin-sdk?tab=readme-ov-file#compatibility-with-the-official-firebase-android-sdk but there should be enough comments there so you can apply the same changes to the remaining classes, fields and functions

val FirestoreSettings.Companion.CACHE_SIZE_UNLIMITED get() = -1L
val FirestoreSettings.Companion.DEFAULT_HOST get() = "firestore.googleapis.com"

expect class FirestoreSettings {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename FirebaseFirestoreSettings to match android

suspend fun disableNetwork()
suspend fun enableNetwork()
}

val FirestoreSettings.Companion.CACHE_SIZE_UNLIMITED get() = -1L
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move inside companion object for binary compat with android

suspend fun disableNetwork()
suspend fun enableNetwork()
}

val FirestoreSettings.Companion.CACHE_SIZE_UNLIMITED get() = -1L
val FirestoreSettings.Companion.DEFAULT_HOST get() = "firestore.googleapis.com"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see this property on android?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ill make it internal only


companion object {}

interface Builder {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -0,0 +1,90 @@
package dev.gitlive.firebase.firestore

sealed class LocalCacheSettings {
Copy link
Member

@nbransby nbransby Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sealed interface to match android

Comment on lines 5 to 8
internal companion object {
// Firestore cache defaults to 100MB
const val DEFAULT_CACHE_SIZE = 100L*1024L*1024L
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont see this in the android sdk?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

override fun build(): Persistent = Persistent(sizeBytes)
}
}
data class Memory(val garbaseCollectorSettings: GarbageCollectorSettings) : LocalCacheSettings() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make constructor internal or private

@nbransby
Copy link
Member

Also I used the term binary compat a lot when technically I mean source compatibility

@Daeda88 Daeda88 requested a review from nbransby April 17, 2024 19:28
@Daeda88
Copy link
Contributor Author

Daeda88 commented Apr 17, 2024

Updated with fixes for remarks. Also moved the IllegalStateException thrown when attempting to set new settings after using Firestore into the common code, so that it may be caught properly (before it was impossible to do so on iOS)

@Daeda88
Copy link
Contributor Author

Daeda88 commented Apr 17, 2024

BTW: we have the extent methods
val FirebaseFirestore.android etc now, but I think I found a way to bring it back into the class itself. Since it's more relevant to the other PR however, Ill add it there instead

@nbransby
Copy link
Member

Also moved the IllegalStateException thrown when attempting to set new settings after using Firestore into the common code, so that it may be caught properly (before it was impossible to do so on iOS)

Let's not start copying business logic such as this from the native SDKs as its causes a maintenance headache - you can see here the logic is more complex. Lets leverage our dependency on the native SDKs and let them do the validation.

The fact you can't catch it on iOS is a good thing, an assertion like this represents a programming error, not expected runtime behavior.

@Daeda88
Copy link
Contributor Author

Daeda88 commented Apr 18, 2024

Removed then

@nbransby
Copy link
Member

look like the build failed:
e: file:///home/runner/work/firebase-kotlin-sdk/firebase-kotlin-sdk/firebase-firestore/src/commonMain/kotlin/dev/gitlive/firebase/firestore/firestore.kt:47:17 Unresolved reference: isConfigured

@Daeda88
Copy link
Contributor Author

Daeda88 commented Apr 18, 2024

Whoops, dont do things quickly in the morning :')

@nbransby
Copy link
Member

Js test still failing 😅

@Daeda88
Copy link
Contributor Author

Daeda88 commented Apr 19, 2024

Source changes pointed to an actual bug. Fixed now

@nbransby nbransby merged commit 8daf907 into GitLiveApp:master Apr 20, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants