【发布时间】:2011-04-20 01:19:59
【问题描述】:
从我的 ApplicationDelegate 中,我正在通过网络进行 NSURLConnection 获取(它包含在一个类中,如下所示)。这似乎工作正常:我得到了 didReceiveData 中的所有数据,我得到了完成调用 connectionDidFinishLoading。在 connectionDidFinishLoading 结束时,我实例化了一个或多个稍微不同的包装类,但它们本质上是相同的。问题是第二个 NSURLConnection 的委托从未调用过它的方法。
我看过manydifferentanswers,但都无济于事。我没有产生任何新线程,并且我在整个代码中乱扔的所有 [NSThread isMainThread] 检查都返回 true。
我被难住了。谁能帮我吗?以下是相关代码:
应用代理:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ConnectionWrapper* w = [[ConnectionWrapper alloc] initWithParams:self
url:[NSURL URLWithString:<url>]];
[w beginFetch];
return YES;
}
...
-(void)fetchCompleted:(NSURL*)url directory:(NSString*)directory
{
NSLog(@"fetch completed");
}
-(void)fetchFailed:(NSURL*)url
{
NSLog(@"fetch failed");
}
...
连接包装器:
-(id)initWithParams:(id<ConnectionWrapperDelegate>)d url:(NSURL*)url
{
delegate = d;
connURL = url;
return [self init];
}
-(void)beginFetch
{
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:connURL];
NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn release];
[request release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"append");
[responseData appendData:data];
}
- (void) connectionDidFinishLoading: (NSURLConnection*) connection
{
... parsing ....
DifferentConnectionWrapper* w = [[DifferentConnectionWrapper alloc] initWithParams:self
url:[NSURL URLWithString:<different url>]];
[w beginFetch];
}
-(void)fetchCompleted:(NSURL*)URL
{
NSLog(@"completed: %@", URL);
}
-(void)fetchFailed:(NSURL*)URL
{
NSLog(@"failed");
}
不同的连接包装器:
-(id)initWithParams:(id)d url:(NSURL*)url { 委托 = d;
connURL = url;
return [self init];
}
-(void)beginFetch
{
NSURLRequest* request = [[NSURLRequest alloc] initWithURL:connURL];
NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn release];
[request release];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"append");
[responseData appendData:data];
}
- (void) connectionDidFinishLoading: (NSURLConnection*) connection
{
... parsing ....
DifferentConnectionWrapper* w = [[DifferentConnectionWrapper alloc] initWithParams:self
url:[NSURL URLWithString:<different url>]];
[w beginFetch];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"got response");
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"got data");
[responseData appendData:data];
}
- (void) connectionDidFinishLoading: (NSURLConnection*) connection
{
NSLog(@"image saver completed: %@", connURL);
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"error");
}
ConnectionWrapper 和DifferentConnectionWrapper 具有相似的功能,但为了简洁,我在此省略了其他逻辑。
感谢您的帮助。我很感激。
【问题讨论】:
标签: iphone objective-c delegates nsurlconnection nsurlrequest