Fetch all users from Firebase with Facebook login - swift - ios

My app is using Facebook login only at the moment. I am trying to build a search function using the UISearchBar.
So my question is: Is it in any way possible to fetch all users from the Firebase database into an array? Maybe even get their UserID as well??
If you want to see any piece of code let me know, I just did not think it was relevant to this question :-)

If I have understood what you're trying to achieve then you should simply be able to save your users to the Realtime database upon registering/authenitcating your users.
After this you can perform a simple observeSingleEventOf to get all your users at the correct path, something like this:
rootRef.usersRef.observeSingleEvent(ofType: .value).....
Then with the completion handler you should get back a snapshot, the snapshot.value will include all your users.
*Please note that you don't need to stop observing this method since it will observe the values once then immediately stop. So I don't think you need to worry about memory issues.

Related

Swift 3 Firebase chat application duplicate messages

I was hoping for some advice/help. I have come across a neat little bug in the chat application I'm running that is currently using the anonymous Firebase authentication based around SWIFT3.
Once in the chat room of the application, and I return to the home/login page it appears to almost keep the previous user that was logged in on the current device and act as though you are multiple people. This makes it so that when sending a message, it returns two (or more depending on the amount you return to home/login and continue to the room) chat bubbles.
The one method I've tried was a response on this site previously, I believe it was "try! FIREAuth.auth().signOut()" attached to an #IBAction however I'm not sure if this is designed for anonymous users also?
I'm currently away from my code and can't give any snippets until this evening, but I will certainly answer anything I can until that point.
Thanks
Anonymous accounts function just like any other account, so can be logged out using FIRAuth.auth().signOut().
It's not possible to have more than one FIRAuth.auth().currentUser associated with a single device, so my best guess would be that you have multiple models being initialised for every controller initialisation.

How to manually post data from firebase on my iOS Swift application?

I wonder if it is possible to manually post data from firebase on my application. Let me explain, I want my user to send a post in my Firebase database from my application. But to ensure that its publication is not displayed until after I validated it manually. I do not know if I'm clear feel free to ask me to explain again.
Thank you!
This is a very common requirement that is easy to implement with Firebase.
Set up two top-level queues:
posts
pendingPosts
Your own account can read/write from pendingPosts and posts. But give your regular users only write access to pendingPosts. They can read from posts, but cannot write there.
Now your users can write a pending post. You can then approve it by moving (recreating) it under posts and removing it from pendingPosts.

Pulling Data of Pointer Field of PFUser.currentUser in Parse

I am writing an iOS app with Swift using Parse.
In my User table, I have a pointer field called UserAddress which is pointing to an Address table record. I would like to pull this Address data for current user. I did a bit of research and figured out fetching in background is an option but I would like to avoid it. Is there an alternative to pull this data without running extra fetch work?
What I can imagine is:
Fetching pointer data in AppDelegate before the app gets ready to use for users.
Somehow, force to set includeKey when loading a currentUser
Any of the above is feasible? Please advise me.
You correctly understand the two options, and they are the only two options. For non-user objects, usually includeKey() on a query is the way to go.
For the current user, since you have the user already, it would seem a little strange to query the user, including the pointer, just to avoid fetching the pointer, but that would work. You might be better off working on whatever's making you hesitant to do the fetch().

Parse: Basic App to App Communication

I am trying to figure out what the basic steps are for getting data passed between users. For example, say I, a user of the app, want to send another user of my app a message or a geopoint or some other form of data. I know the first step would be posting the data to Parse, which I don't have a problem with. But then, how would the other user know there is data to retrieve and also how would they go about retrieving it. Are push notifications the proper and only way of letting the recipient's app know its being sent something? When the recipient app knows there is data posted intended for it to retrieve, how does it go about locating it with a PFQuery? Does the posting app need to supply the receiving app with a UID of some form that the receiving app can then use in its query to locate the data? This is kind of the last puzzle piece for my app and unfortunately it's the only thing Parse didn't make clear to me. It is more than likely user error on my part not finding the correct documentation but, app to app communication is key in most apps and so I need to figure out the defacto way that Parse accomplishes this. Thanks in advance for any help!
You can have a relational table lets say "Messages" table in Parse,
with properties, sender (Pointer to a User), recipient (Pointer to a User) and message (String). and maybe a 'read' Boolean.
You could then query the messages table, With something like:
PSEUDO:
get all messages where recipient is equal to logged in user.
and display these messages on the UI.
Its pretty straightforward, I have done simple messaging service with Parse before
Thanks guys! In the end, I think it is best for a device to not have to be querying for changes but rather to be notified when it has new data to retrieve. Thus, for my uses, I think a combination of your answers, especially with the "onSave" hook function mentioned by Bruno, is the best solution.

Parse's CurrentUser doesn't keep refreshed data

Using Parse for iOS, I modified the _User's table to have a field called "Friends" which is an array of pointers to other _Users (by the way, I also have other additional fields).
When I call PFUser.currentUser(), I don't see this friends field in the user object. So I call PFUser.currentUser().fetch() (because refresh method doesn't seem to exist anymore) and I finally have the friends field in my currentUser object.
However, as soon as I exit the app, this field is lost. It doesn't seem to be saved locally unlike all the other fields of the current user.
How am I supposed to force a refresh of the cached current user ?
According to parse.com itself and one of their posts, there is no other way to keep your user up-to-date:
saveEventually does not write through any caches at this time,
including the currentUser, if the app has been restarted. You'll need
to regularly call fetch to keep it up-to-date. We are aware that this
is inconvenient, and are looking into ways to make it work better for
you.
Are you sure that you want your user objects to contain a value that's an array like that? I obviously don't know your use application, but I would recommend using a PFRelation instead. It works just like an array, but the Parse Framework provides a bunch of additional functionality along with it.
As for your specific question it might have to deal with Parse. Maybe they don't automatically retrieve and save arrays that are on an object. That would be my guess because an array could contain who knows how much data in it. I still don't know why they wouldn't locally save that data after it's fetched.
My suggestion is probably a little over the top, but would most likely be the best outcome. Utilize a local database such as CoreData or even the ParseLocalDatastore. Then encapsulate the Parse framework to have your own User object where you can store the information, which can also maintain persistence via your database.

Resources