开发者

Delegate not working (Restkit related?)

I'm just getting started with Objective C and Restkit

I created a sample application and added the RKRequestDelegate in MyAppDelegate file

@interface MyAppDelegate : NSObject <UIApplicationDelegate, RKRequestDelegate> {…

and added

  RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"]; 
   NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
   [client get:@"/titles.json" delegate:self];

to MyAppDelegate.m in the

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method

I also added a method to MyAppDelegate.m

- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
    开发者_运维问答if ([request isGET]) {        
        NSLog (@"Retrieved : %@", [response bodyAsString]);
    }
 }

so far so good everything is working and I see the results from my Rails app in the output!!!

As those things don't belong into MyAppDelegate.m I'm moving that stuff into my models. In my Titles.h I added

@interface Titles : NSManagedObject <RKRequestDelegate> {

and in Titles.m I added

+ (void) update {   
    [[RKClient sharedClient] get:@"/titles.json" delegate:self];
}

and

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

In my MyAppDelegate.m I replaced :

 RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"]; 
   NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
   [client get:@"/titles.json" delegate:self];

with

  RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"]; 
   NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
  [Titles update];

when I run now I don't get any output. I put several breakpoints, one in the - (void)didFinishLoad:(RKResponse*)response in the RKRequest file and there the if test for :

if ([_delegate respondsToSelector:@selector(request:didLoadResponse:)]) {
        [_delegate request:self didLoadResponse:finalResponse];
    }

fails while it succeeds in my first attempt (when everything is in MyAppDelegate)

I checked the variable _delate in de debugger and it says: _delegate = MyAppDelegate in my first attempt and _delegate = Titles in my second attempt (both like it should)

Why does that respondsToSelector fail ? (the delegate is correct and the method exists in Titles)


Your problem is that your trying to set a class as the delegate:

+ (void) update {   
  [[RKClient sharedClient] get:@"/titles.json" delegate:self];
}

self here is the class Titles.

The callback is (as expected), an instance method:

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

You should have some kind of "DataModel" model class (perhaps "SongList" or whatever makes sense). This is often a singleton, so you have a +sharedModel instance. That instance is what is the delegate for RKClient.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