delay action when using Storage Service

0 votes

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
        }
    }

 

asked Mar 24, 2016 in iOS by khanhtnguyen85 (15 points)

1 Answer

0 votes

Hi Khanh,

Putting some delay to print the response is not the recommende way because to put delay you will be giving some constant amount of time but this does not guarantee that your response comes before that fixed delay time.

You can implement delegate pattern to notify the ViewController that your DataManager has got the response. The Delegate method can be implemented in the ViewController. Once response comes, you can trigger the delegate method from your DataManager to perform the task need to be done with the response received.
 
Let me know if you have any other queries.
 
Thanks.
 
answered Mar 25, 2016 by rajeev.etc (1,660 points)
Hi Rajeev,

Thank you for your response. Can you please give more details on the delegate method? I am very new to programming and I need a little more guidance and direction.

Best,
Khanh
Hi Khanh,

You just have to implement one method in your view controller to handle the json and call the same from your DataManager class once you get the response from App42.
Let me know if you have any other queries.

Thanks.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...