alickbass / CodableFirebase

Use Codable with Firebase
MIT License
691 stars 91 forks source link

Encode object and save with custom key #59

Open kobazzo opened 5 years ago

kobazzo commented 5 years ago

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 commented 5 years ago

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

kobazzo commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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?