從我的ApplicationDelegate中,我通過網絡進行NSURLConnection獲取(它被包裝在一個類中,如下所示)。這一個似乎正常工作:我得到了didReceiveData中的所有數據,並得到了完成調用connectionDidFinishLoading。在connectionDidFinishLoading的結尾處,我實例化了一個或多個稍微不同類型的包裝類,但它們本質上是相同的。問題是第二個NSURLConnection的委託永遠不會調用它的方法。第二個異步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");
}
...
ConnectionWrapper:
-(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");
}
DifferentConnectionWrapper:
- (ID)initWithParams:(ID)d網址:(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)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具有類似的功能,但爲簡潔起見,我省略了其他邏輯。
感謝您的幫助。我很感激。
我試過了,但問題依然存在。 – Seth 2011-04-20 02:18:11
@查看關於您的初始方法的最後編輯。 – 2011-04-20 02:26:33
沒有保持對NSURLConnection的引用沒有錯。它將被runloop保留。 – 2011-04-20 02:52:59