2011-04-20 103 views
0

從我的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具有類似的功能,但爲簡潔起見,我省略了其他邏輯。

感謝您的幫助。我很感激。

回答

0

確定。事實證明,這個錯誤是由我錯過的東西造成的。在第二次請求之後,我正在進入一個艱難的環境,這可能會造成任何事情。一旦我解決了這個問題,一切都很順利。

感謝您的幫助。

1

我相信問題是,你在釋放的-beginFetch URL連接:

-(void)beginFetch 
{ 
    NSURLRequest* request = [[NSURLRequest alloc] initWithURL:connURL]; 
    NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

    [conn release]; 
    [request release]; 
} 

的URL連接對象應保持活力和釋放時的連接完成加載:

- (void) connectionDidFinishLoading: (NSURLConnection*) connection 
{ 
    ... parsing .... 

    // *** Release the connection and whatever data you’ve kept related to 
    // this particular connection 
    [connection release]; 
    [responseData release]; 
    // *** or [responseData setLenght:0]; depending on how you’re 
    // managing responseData 

    DifferentConnectionWrapper* w = [[DifferentConnectionWrapper alloc] initWithParams:self 
     url:[NSURL URLWithString:<different url>]]; 
    [w beginFetch]; 
} 

或出現錯誤時:

- (void)connection:(NSURLConnection *)connection 
    didFailWithError:(NSError *)error 
{ 
    // *** Release the connection and whatever data you’ve kept related to 
    // this particular connection 
    [connection release]; 
    [responseData release]; 
    // *** or [responseData setLenght:0]; depending on how you’re 
    // managing responseData 

    // inform the user 
    NSLog(@"Connection failed! Error - %@ %@", 
      [error localizedDescription], 
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]); 
} 

編輯:你的初始化器是怪異一點點:

-(id)initWithParams:(id<ConnectionWrapperDelegate>)d url:(NSURL*)url 
{ 
    delegate = d;  
    connURL = url; 
    return [self init]; 
} 

有沒有辦法知道會發生什麼,除非我們看到代碼爲-init和,無論如何,這應該是指定的初始化器,所以它無論如何不應該發送-initself。此外,您應該保留傳遞給初始程序的url對象。

下更有意義:

-(id)initWithParams:(id<ConnectionWrapperDelegate>)d url:(NSURL*)url 
{ 
    self = [super init]; 
    if (self) { 
     delegate = d;  
     connURL = [url retain]; 
    } 
    return self; 
} 

不要忘了釋放-deallocurl對象或當你指定其它值connURL

+0

我試過了,但問題依然存在。 – Seth 2011-04-20 02:18:11

+0

@查看關於您的初始方法的最後編輯。 – 2011-04-20 02:26:33

+0

沒有保持對NSURLConnection的引用沒有錯。它將被runloop保留。 – 2011-04-20 02:52:59

1

一些事情:我沒有看到didFailWithError:在你的第一個包裝類中,並且(有點偏離主題)是你用你的DifferentConnectionWrapper * w泄漏了內存嗎?

無論如何,我會嘗試的是:看看你是否可以直接從appDelegate而不是ConnectionWrapper調用DifferentConnectionWrapper。 而且我會嘗試在任何情況下解耦這兩個呼叫。當第一個完成並調用appDelegate時,你不能從那裏啓動你的DifferentConnectionWrapper嗎?

我知道這並不能說明你的問題,但你可能會得到它的工作(以及其中這兩件事情是更重要的,是一個完全不同的討論。)

+0

看起來他正在泄漏他的連接包裝。 – 2011-04-20 02:07:15

+0

DifferentConnectionWrapper可以從App Delegate自行完成,但是一旦我鏈接了兩個連接請求,第二個連接就無處可去。我知道泄漏:這只是我爲調試做的事情(我想消除任何保留/發佈問題)。 – Seth 2011-04-20 02:14:36

相關問題