i have a Problem.
I am working with a Graph Database. For each User i want to write a List of Artists to a database.
I call my saveProfileData and now the Artist should be added to the Database one by one. But the executeCypher Method(from another api) does not get called after the addArtistsToDatabase gets called, but some time later. So not all Artists are written to the Database. I want to implement my Method that for each artist the actual Artist gets written to the Database, and waits until its completed. I am quite new to Swift and don't know how to solve this.
func saveProfileData(){
let p = self.profile
...
//add Artists
for a:Artist in p.artists{
self.addArtistToDatabase(a)
}
}
func addArtistToDatabase(a: Artist){
let cyperQuery: String = "MATCH (a:Artist) WHERE a.id IN [\"\(a.id)\"] RETURN a"
let cyperParams: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
client.executeCypher(cyperQuery, params: cyperParams, completionBlock: {(cypher, error) in
print("response from cyper \(cypher)")
//check if result is empty
let response = cypher?.data
var isEmpty: Bool = false
if((response?.count) == 0){
isEmpty = true
}
if(isEmpty){
//if so create new node for each artist and add the relationhip between artist and user
let cyperQuery1: String = "CREATE (n:Artist {id:\"\(a.id)\", name:\"\(a.name)\"})"
let cyperParams1: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
self.client.executeCypher(cyperQuery1, params: cyperParams1, completionBlock: {(cypher, error) in
print("response from cyper \(cypher)")
})
let query: String = "MATCH (a:User),(b:Artist) WHERE a.id=\"\(self.profile.id)\" AND b.id=\"\(a.id)\" CREATE (a)-[r:ListenTo {interested: \"\(a.display)\"}]->(b) RETURN r "
let params: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
self.client.executeCypher(query, params: params, completionBlock: {(cypher, error) in
print("response from cyper \(cypher)")
})
}else{
//update relationship
let query: String = "MATCH (n:User {id:\"\(self.profile.id)\"})-[r:ListenTo]->(a:Artist {id:\"\(a.id)\"}) SET r.interested = \"\(a.display)\" RETURN r"
let params: Dictionary<String, AnyObject> = Dictionary<String, AnyObject>()
self.client.executeCypher(query, params: params, completionBlock: {(cypher, error) in
print("response from cyper \(cypher)")
})
}
})
}