Categories
discuss

Coroutine Scope inside repository class

Lets assume that i have a list of Dtos, i want to loop throught them and set some values and then insert/update them to my Room database. So from my ViewModel i call the repository class, run the loop inside there and then i call dao.insertItems(list).

fun updateItems(itemList: List<ItemDto>) {
        val readDate = DateUtils.getCurrentDateUTCtoString()
        ???SCOPE???.launch(Dispatchers.IO) {
            for (item in itemList)
                item.readDate = readDate
            itemDao.updateItems(itemList)
        }
    }

The question is what kind of courtineScope do i have to use inside the Repository class. Do i have to create a repositoryScope with Dispatchers.Main..? Perhaps a GlobalScope..?

Answer

You should write your repository APIs as suspend functions instead, like so

suspend fun updateItems(itemList: List<ItemDto>) = withContext(Dispatchers.IO) {
    val readDate = DateUtils.getCurrentDateUTCtoString()
    for (item in itemList)
        item.readDate = readDate
    itemDao.updateItems(itemList)
}

Edit: if you need this to run even after the viewmodel is destroyed, launch it with NonCancellable,

suspend fun updateItems(itemList: List<ItemDto>) = withContext(Dispatchers.IO + NonCancellable) {
    val readDate = DateUtils.getCurrentDateUTCtoString()
    for (item in itemList)
        item.readDate = readDate
    itemDao.updateItems(itemList)
}
Categories
discuss

Injecting @Beans from within a very same @Configuration class idioms

In the past I have seen people using the following 2 idioms to inject dependencies from the same @Configuration:

@Configuration
public class MyConfiguration {

    @Bean
    public MyBeanDependencyA myBeanDependencyA(){
        return new MyBeanDependencyA();
    }

    @Bean . //IDIOM 1
    public MyBeanDependencyB1 myBeanDependencyB1(){
        return new MyBeanDependencyB1(myBeanDependencyA());
    }

    @Bean //IDIOM 2
    public MyBeanDependencyB2 myBeanDependencyB2(MyBeanDependencyA myBeanDependencyA){
        return new MyBeanDependencyB1(myBeanDependencyA);
    }
}

Is there any practical difference between them?

  • Does Spring process the whole instantiation method in each call for IDIOM 1? (relevant if method has any side-effect, might be not idempotent)?
  • Does otherwise Spring inject the global managed instance when injecting for IDIOM 1? (relevant If some external process changes the state of the original singleton bean)

Is Spring container that smart?

Answer

Does Spring process the whole instantiation method in each call for IDIOM 1? (relevant if method has any side-effect, might be not idempotent)?

By default @Configuration classes are proxied at runtime so the MyBeanDependencyA will be created once and myBeanDependencyA() will be called only once by Spring and next calls will be proxied to return the same instance (as far as example that you shared is concerned). There will be only one instance of this bean in the context as it’s scope is Singleton.


Does otherwise Spring inject the global managed instance when injecting for IDIOM 1? (relevant If some external process changes the state of the original singleton bean)

The IOC container will return same instance of Singleton bean when it is queried to do so. Since it is a Singleton all changes to this bean (if it is mutable) will be visible to components that have reference to it.


As a side note you can disable autoproxing of configuration class since Spring 5.2 by using :

@Configuration(proxyBeanMethods = false)

which will prevent proxying calls of methods annotated with @Bean invoked from other @Bean methods.

Categories
discuss

Uncaught TypeError: Cannot read property ‘initializeApp’ of undefined

I’m trying to use a few Firebase libraries in my Vue project (I installed it using npm install firebase).

I added those in main.js:

import { Firebase } from 'firebase/app'
import 'firebase/analytics'
import 'firebase/auth'
import 'firebase/messaging'

Firebase.initializeApp({
  apiKey: 'xxx',
  authDomain: 'xxx',
  databaseURL: 'xxx',
  projectId: 'xxx',
  storageBucket: 'xxx',
  messagingSenderId: 'xxx',
  appId: 'xxx',
  measurementId: 'xxx'
})

And I get:

Uncaught TypeError: Cannot read property ‘initializeApp’ of undefined

Answer

Change this:

import { Firebase } from 'firebase/app'

into this:

import * as firebase from "firebase/app";

import everything from firebase/app then do:

firebase.initializeApp({
  apiKey: 'xxx',
  authDomain: 'xxx',
  databaseURL: 'xxx',
  projectId: 'xxx',
  storageBucket: 'xxx',
  messagingSenderId: 'xxx',
  appId: 'xxx',
  measurementId: 'xxx'
})
Categories
discuss

Firebase firestore timestamp to Formatted Date

I am working on a firebase project and for displaying time regarding and entity, what i got is the timestamp. I am working on a react native project. How to convert the received timestamp to properly formatted date.
This is the timestamp:

enter image description here

I have this time available in this.props.time , and so i tried this.props.time.toDate() but what i got was an error.
Invariant Violation:Objects are not valid as a React child(found: Tue Nov 26 2019 11:51:58 GMT+05:30(Indian Standard Time))

Answer

You can create a Date object with new Date(time.seconds * 1000 + time.nanoseconds/1000000) then manipulate it the way you want.

Categories
discuss

Expiring Daemon because JVM heap space is exhausted?

I just updated the Android Studio to 3.5.0 and I’m getting Expiring Daemon because JVM heap space is exhausted . Message while the build is running. Also, the build is taking more time to complete. Does anyone have any idea regarding this error help me?

Answer

As you add more modules to your app, there is an incredible demand placed on the Android build system, and the default memory settings will not work. To avoid OutOfMemoryErrors during Android builds, you should uncomment the alternate gradle memory setting present in /android/gradle.properties:

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..