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

Encode object and save with custom key #59

Open
kobazzo opened this issue Nov 4, 2018 · 4 comments
Open

Encode object and save with custom key #59

kobazzo opened this issue Nov 4, 2018 · 4 comments

Comments

@kobazzo
Copy link

kobazzo commented Nov 4, 2018

Hi.
I have an object with some properties. One of this property is an Array of object like:

class Club : NSObject, Codable{
    var name : String!
    var address : String!
    var players: [MPUser] = [MPUser]()
}

class MPUser : NSObject, Codable {
    var firebaseUserId : String!;
    var name : String!;
    var secondName : String!;
}

When i encode this object (Club) for add in cloud Firestore, in Swift 4, i would like to set a custom id every player, in particular i want to use the Firebase User id.

Is it possible to do this?

Thank you.

@alickbass
Copy link
Owner

Hi! I am sorry, but I don't really understand your question... What kind of id are you talking about?

@kobazzo
Copy link
Author

kobazzo commented Nov 4, 2018

When i execute the insert in Cloud Firestore with che Club object encoded, firebase add the players items with key 0, 1, 2 etc...
example

I want to set the key of the player item with a custom value, for example the UID user of the firebase authentication user, then i can query the clubs for login user with owner.

@alickbass
Copy link
Owner

Thanks for clarifying that! The thing is, if it is an array, the keys can only be integers and they should go in order. Instead of using [MPUser] try using a dictionary like [String: MPUser] and this way you will be able to set any key to that dictionary

@kobazzo
Copy link
Author

kobazzo commented Nov 5, 2018

Thank you very mutch for your suggestion. Than i can do a cloud firestore query to have clubs with particular (current) owner by his id?

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