I am trying to check for duplicate emails and usernames in the Users table. Here is the code that I am using to do it:
func checkForDuplicateInformation(_ email: String, username: String)throws {
var bc: BackendlessCollection?
var whereClause = "email = \(email)"
let dataQuery = BackendlessDataQuery()
dataQuery.whereClause = whereClause
var error: Fault?
Types.tryblock({ () -> Void in
bc = Backendless.sharedInstance().data.of(BackendlessUser.ofClass()).find(dataQuery, fault: &error)
}, catchblock: { (exception) -> Void in
print(exception)
})
if let count = bc?.data.count {
if error != nil && count > 0 {
throw SignUpError.duplicateEmail
}
else {
print("Server reported an error: \(error)")
}
} else {
print("Server reported an error: \(error)")
}
whereClause = "username = \(username)"
dataQuery.whereClause = whereClause
Types.tryblock({ () -> Void in
bc = Backendless.sharedInstance().data.of(BackendlessUser.ofClass()).find(dataQuery, fault: &error)
}, catchblock: { (exception) -> Void in
print(exception)
})
if let count = bc?.data.count {
if error == nil && count > 0 {
throw SignUpError.duplicateUsername
}
else {
print("Server reported an error: \(error)")
}
} else {
print("Server reported an error: \(error)")
}
}
For some reason instead of the error being printed to the console, the app crashes on:
-[Invoker invokeSync:method:args:]:
Just after:
-[PersistenceService find:dataQuery:error:]:
What is causing this? Do you want my App ID and Secret Key?
Hi Caled,
We are investigating your issue. Thanks for your patience.
Regards, Ilya
One thing I forgot to mention. If I place a breakpoint at the line it crashes on (12), and a go into the call, then travel through it for some time, then continue running the app, server error 1017 (Invalid where clause) gets logged and the app does not crash.
Please, provide your appId, we will investigate this issue with your data.
Here it is: 3E85C016-1C1F-C9FE-FF7D-56FC5CE34A00
Hi Caleb,
I’ve fixed your function, now it works:
func checkForDuplicateInformation(email: String, username: String) throws {
var bc: BackendlessCollection?
var error: Fault?
let dataQuery = BackendlessDataQuery()
dataQuery.whereClause = "email = '\(email)'"
bc = Backendless.sharedInstance().data.of(BackendlessUser.ofClass()).find(dataQuery, fault: &error)
if error == nil {
let users = bc?.getCurrentPage() as! [BackendlessUser]
if users.count > 0 {
print("User: \(email) is registered")
throw SignUpError.duplicateEmail
}
}
else{
print("Server reported an error (1): \(error)")
}
dataQuery.whereClause = "userName = '\(username)'"
bc = Backendless.sharedInstance().data.of(BackendlessUser.ofClass()).find(dataQuery, fault: &error)
if error == nil {
let users = bc?.getCurrentPage() as! [BackendlessUser]
if users.count > 0 {
print("User: \(username) is registered")
throw SignUpError.duplicateUsername
}
}
else{
print("Server reported an error (2): \(error)")
}
}
You could check an email and/or username using only one call, for example:
func isUserExist(email: String, username: String) throws {
let query = BackendlessDataQuery()
query.whereClause = "email = '\(email)' OR userName = '\(username)'"
var error: Fault?
let bc = backendless!.persistenceService.of(BackendlessUser.self).find(query, fault: &error)
if error == nil {
let users = bc?.getCurrentPage() as! [BackendlessUser]
if users.count > 0 {
for user in users {
if user.email.isEqual(email) {
print("User: \(user.email) is registered")
throw SignUpError.duplicateEmail
}
let name = user.getProperty("userName") as! String
if name.isEqual(username) {
print("User: \(name) is registered")
throw SignUpError.duplicateUsername
}
}
}
else {
print("User \(email) [\(username)] is not registered")
}
}
else{
print("Server reported an error: \(error)")
}
}
Regards,
Slava