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

Query relevant data from Firebase realtimedatabase, link the data together using autoIDs and structure data for use. SWIFT 4 #648

Closed
domcini opened this issue Mar 20, 2019 · 1 comment
Labels

Comments

@domcini
Copy link

domcini commented Mar 20, 2019

I'm a relatively new developer to the Firebase/Swift communities and am facing a challenge slightly out of my current capacity. I’m looking to create a chat app similar to slack and my database looks as follows...

USERS
• **User ID**
  *Meta*
    Name - “”
    Email - “”

  *Groups*
    groupID - true
    groupID - true


GROUPS
• **Group ID**
  *Meta*
    Name - “”
    ImageURL - “”

  *Channels*
    channellD - true
    channellD - true


CHANNELS
• **Channel ID**
  *Meta*
    Title - “”

  *Messages*
    messageID - true
    messageID - true


MESSAGES
• **Message ID**
  *Meta*
    Message Body - “”
    userID - true
    channelID - true

The current issue I’m facing is how to query the relevant data and how to link the data together.

I would like to display a collection view, populated with group images. Once a group is selected, display the relevant channels for this group in a table view below, then the relevant messages for the channel selected inside a new VC.

  1. Would I have to create an array of groupIDs for each user, then loop through those IDs to get groups data (and so on)?

  2. Would i have to query the current userID inside the groups node and add all relevant groups data to a 'Groups' array (and so on - or would this too long with large sets of data?)?

  3. Or is there a more efficient way to do this?

I’m also finding it hard to understand how the data would be structured inside my code once I have retrieved it from the database (unless i were to use number 2), so i can display it in the relevant views. I could not imagine a standard Dictionary would be suitable for this?

E.g. How would I know which channels belonged to which groups, and which messages to which channels? This may be a simple solution, but right now i cant seem to find an answer.

With the following question in mind
https://stackoverflow.com/questions/38537028/firebase-ios-swift-retrieve-child-data-corresponding-to-a-chat-conversation-id?rq=1

I'd like to say thanks in advance and hope you guys can help!

@domcini domcini changed the title Query relevant data from realtimedatabase and link the data together using autoIDs Query relevant data from Firebase realtimedatabase, link the data together using autoIDs and structure data for use. SWIFT 4 Mar 20, 2019
@morganchen12
Copy link
Contributor

You'd have to do a lot of querying, but your queries themselves won't be downloading large amounts of data until you start loading messages. Here's what I'd recommend:

  1. From user login, use the user's uid as the unique identifier for their user node in RTDB. This way you can fetch the user node directly without querying a collection of users.
  2. Enumerate the user's groups array and fetch all of those groups from the groups root.
  3. Similarly, enumerate the channels for a selected group, and then load all the messages belonging to that channel in chronological order.

To answer your questions directly:

Would I have to create an array of groupIDs for each user, then loop through those IDs to get groups data (and so on)?

Yes. FUIIndexArray is a sample implementation of what you're describing, though it's in objc.

Would i have to query the current userID inside the groups node and add all relevant groups data to a 'Groups' array (and so on - or would this too long with large sets of data?)?

Yes, though if you're concerned with data size you can always just load the first N and then allow the user to load more later.

Or is there a more efficient way to do this?

There's some optimizations you can make around how frequently groups/channels will be created and destroyed as well as how many groups a user will likely belong to at one time, but otherwise there isn't really a more efficient way to do this.

I’m also finding it hard to understand how the data would be structured inside my code once I have retrieved it from the database (unless i were to use number 2), so i can display it in the relevant views. I could not imagine a standard Dictionary would be suitable for this?

A standard dictionary is not suitable since you have to load each corresponding key's value asynchronously.

How would I know which channels belonged to which groups, and which messages to which channels?

The easiest way would be to add a groupID field to your channel object and a channelID field to your message objects, since each message can only belong to one channel and each channel can only belong to one group.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants