I’m facing some difficulty in the code as shown below:
let group = DispatchGroup()
group.enter()
DispatchQueue.global(qos: .default).async {
Utlities().setCurrUser(currUser: result!.user)
group.leave()
}
group.enter()
DispatchQueue.main.async {
strongSelf.spinner.dismiss()
group.leave()
}
group.wait()
var f = " ", l = " "
(f,l) = strongSelf.getName()
print(f,"---",l)
User.Instance().setfName(name: f)
User.Instance().setlName(name: l)
I want the code to execute in order but what is happening that the last 5 lines are being executed before the first DispatchQueue code i.e this code
Utlities().setCurrUser(currUser: result!.user)
This setCurrUser function is simply getting names of user from firebase on the basis of user id and and storing it in user defaults ,and my getName is just returning names after getting it from user defaults .The code mentioned above is being called in my login functionality part , so what happens is that when I first login, the attributes of user object ‘firstname’ and ‘lastname’ get empty but when I run my app again , the login session is stored so no need to login again , so this time i just get the firstname and lastname from user defaults , this time the attributes of user object ‘firstname’ and ‘lastname’ get filled . So I am concluding that this is the issue that the last 5 lines of code gets executed first before this code completion:
Utlities().setCurrUser(currUser: result!.user)
so thats why first time we get empty names and second we get successful data.
Useful codes:
public func setCurrUser(currUser: FirebaseAuth.User){
let uid = currUser.uid
let email = currUser.email
let filename = uid + ".png"
// get firstname and lastname from database
DatabaseManager().getUserName(uid: uid) { [weak self] result in
switch result {
case .success(let data):
guard let userData = data as? [String:Any],
let firstname = userData["firstname"] as? String,
let lastname = userData["lastname"] as? String
else {return}
UserDefaults.standard.set(firstname, forKey: "fName")
UserDefaults.standard.set(lastname, forKey: "lName")
case .failure(let err):
print("Utilities: func: setCurrUser: Line 55: ",err)
}
}
let usr = User.Instance(firstName:" " ,lastName:" ", email: email!, uid: uid)
usr.setUrl(path:"images/\(filename)")
}
Below is the same function (with same name) , it gets called when the user is already logged in:
public func setCurrUser(currUser: FirebaseAuth.User){
let uid = currUser.uid
let email = currUser.email
let filename = uid + ".png"
var f = " ", l = " "
(f,l) = self.getName()
let usr = User.Instance(firstName:f ,lastName:l, email: email!, uid: uid)
usr.setUrl(path:"images/\(filename)")
}