2009-08-04 63 views
1

我想進行兩次單獨的異步Internet查找。然而,他們正在彼此踩踏,我不知道爲什麼。無意中調用了兩次相同的obj-C對象

當我四處挖掘時,看起來兩個查找都在同一個對象上運行(症狀:第二個調用正在重置「thisPage」由第一個調用設置)。

也許這與委託使用(這方面還不舒服)有關?


我創建了一個類CSNET和我初始化:

CSNet    *startupPage; 
CSNet    *positionUpdatePage; 
startupPage = [[CSNet alloc] init]; 
positionUpdatePage = [[CSNet alloc] init]; 

後來,我嘗試使用的每個對象:

[startupPage getPage:self page:pageName]; 

和:

[positionUpdatePage getPage:self page:pageName]; 

模塊級變量:

enum pageType {nothing, startup, positionUpdate, acquireOpponents, fire, changeCredentials}; 
enum pageType thisPage; 

GETPAGE電話:

-(void)getPage:(id)delegate page:(NSString *)page { 
m_Delegate = delegate; 
if ([[page substringToIndex:2] isEqual:@"st"]) { 
     thisPage = startup; 
} else { 
    if ([[page substringToIndex:2] isEqual:@"pu"]) { 
     thisPage = positionUpdate; 
    } else { 
     if ([[page substringToIndex:2] isEqual:@"ao"]) { 
      thisPage = acquireOpponents; 
     } else { 
      if ([[page substringToIndex:2] isEqual:@"fi"]) { 
       thisPage = fire; 
      } else { 
       if ([[page substringToIndex:2] isEqual:@"cc"]) { 
        thisPage = changeCredentials; 
       } 
      } 
     } 
    } 
} 

NSURLRequest *startupRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[ 
    [NSString stringWithFormat: @"%@%@", SERVER, page] 
    stringByReplacingOccurrencesOfString:@" " withString:@"%20"]]]; 
startupConnection = [[NSURLConnection alloc] initWithRequest:startupRequest delegate:self]; 

if(startupConnection) 
{ 
    workInProgress = YES; 
    m_RequestData = [[NSMutableData data] retain]; 
} 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 

// this method is called when the server has determined that it 
// has enough information to create the NSURLResponse 
// it can be called multiple times, for example in the case of a 
// redirect, so each time we reset the data. 
// receivedData is declared as a method instance elsewhere 
[m_RequestData setLength:0]; 

} 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
// append the new data to the receivedData 
// receivedData is declared as a method instance elsewhere 
[m_RequestData appendData:data];  
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
// release the data object 
[m_RequestData release]; 

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

返回回委託:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
if(workInProgress == YES) { 
    workInProgress = NO; 
    switch (thisPage) { 
     case startup: 
      //if ([m_Delegate respondsToSelector:@selector(startupReady:)]) 
       [m_Delegate startupReady:m_RequestData]; 
      break; 
     case positionUpdate: 
      //if ([m_Delegate respondsToSelector:@selector(positionUpdateReady:)]) 
       [m_Delegate positionUpdateReady:m_RequestData]; 
      break; 
     case acquireOpponents: 
      //if ([m_Delegate respondsToSelector:@selector(opponentsReady:)]) 
       [m_Delegate opponentsReady:m_RequestData]; 
      break; 
     case fire: 
      //if ([m_Delegate respondsToSelector:@selector(fireReady:)]) 
       [m_Delegate fireReady:m_RequestData]; 
      break; 
     case changeCredentials: 
      //if ([m_Delegate respondsToSelector:@selector(changeCredentialsReady:)]) 
       [m_Delegate changeCredentialsReady:m_RequestData]; 
      break; 
     default: 
      break; 
    } 
    [m_RequestData release]; 
} 
} 
+0

變量在你的委託對象中嗎? – Daniel 2009-08-04 16:01:14

+1

這不是真正足夠的代碼來查看究竟發生了什麼。哪些變量被覆蓋?你可以發佈整個CSNet類和你正在調用這個類並從更詳細的解釋嗎? – 2009-08-04 16:04:07

+0

這兩個班都非常大,所以我儘量不要無視細節。我會編輯以嘗試包含適量的細節。 – BankStrong 2009-08-04 16:10:04

回答

4

你說thisPage是一個模塊級別的變量。我假設這意味着它不是一個實例變量。如果是這種情況,則在CSNet的所有實例之間只有一個thisPage變量共享。當然,如果你有兩個同時跑步,他們會互相打架。

如果您確實想要CSNet的每個實例都有自己的thisPage變量,請將其作爲實例變量。

2

您可以跟蹤哪些對象發送方法來委託的,並在明顯處理兩個連接相同的委託對象。如果您查看代理方法(如-connection:didReceiveResponse:)的方法簽名,則第一個參數是發送消息的連接。在你的情況下,這將是通過調用-getPage:page:兩次創建的兩個對象之一。因此,請跟蹤這些對象,並測試哪一個會導致調用委託方法。

相關問題