Start a new topic
Answered

How to get response.body data in iOS client

I am using saveObject method on the client side (iOS) and in the doPreSave method set

response.body = {message: "Some text "};

response.complete(201);


upon certain  condition so the object is not saved and the message sent back to the user. However, I can't find a way to get that response.body value in the client. objectsOrNil contains the same object to be saved that was sent via request and not the message I need to pass to the user. 


Best Answer
Hey Mark,

In your collection->PreSave hook(), you can handle the condition as an error condition as below:

 

if (!result._id) { 
 response.error("No Rooms Available");
}

 


In your iOS code, you can retrieve the error message using localizedFailureReason:

 

store.saveObject(
            jobRole1,
            withCompletionBlock: { (objectsOrNil: [AnyObject]!, errorOrNil: NSError!) -> Void in
                if errorOrNil != nil {
                    //save failed
                    NSLog("Save failed with error: %@", errorOrNil.localizedFailureReason!)
                   
                } else {
                    //save was successful
                    NSLog("Successfully saved”)
                }
            },
           
    withProgressBlock: nil
        )

 


Let me know if it works for you.



Thanks,
Pranav
Kinvey Support

 


Hi,


As you can see in the documentation http://devcenter.kinvey.com/ios/guides/datastore#Saving for saveObject method of iOS SDK, the method always returns an array of objects on success.


I would suggest you go through Business Logic Guide: http://devcenter.kinvey.com/ios/guides/business-logic to understand the use of collection hooks better.

  • Ideally, you should use in response.continue in case of success for presave hooks and response.error to report an error.
  • The returned object contains the complete objecct saved in the backend.



Regards,

Wani

Kinvey Support

Hello Wani,


Thank you for the prompt reply.  I have read and used the guides since working with Kinvey SDK. It is this tutorial example has inspired me for this approach.

http://devcenter.kinvey.com/ios/tutorials/bl-using-collection-hooks-effectively

A feature of the app we develop has a similar reservation use case. So this code from the tutorial returns a message 

// If no rooms found, End the request without saving, // with a message of "no room available"

if (!result._id) { response.body = {message: "No Rooms Available"}; 

 response.complete(); // completes the request and returns the result to the client 

}

The question is how do you extract this message on the client side if the object is not saved as is the case in this example. I totally understand the usual case when the object is saved.


Thank you,

Mark

Answer
Hey Mark,

In your collection->PreSave hook(), you can handle the condition as an error condition as below:

 

if (!result._id) { 
 response.error("No Rooms Available");
}

 


In your iOS code, you can retrieve the error message using localizedFailureReason:

 

store.saveObject(
            jobRole1,
            withCompletionBlock: { (objectsOrNil: [AnyObject]!, errorOrNil: NSError!) -> Void in
                if errorOrNil != nil {
                    //save failed
                    NSLog("Save failed with error: %@", errorOrNil.localizedFailureReason!)
                   
                } else {
                    //save was successful
                    NSLog("Successfully saved”)
                }
            },
           
    withProgressBlock: nil
        )

 


Let me know if it works for you.



Thanks,
Pranav
Kinvey Support

 

Hi Pranav,


I think it is a good enough solution and should work for us although we may or may not have to check for a real error in the message, not sure at this point.


Thanks

Mark

Login or Signup to post a comment