【问题标题】:singleton class loading data单例类加载数据
【发布时间】:2012-05-08 09:01:18
【问题描述】:

我创建了一个单例类,它应该处理我的应用程序的所有数据加载,所有数据都是从 Web 界面加载的,但在不同的请求中。

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

这将例如在一个请求中加载所有用户,然后在另一个请求中加载所有时区。

我的单例类如下所示:

// 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 的那些。

我认为我的数据模型必须以某种方式委托其他类?

【问题讨论】:

  • 委托数组可能会处理这个问题,但是您是否也检查了 Cocoa NSNotificationCenter 和 Key-Value Observing 功能?
  • 我刚刚根据 user523234 的回复尝试了 NSNotificationCenter,它工作正常

标签: ios delegates singleton loader


【解决方案1】:

在这种情况下,您可以使用 NSNotificationCenter。

【讨论】:

  • 谢谢,刚刚尝试了 NSNotificationCenter,它按我的意愿工作,谢谢!
【解决方案2】:

在IOS开发者库中,有一个名为MVCNetworking的Sample项目,http://developer.apple.com/library/ios/#samplecode/MVCNetworking/Introduction/Intro.html

它的NetworkManager是一个单例类,会将所有请求打包成一个NSOperation,然后添加到一个NSOperationQueue中。

因此,甚至可以管理排队的网络请求,并以有限的并发数进行处理。

仅供参考,这行代码可能会导致内存泄漏:

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

【讨论】:

  • 为什么会出现问题?我还需要自动释放吗?
  • object 'theConnection' 是你分配的,你没有在任何地方释放它。保留计数仍为 1。
  • 也许你可以在连接完成或失败时释放它。
  • 啊,我忘了放在那里了。
猜你喜欢
  • 2012-06-17
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多