Skip to content
This repository has been archived by the owner on Jun 30, 2020. It is now read-only.

Question about sample code #17

Closed
skrew opened this issue Sep 29, 2019 · 4 comments
Closed

Question about sample code #17

skrew opened this issue Sep 29, 2019 · 4 comments
Labels
support A request for help using the library

Comments

@skrew
Copy link
Contributor

skrew commented Sep 29, 2019

Hi Gwendal,

I'm playing with GRDBCombine, but i have one question:

In the sample code, in HallOfFameViewModel, you have hardcoded the var maxPlayerCount:

@DatabasePublished(Current.players().hallOfFamePublisher(maxPlayerCount: 10))

How i can make this var dynamic so i can use it like HallOfFameViewModel(maxPlayerCount: 50) ?

When i try to make it dynamic, i got an error (which is normal):

Cannot use instance member 'maxPlayerCount' within property initializer; property initializers run before 'self' is available

@groue
Copy link
Owner

groue commented Sep 29, 2019

Hello @skrew,

Excellent question :-)

The only solution I'm aware of is to perform explicit instantiation of the property wrapper, as below:

class HallOfFameViewModel {
    @DatabasePublished
    var hallOfFame: Result<Players.HallOfFame, Error>
    
    init(maxPlayerCount: Int) {
        let publisher = Current.players().hallOfFamePublisher(maxPlayerCount: maxPlayerCount)
        _hallOfFame = DatabasePublished(publisher)
    }
}

@groue groue added the support A request for help using the library label Sep 29, 2019
@groue
Copy link
Owner

groue commented Sep 29, 2019

If maxPlayerCount has to be provided later, then there is no solution yet and you'd better use raw publishers instead of DatabasePublished.

Generally speaking, I have never tried to feed @DatabasePublished from the SwiftUI environment. @FetchRequest support this, but I have barely scratched the surface in this SO question: https://stackoverflow.com/questions/57668997

@skrew
Copy link
Contributor Author

skrew commented Sep 29, 2019

Yep it's works, thanks !

I'm still not sure to use SwiftUI for my new app (tvOS) because i wanted to reuse a lot of code from iOS version and, as i can see, It won't always be possible... It's maybe too early !

@skrew skrew closed this as completed Sep 29, 2019
@groue
Copy link
Owner

groue commented Sep 29, 2019

Cool! You know, @DatabasePublished is pretty much in flux, and is the most experimental part of the lib. We'll have to really live with Combine and SwitfUI before we know what we need.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
support A request for help using the library
Projects
None yet
Development

No branches or pull requests

2 participants