views:

60

answers:

2

hi, I think i should be using selectors (or even a different paradigm), but even after R'ing TFM I can't figure out what i'm supposed to do. It's all related to callbacks from a delegate

I have my main Model object:

    @implementation Model
    @synthesize myConnection; // which is an NSURLConnection
    ...
    -(void)someMethod{
    MyConnectionDelegate *mcd = [[MyConnectionDelegate alloc]initWithCallingObject:self];
    myConnection = [[NSURLConnection alloc] initWithRequest:requestForToken delegate:mcd];
...
}
-(void)didGetCalledBack:(NSArray *)resultArray{
  NSLog(@"got the callback");
}

and then in my delegate:

@implementation MyConnectionDelegate
@synthesize callingObject; // which is of type id
@synthesize resultArray; // NSArray
-(id)initWithCallingObject:(id)caller{
  ...//std [self init] block
  self.callingObject  = caller;
  return self;
...
}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
...
}
//and all the other NSURLConnection delegate methods


- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
  ...
  // finish building array of results into self.resultArray
  [self.callingObject didGetCalledBack:self.resultArray];
}

So... 1) I think i should be using selectors, or something else rather than hardcoding the fact that the caller (delegator?) needs to implement -didGetCalledBack: Right? IF so, how? (and why, other than cleanliness)

2) Or is my whole implementation wrong in the way i'm attempting to use a callback from the delegate of the NSURLConnection back to the delegator wrong?

I've looked at the Apple samplecode etc but nothing i've seen ever has anything other than delegate:self. Maybe i should have delegate:self too for the NSURLConnection, but I'm making many connections and if i do delegate:self my delegate methods (like -didReceiveData:) become a big mess of if (connection ==connection1){ type code.

thanks, richard

+1  A: 

Maybe i should have delegate:self too for the NSURLConnection, but I'm making many connections and if i do delegate:self my delegate methods (like -didReceiveData:) become a big mess of if (connection ==connection1){ type code.

Then don't use explicit comparisons - use containers or similar abstractions to react to different connections.

E.g. to use the results of connections with different controls, use a dictionary that maps from NSURLConnections to those controls so the following:

if      (connection == connection1) [obj1 doStuff];
else if (connection == connection2) [obj2 doStuff];
// ...

becomes:

[[connectionClients objectForKey:connection] doStuff];
Georg Fritzsche
+2  A: 

I think i should be using selectors, or something else rather than hardcoding the fact that the caller (delegator?) needs to implement -didGetCalledBack: Right? IF so, how? (and why, other than cleanliness)

Nothing wrong with what you are doing. You might want to consider declaring a protocol for a calling object e.g.

@protocol CallingObject <NSObject>
    -(void) didGetCallBack: (NSArray*) resultArray;
@end

And then

@interface Model : NSObject <CallingObject> // ...

and

@interface MyConnectionDelegate : NSObject
{
    // ...
}

-(id) initWithCallingObject: (id<CallingObject>) calller;

// ...

@end

That will give you some compile time checking that the calling object implements the required method(s).

JeremyP
Ah yes, that's a good idea - I was wondering how to get rid of that compile warning. (Now I've come to learn that warnings are really bad, unlike previous languages i've worked in where warnings are just bad ;))
richard