I'm trying to get the current users first name from CloudKit using the following code:
func getUserFirstName() -> String{
var firstNameFromFunction: String?
var currentuserID : CKRecordID?
container.fetchUserRecordID(completionHandler: {
userID, error in
if ((error == nil)) {
self.currentuserID = userID!
}
})
container.discoverUserInfo(withUserRecordID: currentuserID!, completionHandler: {userInfo, error in
let firstNameFromFunction = (userInfo!.displayContact?.givenName)!
})
return firstNameFromFunction!
}
However when executed, the currentUserID is nil, and creating the fatal error. Does anyone know why currentUserID is nil?