0

In my first ViewController (MonitorViewController) this is in the interface file MonitorViewController.h:

#import <RestKit/RestKit.h>
@interface MonitorViewController : UIViewController <RKRequestDelegate>

In MonitorViewController.m ViewDidLoad method, I have this at the end:

RKClient* client = [RKClient clientWithBaseURL:@"http://192.168.2.3:8000/DataRecorder/ExternalControl"]; 
NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
[client get:@"/json/get_Signals" delegate:self];

The implementation of delegate methods in MonitorViewController.m:

- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
    if ([request isGET]) {        
        NSLog (@"Retrieved : %@", [response bodyAsString]);
    }
}

- (void) request:(RKRequest *)request didFailLoadWithError:(NSError *)error
{
    NSLog (@"Retrieved an error");  
}

- (void) requestDidTimeout:(RKRequest *)request
{
    NSLog(@"Did receive timeout");
}

- (void) request:(RKRequest *)request didReceivedData:(NSInteger)bytesReceived totalBytesReceived:(NSInteger)totalBytesReceived totalBytesExectedToReceive:(NSInteger)totalBytesExpectedToReceive
{
    NSLog(@"Did receive data");
}

My AppDelegate method DidFinishLaunchingWithOptions method only returns YES and nothing else.

Torben
  • 101
  • 1
  • 12

1 Answers1

0

I recommend using RestKit framework. With restkit, you simply do:

// create the parameters dictionary for the params that you want to send with the request
NSDictionary* paramsDictionary = [NSDictionary dictionaryWithObjectsAndKeys: @"00003",@"SignalId", nil];
// send your request
RKRequest* req = [client post:@"your/resource/path" params:paramsDictionary delegate:self];
// set the userData property, it can be any object
[req setUserData:@"SignalId = 00003"];

And then, in the delegate method:

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    // check which request is responsible for the response
    // to achieve this, you can do two things
    // check the parameters of the request like this
    NSLog(@"%@", [request URL]); // this will print your request url with the parameters
    // something like http://myamazingrestservice.org/resource/path?SignalId=00003
    // the second option will work if your request is not a GET request
    NSLog(@"%@", request.params); // this will print paramsDictionary
    // or you can get it from userData if you decide to go this way
    NSString* myData = [request userData];
    NSLog(@"%@", myData); // this will log "SignalId = 00003" in the debugger console
}

So you will never need to send the parameters that are not used on the server side, just to distinguish your requests. Additionally, the RKRequest class has lots of other properties that you can use to check which request corresponds to the given response. But if you send a bunch of identical requests, I think the userData is the best solution.

RestKit will also help you with other common rest interface tasks.

