Creating and Using Your Own Delegates in Objective-C

I've been doing iPhone development for about a year now and have used the delegation pattern many times. Specifically with regards to UITableViews and object serialization (NSCoding, NSCopying). Wikipedia has a great write up on the delegation pattern as well as examples in a few languages like Java, C++ and even Objective-C. The problem is that these examples, for me, don't express how powerful and how easy they are to implement and use.

Recently, I've been working on a project where I needed to access a series of web services to post and get my data. This is a pretty common thing to do however, if you look at the many examples of how to do this you'll find overly simplistic code that throws everything into a single class/file and completely ignored code reuse or good Object Oriented design.

The Scenario
My scenario was this; I needed to be able to access several web services from several different form/classes in the application. I not only wanted the class to send the data to the web service, but also retrieve it and do something with the data it got back, which in my case was a SOAP response.

The Problem
Calling a SOAP web service with the iPhone relies on using the NSURLConnection which sends an asynchronous call to the server. It relies on it's own delegate to deal with each process of the call.  My initial web service impementation looked like this:

01. #import "UserWebService.h"
02.  
03. @implementation UserWebService
04.  
05. @synthesize users;
06.  
07. -(id)initWithUserData: (User *)user {
08. // code here to initialize the request with user data
09. }
10.  
11. -(void) send {   
12. NSURLConnection *theConnection = [[NSURLConnection alloc]initWithRequest:theRequest delegate:self];
13. [theConnection release];
14. }
15.  
16. -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
17. NSLog(@"Received Response");
18. [webData setLength:0]; 
19. }
20.  
21. -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {   
22. NSLog(@"Got Data");
23. [webData appendData:data];
24. }
25.  
26. -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
27. [webData release];
28. [connection release];
29. }
30.  
31. -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
32.  
33. // code here to do something with all the data (XML) that was returned
34.  
35. }

 And then to use this I have the following code in another class, a UIViewController subclass where I needed the data

01. User *user = [[User alloc]init];
02. [user setUserId:@"userid"];
03. [user setPassword:@"password"];
04.  
05. ws = [[UserWebService alloc ]initWithUserData:user];
06. [user release];
07. [ws send];
08.  
09. NSArray *users = [ws users];

 But here's the problem.  When we call [ws users] to get the array of users back from the web service there is no way to know if that web service has completed yet.  I kept getting zero users back even though I knew, by stepping through my code, it was getting 3 users back.

The Solution

 I needed to find a way to notify my UIViewController subclass that the web service had completed.  This is where our custom delegate comes to play.  First, you define a protocol for your delegate in a header file:

1. @protocol WsCompleteDelegate
2. -(void) finished;
3. @end

 Next, we modify our web service to notify the delegate when it has completed.  In the web service class header file we define a delegate property like so:

01. @interface UserWebService : NSObject {
02.  
03. id <WsCompleteDelegate> delegate;
04.  
05. }
06.  
07. @property (retain, nonatomic) id <WsCompleteDelegate> delegate;
08.  
09. @end

 Then in our web service class' connectionDidFinishLoading method, we simply call our delegate method:

1. -(void)connectionDidFinishLoading:(NSURLConnection *)connection {
2.  
3. // do all necessary wrap up work
4. [delegate finished];
5.  
6. }

 The next step is to use this delegate in our UIViewController subclass.  First, we modify the header file like so:

1. @interface LoginViewController : UIViewController <WsCompleteDelegate> {
2. // other code here
3. }

 Then in the implementation file we have to do two things. First, we tell the web service instance what it's delegate is, which is the LoginViewController, or in Objective-C, 'self' and then  we impement the delegate's 'finish' method.

01. -(IBAction)submit {
02. User *user = [[User alloc]init];
03. [user setUserId:@"username"];
04. [user setPassword:@"password"];
05.  
06. ws = [[UserWebService alloc ]initWithUserData:user];
07.  
08. ws.delegate = self;
09.  
10. [user release];
11. [ws send]; 
12. }
13.  
14. -(void) finished {
15. NSArray *users = [ws users];   
16. }

 When the web service class calls [delegate finished] it notifies all classes listening to that delegate and the 'finished' method is called. We now know the web service has completed and can safely get the array of users from it.  Then we just do whatever we need to do with them.

Conclusion

There are lots of possibilites for the delegation pattern and this is just one of them.  But it proved to be very simple to implement and an elegant solution to a possibly combersome problem.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值