I have a for loop - running 6 times - containing an async function that uploads an object to a database. When called the function only uploads 3 - 4 objects and for the remaining 2 - 3 the console prints:
fServer reported an error: FAULT = 'Server.Processing' [java.lang.RuntimeException: java.lang.RuntimeException: java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction]
I've read other threads regarding this issue and I followed the advise to use dispatch groups. Unfortunately it's still not working for me and I do not understand why. How can I avoid these deadlocks from happening?
func uploadObjects() {
let myGroup = dispatch_group_create()
self.backendless.initApp(self.APP_ID, secret: self.SECRET_KEY, version: self.VERSION_NUM)
for object in objects {
dispatch_group_enter(myGroup)
let dataStore = backendless.data.of(Game.ofClass())
// save object asynchronously
dataStore.save(
game,
response: { (result: AnyObject!) -> Void in
dispatch_group_leave(myGroup)
let obj = result as! Game
print("Game saved: \(obj.objectId)")
},
error: { (fault: Fault!) -> Void in
print("fServer reported an error: \(fault)")
})
}
dispatch_group_notify(myGroup, dispatch_get_main_queue(), {
print("Finished all requests.")
})
}