lawicko
  • 7,246
  • 3
  • 37
  • 49
  • would it help me to distinguish which response corresponds to which request if I send e.g. 10 requests like this: http://mywebservice.com/myservice?dev=1 http://mywebservice.com/myservice?dev=2 ... http://mywebservice.com/myservice?dev=10 – Torben Feb 20 '12 at 14:02
  • Yes, but I don't recommend it if your web service doesn't really make use of the *dev* parameter. See my updated answer. – lawicko Feb 20 '12 at 15:00
  • Maybe I haven't explained myself well enough. I'll try again :-) – Torben Feb 22 '12 at 12:49
  • Thanks, Lawicko. Maybe I haven't explained myself well enough. I may want to do e.g. 4 asynchronous requests towards the same web service. Each request will have one and only one parameter: SignalId. The answer is a json formatted string returning a simple string. I can't see how I can distinguish the responses from each other which I do need to do, as the information returned cannot be mixed. Will RESTkit help me doing this job - and how? I suppose the SetUserData would be used for setting the SignalId in this case. Are you saying that in the delegate method myData will be SignalId? – Torben Feb 22 '12 at 13:18
  • Yes, RestKit will definitely help you, check my updated answer. – lawicko Feb 22 '12 at 14:33
  • Okay - thanks. It looks smart. I think I'll have to try that RESTkit thing :-). I will come back in case I experience any problems. – Torben Feb 22 '12 at 15:08
  • Hi Lawicko. Sorry to disturb you. Can you please enlighten me. What is "client" in your code: RKRequest* req = [client post:@"your/resource/path".... I have tried to define it as RKClient. Is that correct? Second, I get an error for the "delegate:self" statement saying "Incompatible pointer types sending 'PulseChannels *const_strong' to parameter of type 'NSObject*'". PulseChannel is my class in which I set up this stuff. Any idea of what is wrong? – Torben Feb 24 '12 at 13:23
  • Yes, the *client* in my code is the [RKClient](http://restkit.org/api/master/Classes/RKClient.html) instance. Read the RestKit documentation to learn how to instantiate the client or obtain the shared instance. As for the delegate, it needs to be derived from [NSObject](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html) and implement required methods from [RKRequestDelegate protocol](http://restkit.org/api/master/Protocols/RKRequestDelegate.html). – lawicko Feb 24 '12 at 14:02
  • The great overview of the RestKit features can be found [here](https://github.com/RestKit/RestKit/blob/master/Docs/Presentations/Introduction%20to%20RestKit/Introduction%20to%20RestKit.pdf). – lawicko Feb 24 '12 at 14:04
  • Hi Lawicko. Could you please look at my own answer below and maybe help me out... – Torben Feb 26 '12 at 11:58
  • This means that your request either timed out or failed. Check the [documentation](http://restkit.org/api/master/Protocols/RKRequestDelegate.html) to see the delegate methods for catching the time out and failure. You need to implement them to see what went wrong. – lawicko Feb 26 '12 at 18:25
  • Hi again. Sorry being such a annoying person. I have implemented both timeout and failure (i.e. didLoadResponse, didFailLoadWitherror, requestDidTimeout and didReceivedError). Please look at the code in my edited question. None of these methods are ever called (no breakpoints are hit and no logging are done. Why is that? Should I do anything else? I wonder if you have e.g. a small working example or could point one out. I have been searching, but I haven't been able to find a full working example. – Torben Feb 27 '12 at 09:33
  • It seems that you are doing everything correctly. This line: `[client get:@"/json/get_Signals" delegate:self];` should return `RKRequest` instance, check if it is not nil. If I were you, I'd debug inside RestKit to see what happens when the line is executed. – lawicko Feb 27 '12 at 10:13
  • I now tried to debug inside restKit. I can't find any RKRequest variable so aI create a local variable doing so: RKRequest *myreg = [client get:@"/json/get_Signals" delegate:self]; I've inspected the object but I can't figure out if anything is wrong. Id does say _isLoading = NO, _isLoaded = NO - is that an error. I could email you a screen print of the RKRequest object if you would help me out. – Torben Feb 27 '12 at 11:48
  • If you have any additional information that could help to diagnose the problem, then attach it to the question. If you get the request from the *get* call, then please tell me what are the values of the following properties of the *myreg*: delegate, queue, URL and URLRequest. – lawicko Feb 27 '12 at 12:00
  • Apparently I'm not allowed to add pictures in the Question. If you have an email I can send it directly. The URL property of myReq contains the right URL (http://192.168.2.3:8000/DataRecorder/ExternalControl/json/get_Signals). Im not able to tell if the other properties are correct but both Delegate, Queue and URLRequest are not nil... – Torben Feb 27 '12 at 12:29
  • Do you actually return something from your `application: didFinishLaunchingWithOptions:` method? You need to return YES to proceed with initialization. According to the [documentation](https://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIApplicationDelegate_Protocol/Reference/Reference.html), your application is still in the inactive state when this method is called, so you better move your test code to somewhere after the initialization is complete. – lawicko Feb 27 '12 at 14:30
  • Well spotted , that could be one thing causing an error. I really appreciate your help, Lawicko. I am a "newbe" to this IOS thing. I now have moved the code to a ViewController class - now setting up the client and the request just before leaving the ViewDidLoad method of this ViewController. In the same ViewController I have implemented the methods DidLoadResponse, DidFailLoadWithError, RequestDidTimeout and DidReceivedData. Seems as it sets up the client and the request OK but still no callback to any of the delegate methods.... – Torben Feb 27 '12 at 16:59
  • Please update the question with your current code, otherwise I can only guess. – lawicko Feb 27 '12 at 17:04
  • Sorry Lawicko. The present code is now in my question part. Let me know if you need anything else. Thanks. – Torben Feb 27 '12 at 18:06
  • Hi again. I've just been googling around. It comes to my notice, that many examples have this code in the AppDelegate.m application didFinishLaunchingWithOptions method: RKClient* client = [RKClient clientWithBaseURL:@"http://192.168.2.3:8000/DataRecorder/ExternalControl"]; NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]); [client get:@"/json/get_Signals" delegate:self]; This is what I had previously, too - now moved to my ViewController.m file ViewDidLoad method. – Torben Feb 28 '12 at 01:24
  • I don't know what can be wrong with your code. Just put a debugger on line 359 in RKClient.m and track it from there. – lawicko Feb 28 '12 at 10:43