Delegato non funziona (Restkit correlato?)
-
28-10-2019 - |
Domanda
Ho appena iniziato con l'obiettivo C e Restkit
Ho creato un'applicazione di esempio e ho aggiunto il file rkrequestdelegate nel file myappdelegate
@interface MyAppDelegate : NSObject <UIApplicationDelegate, RKRequestDelegate> {…
e aggiunse
RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"];
NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
[client get:@"/titles.json" delegate:self];
a myappdelegate.m in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method
Ho anche aggiunto un metodo a myappdelegate.m
- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
if ([request isGET]) {
NSLog (@"Retrieved : %@", [response bodyAsString]);
}
}
Fin qui tutto bene tutto funziona e vedo i risultati dell'app Rails nell'output !!!
Poiché quelle cose non appartengono a myappdelegate.m Sto spostando quella roba nei miei modelli. Nei miei titoli.h ho aggiunto
@interface Titles : NSManagedObject <RKRequestDelegate> {
e in titoli.m che ho aggiunto
+ (void) update {
[[RKClient sharedClient] get:@"/titles.json" delegate:self];
}
e
- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
if ([request isGET]) {
NSLog (@"Retrieved : %@", [response bodyAsString]);
}
}
Nel mio myappdelegate.m ho sostituito:
RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"];
NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
[client get:@"/titles.json" delegate:self];
insieme a
RKClient* client = [RKClient clientWithBaseURL:@"http://localhost:3000"];
NSLog(@"I am your RKClient singleton : %@", [RKClient sharedClient]);
[Titles update];
Quando corro ora non ottengo alcun output. Ho messo diversi punti di interruzione, uno nel - (void)didFinishLoad:(RKResponse*)response
Nel file rkrequest e lì il test if per:
if ([_delegate respondsToSelector:@selector(request:didLoadResponse:)]) {
[_delegate request:self didLoadResponse:finalResponse];
}
fallisce mentre ci riesce nel mio primo tentativo (quando tutto è in myappdelegate)
Ho controllato la variabile _delata in de debugger e dice: _delegate = myappdelegate nel mio primo tentativo e _delegate = titoli nel mio secondo tentativo (entrambi come dovrebbe)
Perché quel rispondente fallisce? (Il delegato è corretto e il metodo esiste nei titoli)
Soluzione
Il tuo problema è che stai cercando di impostare una classe come delegato:
+ (void) update {
[[RKClient sharedClient] get:@"/titles.json" delegate:self];
}
self
Ecco il classe Titles
.
Il callback è (come previsto), un metodo di istanza:
- (void) request: (RKRequest *) request didLoadResponse: (RKResponse *) response {
if ([request isGET]) {
NSLog (@"Retrieved : %@", [response bodyAsString]);
}
}
Dovresti avere una sorta di classe modello "DataModel" (forse "songlist" o qualunque cosa abbia senso). Questo è spesso un singleton, quindi hai un +sharedModel
esempio. Quell'istanza è a cosa serve il delegato RKClient
.