我想進行兩次單獨的異步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];
}
}
變量在你的委託對象中嗎? – Daniel 2009-08-04 16:01:14
這不是真正足夠的代碼來查看究竟發生了什麼。哪些變量被覆蓋?你可以發佈整個CSNet類和你正在調用這個類並從更詳細的解釋嗎? – 2009-08-04 16:04:07
這兩個班都非常大,所以我儘量不要無視細節。我會編輯以嘗試包含適量的細節。 – BankStrong 2009-08-04 16:10:04