So I have this function, where I am comparing the password user entered(passed in variable 'password') with that available in the database( fetched in variable 'Pwd'). If the condition matches then I want the function to return value 1 else 0.
But I am facing issue with scope of variable. The count has the value 1 inside recordFetchedBlock only. and not outside.Can you suggest how I can make this function return 1 on password matching
func FetchRecords(user_name: String, password: String) -> Int {
let pubData = CKContainer.default().publicCloudDatabase
let predicate = NSPredicate(format: "Username = %@", user_name)
let query = CKQuery(recordType: "Teachers", predicate: predicate)
let operation = CKQueryOperation(query: query)
var Pwd = String()
var count = Int()
operation.recordFetchedBlock = { (record : CKRecord!) in
let Pwd: String = record["Password"] as! String
if Pwd == password{
count=1
}else{
count=0
}
}
operation.queryCompletionBlock = { cursor, error in
}
CKContainer.default().publicCloudDatabase.add(operation)
return count
}