デリゲート パターンのメモリ管理がよくわかりません。
コントローラーで、そのオブジェクトを所有している場合。それに強いポインタを割り当てる必要があります。それが所有するオブジェクトが失われないように。
非同期接続を行うのに役立つ小さなライブラリ クラスを作成しました。これは、そのプロトコルを採用する ViewController への弱いポインタを保持します。接続が完了すると、データが ViewController に返されます。
#import <Foundation/Foundation.h>
@protocol AsyncConnectionDelegate;
@interface AsyncConnection : NSObject <NSURLConnectionDataDelegate>
@property (weak, nonatomic) id <AsyncConnectionDelegate> delegate;
-(void)callAsyncConnectionAtUrl:(NSString *)url dictionary:(NSDictionary *)dictionary method:(NSString *)method delegate:(id)delegate;
@end
@protocol AsyncConnectionDelegate <NSObject>
- (void)finishConnectionWithData:(NSData *)data connection:(AsyncConnection *)connection;
@end
使用法: (ボタンが押されたとき)
// user input
NSString *username = _usernameTextField.text;
NSString *password = _pwdTextField.text;
//create dictionary key-value pair for transformming into NSData
NSMutableDictionary *loginKeyValue = [[NSMutableDictionary alloc]init];
[loginKeyValue setObject:username forKey:@"username"];
[loginKeyValue setObject:password forKey:@"password"];
AsyncConnection *conc = [[AsyncConnection alloc]init];
[conc callAsyncConnectionAtUrl:@"http://localhost:3000/login.json" dictionary:loginKeyValue method:@"POST" delegate:self];
これ*conc
はローカル変数のみであり、View Controllerはそれへの強い参照を保持していませんでした。したがって、私の観点では、メソッドの実行が終了したときに強制終了されるはずです。ただし、それは生きていて、ViewController にデータを送り返すことができます。
委任方法
- (void)finishConnectionWithData:(NSData *)data connection:(AsyncConnection *)connection{
NSLog(@"Connection Object : %@", connection );
Member *member = [Member initWithData:data];
NSLog(@"member username \n %@",member.username);
NSLog(@"member password \n %@",member.password);
NSString *msg = (member.username)?@"Login Success":@"Failed to login";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"NOTICE!!" message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
クラスはこのメソッドを使用してデータを送り返します。これは NSURLConnection のデリゲート メソッドです。
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
[_delegate finishConnectionWithData:_downloadData connection:self];
}
接続オブジェクトのメモリ アドレスを 2 回記録しようとしましたが、それらは異なります。(ボタンを2回蹴った) . だから、接続オブジェクトがいつ殺されるのだろうか。