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

[Not Issue] Couple of questions on the framework #59

Open
anoop4real opened this issue Feb 28, 2019 · 9 comments
Open

[Not Issue] Couple of questions on the framework #59

anoop4real opened this issue Feb 28, 2019 · 9 comments

Comments

@anoop4real
Copy link

First of all its an interesting framework and I need a few information to see if I can try it out.

  1. For me the firebase paths are already defined. If that is the case, how can I tell the object to use a particular path? Right now you are using version/modelversion/modelName, which wont work in my case.

  2. I have a nested subcollection usecases in my project as explained here (https://stackoverflow.com/questions/54751023/swift-firestore-sub-collections-custom-objects-and-listeners/54751305#comment96528634_54751305) will this framework ease my flow?.

@1amageek
Copy link
Owner

Hi @anoop4real
Thank your for your message.

  1. You can customize the Object path.
    https://github.com/1amageek/Pring/blob/master/Pring/Object.swift#L33

  2. Pring also supports deep nesting.
    https://github.com/1amageek/Pring/blob/master/PringTests/PackReferenceTests.swift#L48

@anoop4real
Copy link
Author

Hi,

Thanks for the reply,

  1. Can the path be dynamic?

For example: my Country data looks like

apiVersion/Countries/(Country1, Country2...)/States...

So if I set up a State object, the state object should know the path till the particular country document

Right now I am creating the reference dynamically and the State object doesnt know it.

db.collection("apiVersion").document({versionPlaceholder}).collection("Countries").document({countryPlaceHolder}).collection("states")

Hope I am able to explain it.

@1amageek
Copy link
Owner

It can be given dynamically by using setRefernece.

https://github.com/1amageek/Pring/blob/master/Pring/Object.swift#L222

@anoop4real
Copy link
Author

Thanks again, I really appreciate your prompt replies...
I have more queries ;). If I want to retrieve all countries....I would do

db.collection("apiVersion").document({versionPlaceholder}).collection("Countries").getDocuments...

what is the equivalent in Pring? In the sample, most of the places, you are directly getting data by providing id.

Lets say below are my classes, how can I get all countries and their respective states

@objcMembers
class Country: Object{
    
    dynamic var name: String?
    dynamic var capital: String?
    
    let states: NestedCollection<State> = []
}


@objcMembers
class State: Object{
    
    dynamic var name: String?
    dynamic var capital: String?
    
    let provinces: NestedCollection<Province> = []
}

@objcMembers
class Province:Object {    
    dynamic var name: String?
}

I dont get something like Country.getAll

@1amageek
Copy link
Owner

1amageek commented Feb 28, 2019

@anoop4real

Use DataSource for Collection get

https://github.com/1amageek/Pring#datasource

// get
let dataSource = County
Country.query.dataSource().onCompleted( { _, _ in 

}).get()

// listen
let dataSource = County.query.dataSource().onCompleted( { _, _ in 

}).listen()

// Query
Country.where("", "").dataSource()

// SubCollection
let country: Country = Country()
country.states.query.dataSource()

@1amageek
Copy link
Owner

1amageek commented Mar 1, 2019

Hi @anoop4real
Do you have any other questions?
Please close if it is not there.

@anoop4real
Copy link
Author

Hello @1amageek
I have more queries...
My intention is to get a list of countries and each country object should be full that means
if i do a country.state or country.state.province I should get the value, check my post here to see how I am getting it now ...https://stackoverflow.com/questions/54751023/swift-firestore-sub-collections-custom-objects-and-listeners , I would like to avoid this and want to have a better way and I think Pring can help

Below is what I am trying....see if this is the way..

    func getCountries(){
        let country: Country = Country()
        country.setReference(db.collection("apiVersion").document("1").collection("Countries"))
        var dataSrc: DataSource<Country> = country
        dataSrc.query.dataSource().onCompleted { (snapshot, countries) in
            
            // Will this countries contain the States 0r I have to do a loop like this?
            for country in countries{
// How do I set the reference for each state, will it be implied from Country?
                country.states.query.dataSource().onCompleted({ (snapshot, states) in
                    //.loop for provinces??
                })
            }
        }
    }

@1amageek
Copy link
Owner

1amageek commented Mar 1, 2019

@anoop4real

@objcMembers
class State: Object{
    
    dynamic var name: String?
    dynamic var capital: String?
    dynamic var provine: Relation<Province> = .init()
}

@objcMembers
class Province:Object {    
    dynamic var name: String?
}

Please look at READEME

https://github.com/1amageek/Pring#datasource

self.dataSource = User.order(by: \User.updatedAt).dataSource()
    .on({ [weak self] (snapshot, changes) in
        guard let tableView: UITableView = self?.tableView else { return }
        debugPrint("On")
        switch changes {
        case .initial:
            tableView.reloadData()
        case .update(let deletions, let insertions, let modifications):
            tableView.beginUpdates()
            tableView.insertRows(at: insertions.map { IndexPath(row: $0, section: 0) }, with: .automatic)
            tableView.deleteRows(at: deletions.map { IndexPath(row: $0, section: 0) }, with: .automatic)
            tableView.reloadRows(at: modifications.map { IndexPath(row: $0, section: 0) }, with: .automatic)
            tableView.endUpdates()
        case .error(let error):
            print(error)
        }
    })
    .on(parse: { (snapshot, user, done) in
        user.group.get({ (group, error) in
            done(user)
        })
    })
    .onCompleted({ (snapshot, users) in
        debugPrint("completed")
    })
    .listen()

@anoop4real
Copy link
Author

anoop4real commented Mar 1, 2019

Hello @1amageek

I am not sure whether you are able to get my query. Did you have a look at my Stackoverflow post. In the approach explained there, I get all the Countries , states and provinces in one shot through recursive loop and assign those the right hierarchy. ie if I take a Country object, the states and provinces comes along.

Now here are the questions. Read my comments ** in the below code , your readme is not telling me how to do this.

    func getCountries(){
        let country: Country = Country()
        country.setReference(db.collection("apiVersion").document("1").collection("Countries"))
        var dataSrc: DataSource<Country> = country
        dataSrc.query.dataSource().onCompleted { (snapshot, countries) in
            
            **// Will this countries contain the States 0r I have to do a loop like this? Do I have to do this loop?**
            for country in countries{
**// How do I set the reference for each state, will it be implied from Country?**
                country.states.query.dataSource().onCompleted({ (snapshot, states) in
                    **//should I again loop for provinces??**
                })
            }
        }
    }







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

No branches or pull requests

2 participants