0

iOS アプリケーションでネットワーク呼び出しを行うために ASIHTTPRequest フレームワークを使用しています。しかし、アプリケーションのすべてのコントローラーで直接使用したくありません。そこで、ASIHTTPRequest に関するレイヤーを作成することを考えました。私のコードでは、このレイヤーを使用して ASIHTTPRequest を使用しています。利点は、将来、このフレームワークを他のフレームワークに置き換えることができるはずであり、レイヤーが変更されるだけでコードが変更されないことです。そのための戦略はどうあるべきか知りたいです。クラスを ASIHTTPRequest クラスからサブクラス化するか、独自のクラスを実装する必要があります。実装を検討すべき方法は何ですか。

現在、私はこのように実装しています。私のラッパーは

MyRequestHandler.h : NSObject       
@property ASIHTTPRequest *asiHttpReq;    

-(void) sendAsyncGetRequest
{
    self.asiRequest = [ASIHTTPRequest requestWithURL:self.url];
    if(self.tag != 0){
        self.asiRequest.tag = self.tag;
    }
    [self.asiRequest setDelegate:self];
    [self.asiRequest startAsynchronous];        
}

- (void)requestFinished:(ASIHTTPRequest *)request{
     MyResponseObj *respone = <From request obj>
     if([delegate respondsToSelector:@selector(requestFinished:)]){
            [delegate performSelector:@selector(requestFinished:) withObject:response];
        }
 }

そして、私のviewcontrollerでこれを行います:

MyViewController.h : UIViewContoller
@property MyRequestHandler *reqHandler;

-(void) fireRequest
{
NSString* requestUrl = <create URL>;
if(requestUrl){

    // [self showLoadingIndicatorView];

    // Proceed for request.
    NSURL *url = [NSURL URLWithString:requestUrl];
    reqHandler = [MyRequestHandler requestWithURL:url];
    reqHandler.tag = 1000;
    [reqHandler setDelegate:self];
    [reqHandler sendAsyncGetRequest];
}
}

- (void)requestFinished:(MyResponse*) responseData{
        // Do Your parsing n all here.
}

- (void)requestFailed:(MyResponse*) responseData{
        // Handle the error here.
}

これは正しい方法ですか?ここでの問題は、viewcontroller で myrequesthandler のプロパティを作成したため、一度に 1 つの要求しか作成できず、同時に複数の要求を作成する ASIHTTPRequest の機能が失われることです。

このような問題にアプローチする方法を教えてください。

4

2 に答える 2

2

これは私が使用しているものです:

#import "ASIFormDataRequest.h"

@interface RequestPerformer : NSObject {
    id localCopy; // to avoid exec_bad_access with arc
    ASIHTTPRequest *getRequest;
    ASIFormDataRequest *postRequest;
}

@property (nonatomic, retain) id delegate;
@property (nonatomic, readwrite) SEL callback;
@property (nonatomic, readwrite) SEL errorCallback;

- (void)performGetRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector;
- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector;

@end

///

#import "RequestPerformer.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"

@implementation RequestPerformer

@synthesize delegate;
@synthesize callback, errorCallback;

- (void)performGetRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector {

    localCopy = self;

    self.delegate = requestDelegate;
    self.callback = requestSelector;
    self.errorCallback = errorSelector;

    NSMutableString *requestStringData = [[NSMutableString alloc] init];
    if (stringDictionary)
        for (NSString *key in [stringDictionary allKeys])
            [requestStringData appendFormat:@"%@=%@&", key, [stringDictionary objectForKey:key]];
    NSString *resultString = [requestStringData substringToIndex:[requestStringData length]-1];

    getRequest = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", string, [resultString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
    [getRequest setDelegate:self];
    [getRequest setRequestMethod:@"GET"];

    //NSLog(@"request url = %@", [getRequest.url absoluteString]);
    [getRequest startAsynchronous];
}

- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector {

    localCopy = self;

    self.delegate = requestDelegate;
    self.callback = requestSelector;
    self.errorCallback = errorSelector;

    NSURL *url = [NSURL URLWithString:string];

    postRequest = [[ASIFormDataRequest alloc] initWithURL:url];
    [postRequest setDelegate:self];
    [postRequest setRequestMethod:@"POST"];

    if (stringDictionary)
        for (NSString *key in [stringDictionary allKeys])
            [postRequest setPostValue:[stringDictionary objectForKey:key] forKey:key];

    if (dataDictionary)
        for (NSString *key in [dataDictionary allKeys])
            [postRequest setData:[dataDictionary objectForKey:key] forKey:key];

    //NSLog(@"request url = %@", [postRequest.url absoluteString]);
    [postRequest startAsynchronous];
}

#pragma mark - ASIHTTPRequest Delegate Implementation

- (void)requestFinished:(ASIHTTPRequest *)crequest {
    NSString *status = [crequest responseString];

    if (self.delegate && self.callback) {
        if([self.delegate respondsToSelector:self.callback])
            [self.delegate performSelectorOnMainThread:self.callback withObject:status waitUntilDone:YES];
        else
            NSLog(@"No response from delegate");
    }
    localCopy = nil;
}
- (void)requestFailed:(ASIHTTPRequest *)crequest {
    if (self.delegate && self.errorCallback) {
        if([self.delegate respondsToSelector:self.errorCallback])
            [self.delegate performSelectorOnMainThread:self.errorCallback withObject:crequest.error waitUntilDone:YES];
        else
            NSLog(@"No response from delegate");
    }
    localCopy = nil;
}

@end

それを使用するには、インポートRequestPerformer.hしてUIViewController次のようにします。

[requestManager performGetRequestWithString:tempString stringDictionary:stringDictionary dataDictionary:dataDictionary delegate:self requestSelector:@selector(requestSucceeded:) errorSelector:@selector(requestFailed:)];

パラメーター:

  • (NSString *)string- URL 文字列、リクエストを投稿する場所。
  • (NSDictionary *)stringDictionary- すべてのテキスト情報 (名前、ID など) を含む辞書。
  • (NSDictionary *)dataDictionary- すべてのデータ情報 (写真、ファイルなど) を含む辞書。
  • (id)requestDelegate- 以下のセレクターの実行を委譲します。
  • (SEL)requestSelector- リクエストが成功している間に実行されるセレクター。
  • (SEL)errorSelector- エラーが発生したときに実行されるセレクター。
于 2012-04-28T08:16:24.690 に答える
0

上記の回答 (demon9733) では、ラッパー クラスが保持されたデリゲート プロパティで作成されています。一般に、保持サイクルを削除するには、delegate プロパティを割り当てる必要があります。

于 2012-11-19T19:15:07.613 に答える