I am able to open the database from within the main app activity using the following code, but once it's wrapped into a singleton object, it keeps throwing a null-object error:
object CommonClass {
fun openSQLDatabase(): SQLiteDatabase? {
var dbase: SQLiteDatabase? = null
try {
dbase = openOrCreateDatabase(
"dbfile.sqlite",
Context.MODE_PRIVATE, null
)
} catch (e: SQLException) {
println(e.message)
}
return dbase
}
}
I'm assuming that the main AppCompatActivity should be passing its context to the object in some way, but I've not been able to find a working model.
to Swayangjit
Android Studio highlights the Context.MODE_PRIVATE parameter and flags it as:
Type mismatch.
Required: SQLiteDatabase.CursorFactory?
Found: Int
But when I implement the AppCompatActivity to the singleton object and pass the Context.MODE_PRIVATE from the main activity, it runs but throws this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory)' on a null object reference
I believe the following will work :
object CommonClass {
fun openSQLDatabase(context: Context): SQLiteDatabase? {
var dbase: SQLiteDatabase? = null
if (dbase == null) {
try {
dbase = openOrCreateDatabase(context.getDatabasePath("dbfile.sqlite"), null)
} catch (e: SQLException) {
println(e.message)
}
}
return dbase
}
}
Note this assumes that you want the database in the default location i.e. data/data/the_package_name/databases/dbfile.sqlite
You could invoke/call it using something like :-
val mydb = CommonClass.openSQLDatabase(this)
Related
I am recently working with Flow in my retrofit's repository.
Sealed class for Result
enum class ApiStatus{
SUCCESS,
ERROR,
LOADING
}
sealed class ApiResult <out T> (val status: ApiStatus, val data: T?, val message:String?) {
data class Success<out R>(val _data: R?): ApiResult<R>(
status = ApiStatus.SUCCESS,
data = _data,
message = null
)
data class Error(val exception: String): ApiResult<Nothing>(
status = ApiStatus.ERROR,
data = null,
message = exception
)
data class Loading<out R>(val _data: R?, val isLoading: Boolean): ApiResult<R>(
status = ApiStatus.LOADING,
data = _data,
message = null
)
}
Example repository call for 3 state - Loading, Error, Success
fun googleDisconnect() = flow {
emit(ApiResult.Loading(null, true))
val call = userDataSource.self("v4").googleDisconnect()
if(call.isSuccessful) {
emit(ApiResult.Success(call.body()))
} else {
emit(ApiResult.Error("Google Disconnect Failed"))
}
}
However, I have multiple network call with different function in my repository. Is there any idea to write a generic function for these flow so that these flow can be emitted to the flow builder?
My attempt but problem is How can I pass suspend function into the function?
Finally I got myself the answer. I wonder if this will helps but I will post out my answer.
fun <T> toResultFlow(call: suspend () -> Response<T>?) : Flow<ApiResult<T>?> {
return flow {
emit(ApiResult.Loading())
val c = call() <-- have to initialize the call method first
c?.let {
try{
if(c.isSuccessful) {
c.body()?.let {
emit(ApiResult.Success(it))
}
} else {
c.errorBody()?.let {
val error = it.string()
it.close()
emit(ApiResult.Error(error))
}
}
}catch (e: Exception) {
emit(ApiResult.Error(e.toString()))
}
}
}.flowOn(Dispatchers.IO)
}
Then, pass in your suspend function as lambda
fun googleDisconnect() = toResultFlow {
userDataSource.self("v4").googleDisconnect()
}
Finally, the toResultFlow will be return Flow<ApiResult> and T is your preferred datatype! Volla!
I am trying to persist a timestamp in my room database using the following TypeConverter:
class Converters {
#TypeConverter
fun fromTimestamp(value: Long?): Calendar? {
if(value == null) return null
val cal = GregorianCalendar()
cal.timeInMillis = value
return cal
}
#TypeConverter
fun toTimestamp(timestamp: Calendar?): Long? {
if(timestamp == null) return null
return timestamp.timeInMillis
}
}
Two of my Entities include the following column
:
#ColumnInfo(name = "timestamp")
val timestamp: Calendar?,
But I get a compilation error upon trying to build the project - I had no issues when using the Date TypeConverter example from the developer reference guide.
I am unable to see what the actual error is as I just get a bunch of databinding 'cannot find symbol' errors if there is something wrong with the code related to Room.
Use:
object Converters {
#TypeConverter
#JvmStatic
fun fromTimestamp(value: Long?): Calendar? = value?.let { value ->
GregorianCalendar().also { calendar ->
calendar.timeInMillis = value
}
}
#TypeConverter
#JvmStatic
fun toTimestamp(timestamp: Calendar?): Long? = timestamp?.timeInMillis
}
And
#TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
I'm trying to optimise the performances in my app and I noticed that I do not remove Firestore listeners from my repository.
My repository has a number of functions that return a LiveData, that is then observed via Transformations from ViewModels and then the views.
One-time operations work absolutely fine (upload, delete etc.) but permanent listeners don't get garbage collected when the activity finishes.
Right now the function inside the repository looks like this:
// [...]
class Repository {
// [...]
fun retrieveCode() {
val observable = MutableLiveData<Code>()
val reference =
FirebaseFirestore.getInstance().collection(/**/).document(/**/)
reference
.addSnapshotListener { snapshot, exception ->
if(exception != null) {
observable.value = null
}
if(snapshot != null {
observable.value = snapshot.//[convert to object]
}
}
return observable
}
}
I found a workaround which is to create a custom LiveData object that handles the listener removal when it becomes inactive, like this:
class CodeLiveData(private val reference: DocumentReference):
LiveData<Code>(), EventListener<DocumentSnapshot>{
private var registration: ListenerRegistration? = null
override fun onEvent(snapshot: DocumentSnapshot?,
exception: FirebaseFirestoreException?) {
if(exception != null) {
this.value = null
}
if(snapshot != null) {
this.value = snapshot.//[convert to object]
}
}
override fun onActive() {
super.onActive()
registration = reference.addSnapshotListener(this)
}
override fun onInactive() {
super.onInactive()
registration?.remove()
}
}
Is there a way to solve this problem without creating a custom class, but rather by improving a function similar to the first example?
Thanks,
Emilio
There are two ways in which you can achieve this. The first one would be to stop listening for changes and this can be done in your onStop() function by calling remove() function on your ListenerRegistration object like this:
if (registration != null) {
registration.remove();
}
The approach would be to you pass your activity as the first argument in the addSnapshotListener() function, so Firestore can clean up the listeners automatically when the activity is stopped.
var registration = dataDocumentReference
.addSnapshotListener(yourActivity, listener)
I am trying to write a method that takes a username as its input and creates a SQL query statement which is then executed. As of right now, I am getting a NullPointer when I try to call result.getObject().
Here is my method for creating the SQL statement dynamically:
fun getByUsername(name: String): User?{
val sql = """SELECT * FROM usertable WHERE username="$name";"""
val result = DatabaseController().trySql(sql)
if (result != null) {
if (!result.isBeforeFirst) {
println("User was not found...")
return null
} else {
println("User found....")
return User(result.getObject("username").toString(),
result.getObject("password").toString(),
result.getObject("admin").toString().toBoolean())
}
}
return null
}
Method that executes query:
fun trySql(sqlCommand: String): ResultSet? {
var conn = this.connect()
println("trySql()-------Running query $sqlCommand")
var result = conn?.createStatement()?.executeQuery(sqlCommand)
conn?.close()
return result
}
The username I am running it on is in my table too, so that is not the issue. My method that creates a row in my table is working properly, so I know there is no issue with connecting to the database.
EDIT
If I just return result in trySql(), I no longer get any errors, but from my reading I am supposed to close connections after use?
Why you are having issues
You are closing the connection, which makes the ResultSet you get back no longer useful. You will have access to the result object, but the result object will be unable to read data from SQLite, leading to errors.
How to fix
You can pass some code to your trySql function, which will be performed with the connection still open, and then close after the passed block.
Example
Library
class Con(val con: Connection = DriverManager.getConnection(URL)): AutoCloseable by con {
fun <T> withSQL(query: String, block: (ResultSet)->T): T {
con.createStatement().use { statement ->
statement.executeQuery(query).use { results ->
return block(results)
}
}
}
}
Usage
Con().use { con ->
val result: Int = con.withSQL("SELECT * from example WHERE name='test'") {
it.getInt(1)
}
}
I 've built an ASP.NET website using EF. I created a DataContext class which implements the singleton pattern. My DAO classes (singletons too) instanciate this datacontext and store it in a property. They use it in order to query the SQLServer DataBase. This worked ok for 3 months but I suddenly got exception messages like :"Connection must be valid and open / connection already open". It seemed that datacontext was not disposed. The only change, according to me, was the data size and number of users increasing.
I then found multiple posts saying that singleton was a bad idea foe datacontext, so I tried to instanciate datacontext in a using statement in every request and that resolved the problem, except for update queries which had no effects in database. I had to attach the db object to the context and then set its EntityState to "modified" to have my SaveChanges work.
Like this :
public bool DoucheXpsu(as_headers session) {
using (MyDBEntities MyContext = new MyDBEntities()) {
try {
as_status status = GetStatus(session);
if (status != null) {
if (status.mainstatusvalue == 300) {
status.DateDoucheXpsu = DateTime.Now;
status.DoucheXpsu = 1;
MyContext.as_status.Attach(status);
MyContext.ObjectStateManager.ChangeObjectState(status, EntityState.Modified);
MyContext.SaveChanges();
return true;
} else {
return false;
}
} else {
return false;
}
} catch (OptimisticConcurrencyException) {
return false;
} catch (Exception) {
return false;
}
}
}
The problem is that it actually didn't work for ONE method (which has nothing different from the other update method) !
The exception occured as I tried to attach the object : "The object cannot be attached because it is already in the object context. An object can only be reattached when it is in an unchanged state. " So I had to comment the attach and ChangeObjectState methods to have it work as expected :
public bool SetSessionToDelete(string numSession) {
using (MyDBEntities MyContext = new MyDBEntities()) {
try {
view_headerStatus view = (from v in MyContext.view_headerStatus
where v.CodeSession == numSession
where v.lastinserted == 1
select v).First();
if (view != null) {
as_status status = (from s in MyContext.as_status
where s.jobclsid == view.jobclsid
where s.lastinserted == 1
select s).First();
if (status != null) {
status.DeleteSession = 1;
//MyContext.as_status.Attach(status);
//MyContext.ObjectStateManager.ChangeObjectState(status, EntityState.Modified);
MyContext.SaveChanges();
return true;
} else {
return false;
}
} else {
return false;
}
} catch (OptimisticConcurrencyException) {
return false;
} catch (Exception) {
return false;
}
}
}
The question is WHY should this one behave differently ???
I've read many posts about EF and dataContext but I feel I'm missing something. I would be glad if anyone can help.
Thanks.
In your first example, this line here:
as_status status = GetStatus(session);
I would assume this populates using a DIFFERENT context, and when it leaves the GetStatus() method the context it used to load is disposed. That is why your subsequent Attach() works. However in your second example you do not need to attach because it was loaded using the current (connected) context.
To solve you may want to either pass the context to your methods like GetStatus() resulting in no need to reattach. I don't typically reattach unless I am resurrecting an object over the wire or from a file.