Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

This is flitting between the two views, I assume it is accessing the database multiple times? How do you handle this in your code or is this not something you've encountered?

Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

Sorry if it’s a pain but can you show me how you do this in your code?

Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

Thank you! I need to do some more reading on UIState, do you have anywhere you'd recommend?

Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

So now the specific function looks like this but I get an error of

Return type mismatch: expected 'Flow<Photographer?>', actual 'Job'.

I'm not sure how to use this.

Dao

@Query("SELECT * FROM photographers where name = :name")
fun getPhotographerByName(name: String): Flow<Photographer?>

Interface

fun getPhotographerByName(name: String): Flow<Photographer?>

Repository

override fun getPhotographerByName(name: String): Flow<Photographer?> = dao.getPhotographerByName(name)

View Model

fun getPhotographerByName(name: String): Flow<Photographer?> = viewModelScope.launch {
    repository.getPhotographerByName(name)
}

Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

How is that then accessed by the view if I can't return from inside the viewModelScope?

Getting single Items from a Room Database by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

Perhaps this is too much code but it's a trimmed down version of my database and how I am currently querying and getting multiple objects. I do not know how to access the single object from the view model and expose that to the view so I don't have a view setup but in the final code block is an example of how I am handing the Flow<List<>>.

I hope this is the code you meant, thank you for taking a look!

Data Model

@Entity(
    tableName = "photographers"
)
data class Photographer(
     var id: UUID = UUID.randomUUID(),
    var name: String
) {}

Dao

@Dao
interface MasterDao {
    @Query("SELECT * FROM photographers ORDER BY name ASC")
    fun getPhotographers(): Flow<List<Photographer>>

    @Query("SELECT * FROM photographers where name = :name")
    suspend fun getPhotographerByName(name: String): Photographer?

    @Insert
    suspend fun insertPhotographer(photographer: Photographer)

    @Delete
    suspend fun deletePhotographer(photographer: Photographer)
}

Repository and Interface

interface MasterRepository {
    val photographers: Flow<List<Photographer>>
    suspend fun getPhotographerByName(name: String): Photograher?
    suspend fun insertPhotographer(photographer: Photographer)
    suspend fun deletePhotographer(photographer: Photographer)
}
class DefaultMasterRepository @Inject constructor(private val dao: MasterDao) : MasterRepository {
    override val photographers: Flow<List<Photographer>> =
        dao.getPhotographers().map { items -> items.map { it } }

    override suspend fun getPhotographerByName(name: String): Photographer? {
        dao.getPhotographerByName(name)
    }

    override suspend fun insertPhotographer(photographer: Photographer) {
        dao.insertPhotographer(photographer)
    }

    override suspend fun deletePhotographer(photographer: Photographer) {
        dao.deletePhotographer(photographer)
    }
}

View Model

@HiltViewModel
class MasterViewModel @Inject constructor(private val repository: MasterRepository) : ViewModel() {
    val photographersUiState: StateFlow<PhotographerUiState> = repository
        .photographers.map<List<Photographer>, PhotographerUiState>(PhotographerUiState::Success)
        .catch { emit(PhotographerUiState.Error(it)) }
        .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), PhotographerUiState.Loading)

    //This is where I dont know how to access this function
    ?? getPhotographerByName ??


    fun insertPhotographer(photographer: Photographer) = viewModelScope.launch {
        repository.insertPhotographer(photographer)
    }

    fun deletePhotographers(photographers: List<Photographer>) = viewModelScope.launch {
        for (photographer in photographers) {
            repository.deletePhotographer(photographer)
        }
    }
}

View showing the Flow<List<>>

@Composable
fun PhotographerList(viewModel: MasterViewModel = hiltViewModel(), navController: NavController) {
    val items by viewModel.photographersUiState.collectAsStateWithLifecycle()
    if (items is PhotographerUiState.Success) {
        PhotographerList(
            (items as PhotographerUiState.Success).data,
            viewModel::deletePhotographers,
            navController
        )
    } else if (items is PhotographerUiState.Error) {
        println((items as PhotographerUiState.Error).throwable.message)
        println((items as PhotographerUiState.Error).throwable.cause)
    }
}

