Pring icon indicating copy to clipboard operation
Pring copied to clipboard

[Not Issue] Couple of questions on the framework

Open anoop4real opened this issue 5 years ago • 9 comments

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?.

anoop4real avatar Feb 28 '19 11:02 anoop4real

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

1amageek avatar Feb 28 '19 11:02 1amageek

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.

anoop4real avatar Feb 28 '19 12:02 anoop4real

It can be given dynamically by using setRefernece.

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

1amageek avatar Feb 28 '19 13:02 1amageek

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

anoop4real avatar Feb 28 '19 13:02 anoop4real

@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 avatar Feb 28 '19 14:02 1amageek

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

1amageek avatar Mar 01 '19 07:03 1amageek

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??
                })
            }
        }
    }

anoop4real avatar Mar 01 '19 10:03 anoop4real

@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()

1amageek avatar Mar 01 '19 11:03 1amageek

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??**
                })
            }
        }
    }







anoop4real avatar Mar 01 '19 14:03 anoop4real