I am not positive if I am structuring my app correctly. I am using structs for the first time and I am trying to save locations from users and create users using Firebase. I want to display all users only by location. The Global.refString gets set from a UIPicker which is hard coded with 6 different locations. loadUserInfo() is not returning any information. It worked the first time I tried it but after reopening and closing the app it always returns empty. Im not sure if a struct will get saved each time I open the app. Should I use a different method to accomplish these tasks.
private func saveUserInfo(firstLastName: String, user: User!, location: String, biography: String, password: String, phoneNumber: String){
let locationRef = Global.refString
let userInfo = ["firstLastName": firstLastName, "email": user.email!, "password": password, "location": location, "phoneNumber": phoneNumber, "biography": biography, "uid": user.uid, "photoURL": String(describing: user.photoURL!)] as [String : Any]
let userRef = dataBaseRef.child(locationRef!).child(user.uid)
userRef.setValue(userInfo) { (error, ref) in
if error == nil{
print("USER SAVED")
self.logIn(email: user.email!, password: password)
}else{
print(error?.localizedDescription)
}
}
}
func loadUserInfo(){
let locationRef = Global.refString
let userRef = dataBaseRef.child(locationRef!).child(Auth.auth().currentUser!.uid)
userRef.observe(.value, with: { (snapshot) in
let user = Users(snapshot: snapshot)
if let username = user.name{
self.nameLabel.text = username
}
if let number = user.phoneNumber{
self.phone = Int(number)
}
if let userLocation = user.location{
self.bioLabel.text = userLocation
}
self.storageRef.storage.reference(forURL: imageOld).getData(maxSize: 10 * 1024 * 1024, completion: { (imgData, error) in
if error == nil {
DispatchQueue.main.async {
if let data = imgData {
self.avatar.image = UIImage(data: data)
}
}
}else {
print(error!.localizedDescription)
}
}
)}
}) { (error) in
print(error.localizedDescription)
}
}
}
struct Global
{
static var Location : String!
static var usersListSent
= [String]()
static var refString: String!
}
Try:
Database.database().reference().child(Global.refString).child(id).observeSingleEvent(of: .value) { (snapshot) in
if let snapshots = snapshot.children.allObjects as? [DataSnapshot] {
for snap in snapshots {
print(snap)
}
}
Related
In my page named service, xcode points to the user and gives an error. but it doesn't work. What do you think should I change?
my user is already optional. I think it is an index problem but I don't know how to solve it I would appreciate it if you could help.where do you think the problem
message.swift
import Firebase
struct Message {
let text: String
let toId: String
let fromId: String
var timestamp: Timestamp!
var user: User?
let isFromCurrentUser :Bool
init(dictionary: [String: Any]) {
self.text = dictionary["text"] as? String ?? ""
self.toId = dictionary["toId"] as? String ?? ""
self.fromId = dictionary["fromId"] as? String ?? ""
self.timestamp = dictionary["timestamp"] as? Timestamp ?? Timestamp(date: Date())
self.isFromCurrentUser = fromId == Auth.auth().currentUser?.uid
}
}
struct Conversation {
let user: User
let message : Message
}
Service.Swift
import Firebase
struct Service {
static func fetchUsers (completion: #escaping([User]) -> Void) {
var users = [User] ()
COLLECTION_USERS.getDocuments { (snapshot, error) in
snapshot?.documents.forEach({ (document) in
let dictionary = document.data()
let user = User(dictionary: dictionary)
users.append(user)
completion(users)
})
}
}
static func fetchUser(widhtUid uid: String, completion:#escaping([User]) ->Void) {
COLLECTION_USERS.document(uid).getDocument { (snapshot, error) in
guard let dictionary = snapshot?.data() else {return}
let user = User(dictionary: dictionary)
completion(user)
}
}
static func fetchConversations (completion: #escaping([Conversation]) ->Void) {
var conversations = [Conversation]()
guard let uid = Auth.auth().currentUser?.uid else {return}
let query = COLLECTION_MESSAGES.document(uid).collection("recent-messages").order(by: "timestamp")
query.addSnapshotListener { (snapshot, error) in
snapshot?.documentChanges.forEach({ change in
let dictionary = change.document.data()
let message = Message(dictionary: dictionary)
self.fetchUser(widhtUid: message.toId) { user in
let conversation = Conversation(user:user, message: message)
conversations.append(conversation)
completion(conversations)
}
})
}
}
static func fetchMessages (forUser user: User, completion: #escaping([Message])-> Void) {
var messages = [Message]()
guard let currentUid = Auth.auth().currentUser?.uid else {return}
let query = COLLECTION_MESSAGES.document(currentUid).collection(user.uid).order(by: "timestamp")
query.addSnapshotListener{(snapshot,error) in
snapshot?.documentChanges.forEach({ change in
if change.type == .added {
let dictionary = change.document.data ()
messages.append(Message(dictionary: dictionary))
completion(messages)
}
})
}
}
static func uploadMessage(message: String, to user: User, completion: ((Error?)->Void)?) {
guard let currentUid = Auth.auth().currentUser?.uid else {return}
let data = ["text": message,
"fromId": currentUid,
"toId": user.uid,
"timestamp" : Timestamp(date: Date())] as [String : Any]
COLLECTION_MESSAGES.document(currentUid).collection(user.uid).addDocument(data:data) { _ in
COLLECTION_MESSAGES.document(user.uid).collection(currentUid).addDocument(data:data,completion:completion)
COLLECTION_MESSAGES.document(currentUid).collection("recent- messages").document(user.uid).setData(data)
COLLECTION_MESSAGES.document(user.uid).collection("recent- messages").document(currentUid).setData(data)
}
}
}
In this method:
static func fetchUser(widhtUid uid: String, completion:#escaping ([User]) -> Void)
The completion closure's parameter should be a User, not an array of users - [User].
Xcode should point you to the line where this error happens...
Anyway, here
static func fetchUser(widhtUid uid: String, completion:#escaping([User]) ->Void) {
COLLECTION_USERS.document(uid).getDocument { (snapshot, error) in
guard let dictionary = snapshot?.data() else {return}
let user = User(dictionary: dictionary)
completion(user)
}
}
Your completion:#escaping([User]) ->Void) expects an array [User] , but you invoke it with just one User object here completion(user)
I have a bit of a lengthy question, So I apologize in advance I will try to illustrate this to the best of my abilities. I am trying to establish a notifications view controller that calls different types of data from Firebase and sets different notification types.
In the image above, this is how the cells should look when a user sends a notification to firebase. The user associated with that specific notification type as called and posted onto the screen.
In the firebase structure, We see that all of the information Stored is saved under the UID of the user in the first picture and is set under that specific users notification to show who is sending them a notification which is correct. These users names and images show perfectly as well as the image on the right.
The code I use to save this information is below,
fileprivate func saveSwipeToDataBase(didLike: Any) {
let swipeDate = Int(NSDate().timeIntervalSince1970)
guard let uid = Auth.auth().currentUser?.uid else { return }
guard let cardUID = topCardView?.cardViewModel.uid else { return }
let documentData = ["workerId": uid,
"didLike": didLike,
"checked": 0,
"Swipe Date": swipeDate,
"type": SWIPE_INT_VALUE,
"posterId" : cardUID] as [String : Any]
self.postJobNotificationsIntoDatabseWithUID(uid: cardUID, values: documentData as [String : AnyObject])
}
private func postJobNotificationsIntoDatabseWithUID(uid: String, values: [String: AnyObject]) {
let ref = Database.database().reference(fromURL: "https://oddjobs-b131f.firebaseio.com/")
let usersReference = ref.child("notifications").child(uid).childByAutoId()
usersReference.setValue(values, withCompletionBlock: { (err, ref) in
if err != nil {
print("error saving data into firebase")
return
}
})
}
And below is how I retrieve this information and store it onto the Notifications View controller.
func fetchNotifications() {
guard let currentUID = Auth.auth().currentUser?.uid else { return }
NOTIFICATIONS_REF.child(currentUID).observeSingleEvent(of: .value) { (snapshot) in
guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
print(dictionary)
for (_, postingRawData) in dictionary {
guard let postingDictionary = postingRawData as? Dictionary<String, AnyObject> else { continue }
guard let uid = postingDictionary["workerId"] as? String else { continue }
Database.fetchUser(with: uid, completion: { (user) in
if let postId = postingDictionary["posterId"] as? String {
Database.fetchPoster(with: postId, completion: {(poster) in
let notification = userNotifications(user: user, poster: poster, dictionary: postingDictionary)
self.notifications.append(notification)
self.handleSortNotification()
})
} else {
let notification = userNotifications(user: user, dictionary: postingDictionary)
self.notifications.append(notification)
self.handleSortNotification()
}
})
}
}
}
Now that I got the correct way to setup up and show out of the way, I will show my enum and how I am distinguishing the different types of calls from firebase.
class userNotifications {
// MARK: - establish notificationTypes
enum NotificationType: Int, Printable {
case swipe
case accepted
case confirmed
case completed
case pay
var description: String {
switch self {
case .swipe: return " swiped on your Job "
case .accepted: return " accepted you to complete the job, "
case .confirmed: return " confirmed the job"
case .completed: return " completed the job"
case .pay: return " pay for completed"
}
}
init(index: Int) {
switch index {
case 0: self = .swipe
case 1: self = .accepted
case 2: self = .confirmed
case 3: self = .completed
case 4: self = .pay
default: self = .swipe
}
}
}
// MARK: - access firebaseData
var creationDate: Date!
var timeDate: Date!
var uid: String!
var fromId: String?
var workerId: String?
var user: User!
var poster: Poster!
var type: Int?
var notificationType: NotificationType!
var didCheck = false
init(user: User? = nil, poster: Poster? = nil, dictionary: Dictionary<String, AnyObject>) {
self.user = user
if let poster = poster {
self.poster = poster
}
if let swipeDate = dictionary["Swipe Date"] as? Double {
self.creationDate = Date(timeIntervalSince1970: swipeDate)
}
if let createDate = dictionary["creationDate"] as? Double {
self.creationDate = Date(timeIntervalSince1970: createDate)
}
if let swipeDate = dictionary["time&date"] as? Double {
self.timeDate = Date(timeIntervalSince1970: swipeDate)
}
if let type = dictionary["type"] as? Int {
self.notificationType = NotificationType(index: type)
}
if let uid = dictionary["uid"] as? String {
self.uid = uid
}
if let fromId = dictionary["fromId"] as? String {
self.fromId = fromId
}
if let workerId = dictionary["workerUID"] as? String {
self.workerId = workerId
}
if let checked = dictionary["checked"] as? Int {
if checked == 0 {
self.didCheck = false
} else {
self.didCheck = true
}
}
}
}
Above is the different types of notifications to be set.
Now, My issue is If I call a different notification type, such as .accepted, the information calls in a very different way.
The image above seems correct, However, the name and image are incorrect. it should be from the user ZacheryWilcox instead of Cjbwjdhbe. the user Cjbwjdhbe is the current user and the user who should be receing a notification from Zacherywilcox. not from itself.
In firebase, the information is saved as
the code I use to save this information is below
var workerUser: User? {
didSet {
let name = workerUser?.name
workerNameLabel.text = name
let workersUID = workerUser?.uid
workerNameLabel.text = name
guard let profileImage = workerUser?.profileImageUrl else { return }
workerImageView.loadImageUsingCacheWithUrlString(profileImage)
}
}
func saveUserData() {
let workUser = self.workerUser
guard let uid = Auth.auth().currentUser?.uid else { return }
let workerId = workUser?.uid
Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
guard let dictionary = snapshot.value as? [String : Any] else { return }
let user = User(dictionary: dictionary as [String : AnyObject])
workUser?.uid = snapshot.key
self.datePicker.datePickerMode = UIDatePicker.Mode.date
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd yyyy/ hh:mm a"
let selectedDate = dateFormatter.string(from: self.datePicker.date)
let creationDate = Int(NSDate().timeIntervalSince1970)
print(selectedDate)
let docData: [String: Any] = [
"workerId": workerId!,
"time&date": selectedDate,
"posterId" : uid,
"creationDate": creationDate,
"location": user.address!,
"type": 1,
"jobPost": "someUIDString",
"checked": 0,
]
self.postJobNotificationsIntoDatabseWithUID(uid: workerId!, values: docData as [String : AnyObject])
}, withCancel: { (err) in
print("attempting to load information")
})
print("Finished saving user info")
self.dismiss(animated: true, completion: {
print("Dismissal complete")
})
}
private func postJobNotificationsIntoDatabseWithUID(uid: String, values: [String: AnyObject]) {
let ref = Database.database().reference(fromURL: "https://oddjobs-b131f.firebaseio.com/")
let usersReference = ref.child("notifications").child(uid).childByAutoId()
usersReference.setValue(values, withCompletionBlock: { (err, ref) in
if err != nil {
print("error saving data into firebase")
return
}
})
}
When the type .accepted is being used to differentiate what notificationType is being called, the user who sent the notification is not being set correctly and I have no idea what is the reasoning behind this. The correct user that is sending this information over is Zacherywilcox, and that users image and name should be set to the user's notification screen. not the user Cjbe... I was wondering if anyone could help me fix these issues. Thank you in advance. I'm starting to think that the way I am saving the users information when accepting the user is incorrect.
When I am fetchingNotifications(), is it possible that since calling
guard let uid = postingDictionary["workerId"] as? String else { continue }
Database.fetchUser(with: uid, completion: { (user) in
if let postId = postingDictionary["posterId"] as? String {
has an effect on whats going on? if so, Is there a way to differentiate between what notificationType is being called and fetch what notifications has been called with their respective users?
Just update your code to:
func fetchNotifications() {
guard let currentUID = Auth.auth().currentUser?.uid else { return }
NOTIFICATIONS_REF.child(currentUID).observeSingleEvent(of: .value) { (snapshot) in
guard let dictionary = snapshot.value as? Dictionary<String, AnyObject> else { return }
print(dictionary)
let notificationId = snapshot.key
for (_, postingRawData) in dictionary {
guard let postingDictionary = postingRawData as? Dictionary<String, AnyObject> else { continue }
guard let type = postingDictionary["type"] as? Int else { continue }
guard let uid = (type == userNotifications.NotificationType.accepted.rawValue) ? postingDictionary["fromId"] as? String : postingDictionary["workerId"] as? String else { continue }
Database.fetchUser(with: uid, completion: { (user) in
if let postId = postingDictionary["fromId"] as? String {
Database.fetchPoster(with: postId, completion: {(poster) in
let notification = userNotifications(user: user, poster: poster, dictionary: postingDictionary)
self.notifications.append(notification)
self.handleSortNotification()
})
} else {
let notification = userNotifications(user: user, dictionary: postingDictionary)
self.notifications.append(notification)
self.handleSortNotification()
}
// NOTIFICATIONS_REF.child(currentUID).child(notificationId).child("checked").setValue(1)
})
}
}
}
This will solve your problem.
I am trying to allow users to delete comments upon pressing the delete button. When comments are submitted, they're created using autoId and the header of the node will be the postId to see what post they commented on.
"comments" : {
"-LmfZZis5ovtBwfm_4xR" : {
"-LoHu5Qv3BmuHTsSlthj" : {
"creationDate" : 1.567980283717026E9,
"text" : "Kkkk",
"uid" : "64r3dgTN6xMhHYhptFlsFWX0dLk2"
},
"-LoHuPohuQ3eUtDWL_G-" : {
"creationDate" : 1.567980367209054E9,
"text" : " Ok",
"uid" : "64r3dgTN6xMhHYhptFlsFWX0dLk2"
}
}
},
I do not know how to retrieve the autoId so current logged in users can delete their comments. Here is the code for submission
func didSubmit(for comment: String) {
guard let uid = Auth.auth().currentUser?.uid else { return }
print("post id:", self.post?.postId ?? "")
print("Inserting comment:", comment)
let postId = self.post?.postId ?? ""
let values = ["text": comment, "creationDate": Date().timeIntervalSince1970, "uid": uid] as [String : Any]
Database.database().reference().child("comments").child(postId).childByAutoId().updateChildValues(values) { (err, ref) in
if let err = err {
print("Failed to insert comment:", err)
return
}
self.uploadCommentNotificationToServer()
if comment.contains("#") {
self.uploadMentionNotification(forPostId: postId, withText: comment, isForComment: true)
}
self.containerView.clearCommentTextView()
}
}
Comment struct
struct Comment {
var commentId: String!
let user: User
var creationDate: Date!
let text: String
let uid: String!
init(commentId: String!,user: User, dictionary: [String: Any]) {
self.commentId = commentId
self.user = user
self.text = dictionary["text"] as? String ?? ""
self.uid = dictionary["uid"] as? String ?? ""
if let creationDate = dictionary["creationDate"] as? Double {
self.creationDate = Date(timeIntervalSince1970: creationDate)
}
}
var post: Post?
func deleteComment() {
guard let postId = self.post?.postId else { return }
let commentsRef = Database.database().reference().child("comments")
commentsRef.child(postId).child(commentId).removeValue()
}
}
Code to fetch the comments
var comments = [Comment]()
func fetchComments() {
guard let postId = self.post?.postId else { return }
let ref = Database.database().reference().child("comments").child(postId)
ref.observe(.childAdded, with: { (snapshot) in
let commentId = snapshot.key
guard let dictionary = snapshot.value as? [String: Any] else { return }
guard let uid = dictionary["uid"] as? String else { return }
Database.fetchUserWithUID(with: uid, completion: { (user) in
let comment = Comment(commentId: commentId, user: user, dictionary: dictionary)
self.comments.append(comment)
self.collectionView?.reloadData()
})
}) { (err) in
print("Failed to observe comments")
}
}
Thanks!
To be able to delete a node, you must know the full path to that node.
There are two ways to know the key of the product you want to delete:
You've passed it along your app from the moment when you loaded the data.
You have some other value that allows you to perform a query on the database to look up the key.
The first option is the most common, as you'll typically be loading the data from the database already to display it to the user. In that case you should "simply" pass the key of the data along when displaying the value.
Once you have the key of the product/child node, you can delete it with:
let postId = "-LmfZZis5ovtBwfm_4xR"
let commentId = "-LoHu5Qv3BmuHTsSlthj"
let commentsRef = Database.database().reference().child("comments")
commentsRef.child(postId).child(commentId).removeValue()
I am making a tinder type app, at the moment I have cardViewModels: [CardViewModel] which contains 4 fictitious app users in the producers array of user objects.
this currently works fine and loads the user on to the tinder type cards.
though now I would like to some how replace the fictitious users with other fictitious users from Firebase/Firestore.
I would expect to call fetchUsersCurrentUserIsFollowing() which returns
UserGrapevine users (as seen below)
UserGrapevine(name: displayName, age: age, profession: occupation, imageNames: ["lady5c"], kmsAway: distanceInKms)
I then wish to append these users to the producers array and then call setupDummyCards() after my producers array has been set with the users from Firestore?
let cardViewModels: [CardViewModel] = {
let producers = [
UserTinder(name: "Kelly", age: 23, profession: "Music DJ", imageNames: ["lady5c","lady4c","kelly1","kelly2","kelly3"]),
UserTinder(name: "Jane", age: 18, profession: "Teacher", imageNames: ["jane1","jane2","jane3"]),
Advertiser(title: "Buy My Shirt", brandName: "Ben's Shirts", posterPhotoName: "lady5c"), Joker(title: "JOKER", cardSuit: "HEART", jokerPhotoName: "lady5c")] as [ProducesCardViewModel]
let viewModels = producers.map({return $0.toCardViewModel()})
return viewModels
}()
fileprivate func setupDummyCards() {
cardViewModels.forEach { (cardVM) in
let cardView = CardView(frame: .zero)
cardView.cardViewModel = cardVM
cardsDeckView.addSubview(cardView)
cardView.fillSuperview()
}
}
func fetchUsersCurrentUserIsFollowing(){
guard let uid = Auth.auth().currentUser?.uid else { return }
let db = Firestore.firestore()
db.collection("Users").document(uid).collection("Following").getDocuments() { (querySnapshot, err) in
if let err = err {
print("Error getting documents: \(err)")
} else {
for document in querySnapshot!.documents {
//print("\(document.documentID) => \(document.data())")
let d = document.data()
d.forEach({ (key: String, value: Any) in
print("this is the key",key)
Database.firestorefetchUserForGrapevineCardWithUID(uid: key, completion: { (user) in
//append to producers list somehow.
}
)}
)}
}
}
}
extension Database {
static func firestorefetchUserForGrapevineCardWithUID(uid: String, completion:#escaping (UserGrapevine) -> ()) {
print("fire store fetch user with uid called")
let defaults = UserDefaults.standard
let db = Firestore.firestore()
let docRef = db.collection("Users").document(uid)
docRef.getDocument { (document, error) in
if (document?.exists)! {
if let document = document {
guard let dictionary = document.data() else {
print("error 1245")
return }
guard let age = dictionary["Age"] as? Int else { return }
guard let occupation = dictionary["Occupation"] as? String else {
return }
guard let displayName = dictionary["Display Name"] as? String else {
return }
guard let searchingFromLatitude = defaults.string(forKey: "SearchingFromLatitude")
else {
print("error 1234324245")
return }
guard let searchingFromLongitude = defaults.string(forKey: "SearchingFromLongitude") else {
print("error 1832835")
return }
guard let DoubleLatitude = Double(searchingFromLatitude) else {
print("error t345345345")
return }
guard let DoubleLongitude = Double(searchingFromLongitude) else {
print("error 55545345")
return }
//////////////Start of distanceFrom code///////////////////
guard let latitude = dictionary["Actual Latitude"] as? String, let longitude = dictionary["Actual Longitude"] as? String, let latDouble = Double(latitude), let longDouble = Double(longitude) else {
return }
let distanceInKms = Database.getDistanceInKms(currentUserLat: DoubleLatitude, currentUserLong: DoubleLongitude, lat: latDouble, long: longDouble)
///////////////End of distance code/////////////////
let user = UserGrapevine(name: displayName, age: age, profession: occupation, imageNames: ["lady5c"], kmsAway: distanceInKms)
completion(user)
} else {
print("Document does not exist")
}
}
}
}
}
After I update Firebase pod I got this Error :
Cannot invoke initializer for type 'User' with an argument list of type '(snapshot: (DataSnapshot))'
And here is my code enter image description here
Any Idea to solve this ..???
func loadUserInfo(){
let userRef = dataBAseRef.child("users/\(Auth.auth().currentUser!.uid)")
userRef.observe(.value, with: { (snapshot) in
let user = User(snapshot: snapshot)
self.usernameLabel.text = user.username
self.userCountry.text = user.country!
self.userBiographyTextView.text = user.biography!
let imageURL = user.photoURL!
self.storageRef.reference(forURL: imageURL).data(withMaxSize: 1 * 1024 * 1024, completion: { (imageData, error) in
if error == nil {
DispatchQueue.main.async {
if let data = imageData {
self.userImageView.image = UIImage(data: data)
}
}
} else {
print(error!.localizedDescription)
}
})
}) { (error) in
print(error.localizedDescription)
}
}
After I update everything to firebase 4.0.4 I got the error, here is the whole codes :
import Foundation
import Firebase
import FirebaseStorage
import FirebaseDatabase
import FirebaseAuth
struct AuthService {
var dataBAseRef: DatabaseReference! {
return Database.database().reference()
}
var storageRef: StorageReference! {
return Storage.storage().reference()
}
// 1- creating the signup function
func signUp(username:String,email:String,country:String,password:String,biography: String, pictureData:NSData!) {
Auth.auth().createUser(withEmail: email, password: password, completion: {(user, error) in
if error == nil {
self.setUserInfo(user: user, username: username, country: country, password: password, biography: biography, pictureData: pictureData)
} else {
print(error?.localizedDescription as Any)
}
})
}
// 2- create the set user info function
private func setUserInfo(user: User!, username: String, country: String, password: String, biography: String, pictureData: NSData!)
{
let imagePath = "profileImage\(user.uid)/userPic.jpg"
let imageRef = storageRef.child(imagePath)
let metaData = StorageMetadata()
metaData.contentType = "image/jpeg"
imageRef.putData(pictureData as Data, metadata: metaData) {(newMetaData, error) in
if error == nil {
let changeRequest = user.profileChangeRequest()
changeRequest.displayName = username
if let photoURL = newMetaData!.downloadURL() {
changeRequest.photoURL = photoURL
}
changeRequest.commitChanges(completion: { (eroor) in
if error == nil {
self.saveUserInfo(user: user, username: username, country: country, password: password, biography: biography )
} else {
print(error?.localizedDescription as Any)
}
})
} else {
print(error?.localizedDescription as Any)
}
}
}
// 3- save the User info in Firebase
private func saveUserInfo(user: User!, username: String, country: String, password: String, biography: String)
{
let userInfo = ["Email": user.email!, "username": username, "country": country,"biography": biography, "uid": user.uid, "photoURL": String(describing: user.photoURL!)]
let userRef = dataBAseRef.child("users").child(user.uid)
userRef.setValue(userInfo) { (error, ref) in
if error == nil {
print("user info saved successfully")
self.logIn(email: user.email!, password: password)
} else {
print(error?.localizedDescription as Any)
}
}
}
// logging the user in function
func logIn(email: String, password: String) {
Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
if let user = user {
print("\(user.displayName!) has logged in successfully")
let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDel.logUser()
}
}
else {
print(error?.localizedDescription as Any)
}
})
}
}
Now I have 2 errors :
here is the errors
I tried to update profileChangeRequest() to createProfileChangeRequest() but didn't help
Here is User class:
import Foundation
import Firebase
import FirebaseDatabase
struct User {
var username: String!
var email: String?
var country: String?
var biography: String?
var photoURL: String!
var uid: String!
var ref: DatabaseReference?
var key: String?
init(snapshot: DataSnapshot) {
key = snapshot.key
ref = snapshot.ref
username = (snapshot.value! as! NSDictionary)["username"] as! String
email = (snapshot.value! as! NSDictionary)["email"] as? String
country = (snapshot.value! as! NSDictionary)["country"] as? String
uid = (snapshot.value! as! NSDictionary)["uid"] as! String
biography = (snapshot.value! as! NSDictionary)["biography"] as? String
photoURL = (snapshot.value! as! NSDictionary)["photoURL"] as! String
}
}
I think your user class is clashing with with the FIRUser class which they changed from FIRUser to User in the 4.x SDK. Try renaming your user class to something like LocalUser and see if that helps, if it does then there's your problem