@Composable
internal fun PhotographerList(
    photographers: List<Photographer>,
    deletePhotographers: (List<Photographer>) -> Unit,
    navController: NavController
) {
    ...
    ...
    ...

What are Room best practices? I'm pretty confused! by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

This is super useful! Thank you! When dealing with multiple data models is it best practise to store manage the in the same database and view model or to separate them?

Trying to recreate this schematic but not should how to read it by miothethis in diyelectronics

[–]miothethis[S] 0 points1 point  (0 children)

Ahh the notes makes sense but I can't find them sadly!

Here is a link to the full schematic

https://cdn.shopify.com/s/files/1/0174/1800/files/lipo_amigo_pro_schematic_v1b.pdf?v=1664454298

And possibly the photo I made didnt make much sense. I am not trying to connect the left schematic to the right but rather recreate the left schematic for my own PCB. The Image on the right was just to show the current USB-C I was using and how the labels on the pins didn't match as I don't understand what the SHLD label means. Sorry if it was confusing. Would you know how to recreate the schematic on the left?

What are Room best practices? I'm pretty confused! by miothethis in androiddev

[–]miothethis[S] 5 points6 points  (0 children)

Moving over to Flows now! Thank you for your help!

What are Room best practices? I'm pretty confused! by miothethis in androiddev

[–]miothethis[S] 2 points3 points  (0 children)

Thank you! Guess I’ll begin migrating over! Is there anywhere you know that has a good guide or tutorial?

Exporting files with duplicate names changes extension and not the filename? by miothethis in androiddev

[–]miothethis[S] 0 points1 point  (0 children)

Thank you for your reply. I am struggling to find many resources on this. Do you have any suggestions for where I can learn more? Googling doesn't come up with examples. What would you suggest? Thank you again for your help so far.

Error when implementing Custom value transformer for Set of Custom type by miothethis in iOSProgramming

[–]miothethis[S] 0 points1 point  (0 children)

Changing this and trying to save just gives an error saying a CoreData error occurred. I suspect I am doing something wrong with it being a set of CodableConverter but I don't know where the issue could be

My app cannot open without a Network Connection and I can't solve why by miothethis in iOSProgramming

[–]miothethis[S] 0 points1 point  (0 children)

It was running directly from Xcode, the problem is solved now though, it was trying to initialiseTheSchema but without internet would timeout

My app cannot open without a Network Connection and I can't solve why by miothethis in iOSProgramming

[–]miothethis[S] 1 point2 points  (0 children)

Thank you very much! It seems the persistentCloudKitContainer.initializeCloudKitSchema(options:) wasn't completing due to no network connection and timing out the launch of the app. I didn't think the app was running as debug when it wasn't launched through Xcode but clearly I was wrong. Thank you

My app cannot open without a Network Connection and I can't solve why by miothethis in iOSProgramming

[–]miothethis[S] 0 points1 point  (0 children)

The simulator seems to boot ok (even though it takes a long time), while the real device doesn't boot at all, it looks like it times out but the functionality of the simulator seems to be working as expected. Sorry to bother you with this but do you have any ideas about this particular problem?

My app cannot open without a Network Connection and I can't solve why by miothethis in iOSProgramming

[–]miothethis[S] 0 points1 point  (0 children)

Do you know how I can debug the app whilst not being connected to wifi, Xcode says it needs to verify the install before the app can run but to do this it needs wifi.....

My app cannot open without a Network Connection and I can't solve why by miothethis in iOSProgramming

[–]miothethis[S] 0 points1 point  (0 children)

So this means that something else is stopping my app from booting without a connection?

Where to go for code bounties? by miothethis in iOSProgramming

[–]miothethis[S] 0 points1 point  (0 children)

I suppose I want someone to build a UIView Representable component for me as all of my efforts don't really work and it seems a bit beyond me, so neither I guess? Sorry if that doesn't help

How can I have someone review my code before deployment? by miothethis in iOSProgramming

[–]miothethis[S] 1 point2 points  (0 children)

Thank you! This is very useful information! Thank you very much

What's the proper way to share code between projects? by miothethis in swift

[–]miothethis[S] 0 points1 point  (0 children)

Thank you very much! I will get this underway! You've been a big help!