Hi, I'm using SDK version 3.4 and Swift language. When I use find document method in Storage Service to get my json, the response always comes later when my action in ViewController and my json always prints nil. I use dispatch_after to delay the print action but it still does not work. can you please help?
class DataManager {
var wishList = App42API.buildStorageService() as? StorageService
var json: [String: AnyObject]! // I need to assign data into this variable
init() {
wishList?.findAllDocuments(dataBase, collectionName: collectionName, completionBlock: {(success, response, exception) -> Void in
if(success){
let storage = response as! Storage
let jsonDocList = storage.jsonDocArray
var jsonString: String {
var someString: String = ""
for jsonDoc in jsonDocList{
someString = jsonDoc.jsonDoc
}
return someString
}
let data = jsonString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
do {
self.json = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions()) as! [String: AnyObject]
}catch{
print(error)
}
}else{
NSLog("%@", exception.reason!)
NSLog("%d", exception.appErrorCode)
NSLog("%d", exception.httpErrorCode)
NSLog("%@", exception.userInfo!)
}})}})}
The print line always prints "error" in ViewController
override func viewDidLoad() {
super.viewDidLoad()
let delayInSeConds = 2.0
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delayInSeConds * Double(NSEC_PER_SEC)))
var GlobalMainQueue: dispatch_queue_t{
return dispatch_get_main_queue()
}
dispatch_after(popTime, GlobalMainQueue){
if DataManager.sharedInstance.json != nil{
print("json is not nil")
}else{
print("error") // It always prints error since json is nil
}
}