iOS Swift Read Request


#1

I was wondering if there were any resources available to demonstrate how to conduct a EXORpcReadRequest for iOS using Swift? Though I’ve worked with the API before on other platforms, I am super new to iOS as well so I am trying to understand this process without a very strong understanding of Swift/Objective C.

I have imported the EXORpc cocoapod appropriately and I was able to accomplish a write command to a specific RID using an example in the forum, but when I tried to port this over to a read command, I wasn’t sure where the data was once I completed the read command. I would have presumed it was available in the callback but I’ve been going through the EXO files and can’t find any reference to a data object.

Below is my lousy code. I’d love some input on what additional step needs to be accomplished in order to access the JSON data.

    let questionRID = EXORpcResourceID(byRID: "RID")
    let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration()
    let domain = NSURL(string: "https://m2.exosite.com")
    let rpc = EXORpc(domain: domain, sessionConfiguration: sessionConfig)
    let authKey = EXORpcAuthKey(CIK: "CIK")
    print("Start")

    var req = EXORpcReadRequest.readWithRID(questionRID, complete: {_,_ in
            print("Read Request Created")
    })
    
    rpc!.doRPCwithAuth(authKey!, requests: [req], complete: { (error: NSError?) -> Void in
        if ((error) != nil){
            print(error)
        }
    })

I’m slowly piecing together my iOS knowledge together so if there are any very apparent voids of basic knowledge, recommendations on where I should focus my efforts would be appreciated as well.

Thanks much!


#2

Hi @boland -

Thanks for posting! I haven’t programmed much in Swift before and I have not yet used that library. Let me see if I can find someone who knows, or get educated about it myself.

Just out of curiosity, what happens if you try to print the req variable?

Cheers,
-Martin


#3

Hey Martin,

When I print the req variable I get:

<EXORpcReadRequest: 0x15f61d280, {
    arguments =     (
        958a60456ac5bb44edc585ef0cae1b8f14791abe,
                {
            limit = 1;
            selection = all;
            sort = desc;
        }
    );
    procedure = read;
}>

My impression is that the EXORpcReadRequest handles the details of the read request itself and the EXORpc object conducts the communication based on the requests that it has been provided. But I’m just not sure where the data is put once the requests have been processed.

Thanks for your help!

Jack


#4

I think I have figured it out. I declared an NSArray() object and then passed this into the callback method in the declaration of the EXORpcReadRequest. Then when the callback is called, the data is available and can be read. I then very clunkily, inefficiently and presumably incorrectly was able to parse out the contents of the raw data to get the value that I requested. I certainly need some work to understand how to properly deal with the data, but this seemed to work fine for now.

    var readResult = NSArray()
    
    var req = EXORpcReadRequest.readWithRID(questionRID, complete: { (readResult, error: NSError?) in
        print(readResult)
        var rawArray = String(readResult!).componentsSeparatedByString(":")
        var question: String = rawArray[5]
        question = String(question.characters.dropLast())
        question = String(question.characters.dropLast())
        self.questionLabel.text = question
    })
    rpc!.doRPCwithAuth(authKey!, requests: [req], complete: { (error: NSError?) -> Void in
        if ((error) != nil){
            print(error)
        }
    })

#5

@boland

Awesome! I am glad to hear that you were able to figure it out.

Thanks for posting your solution back,
-Martin