1

アプリケーションのすべてのデータ読み込みを処理するシングルトン クラスを作成しました。すべてのデータは Web インターフェイスから読み込まれますが、異なる要求で読み込まれます。

DataModel *instance = [[[DataModel instance] init] autorelease];
[instance doRequestFromLocation:@"users" withPostType:@"GET" andData:@"data"];
[instance doRequestFromLocation:@"timezones" withPostType:@"GET" andData:@"data"];

これにより、たとえば、1 つのリクエストですべてのユーザーが読み込まれ、別のリクエストですべてのタイムゾーンが読み込まれます。

私のシングルトンクラスは次のようになります。

// Send a curl request
- (void)doRequestFromLocation:(NSString *)location withPostType:(NSString *)type andData:(NSString *)data
{    
    // NSArray *keys = [NSArray arrayWithObjects:@"username", @"password", nil];
    // NSArray *objects = [NSArray arrayWithObjects:@"test", @"test", nil];
    // NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObject:objects forKey:keys];


    NSString *username = @"username";
    NSString *password = @"password";
    NSString *urlString = [url stringByAppendingFormat:@"%@", location];

    NSMutableString *loginString = (NSMutableString *)[@"" stringByAppendingFormat:@"%@:%@", username, password];
    NSLog(@"%@", loginString);

    NSString *encodedLoginData = [Base64 encode:[loginString dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *authHeader = [@"Basic " stringByAppendingFormat:@"%@",encodedLoginData];

    NSLog(@"%@", authHeader);

    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    [theRequest setHTTPMethod:type];

    [theRequest setValue:authHeader forHTTPHeaderField:@"Authorization"];
    [theRequest setValue:@"application/xml" forHTTPHeaderField:@"Content-type"];
    [theRequest setValue:@"application/xml" forHTTPHeaderField:@"Accept"];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
}

#pragma mark -
#pragma mark Responses

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError");
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"%@", responseString);
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading");
}

これはすべて正常に機能しますが、次のステップで少し立ち往生しています。

どこからでも doRequestFromLocation を呼び出せるようにしたいのですが (現在は可能です)、didReceiveData に入るクラスに他のクラスが応答するようにしたいと考えています。

私のデータモデルは何らかの形で他のクラスを委任する必要があると思いますか?

4

2 に答える 2

2

この場合、NSNotificationCenterを使用できます。

于 2012-05-08T09:08:50.497 に答える
0

IOS 開発者ライブラリには、MVCNetworking という名前のサンプル プロジェクトがあります。http://developer.apple.com/library/ios/#samplecode/MVCNetworking/Introduction/Intro.html

その NetworkManager はシングルトン クラスであり、すべてのリクエストを NSOperation としてパックし、NSOperationQueue に追加します。

そのため、キューに入れられたネットワーク要求を管理し、制限された同時実行数で処理することもできます。

参考までに、この行コードはメモリ リークを引き起こす可能性があります。

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
于 2012-05-08T09:14:33.990 に答える