Reputation: 78
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
Upvotes: 0
Views: 157
Reputation: 86651
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).
Upvotes: 2
Reputation: 98994
Maybe i should have delegate:self too for the
NSURLConnection
, but I'm making many connections and if i dodelegate:self
my delegate methods (like-didReceiveData:
) become a big mess ofif (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 NSURLConnection
s to those controls so the following:
if (connection == connection1) [obj1 doStuff];
else if (connection == connection2) [obj2 doStuff];
// ...
becomes:
[[connectionClients objectForKey:connection] doStuff];
Upvotes: 1