gpt4 book ai didi

iphone - 充当 NSUrlConnection 委托(delegate)的 NSObject 实例似乎不是孤立的

转载 作者:行者123 更新时间:2023-11-29 11:19:25 25 4
gpt4 key购买 nike

第一次发帖,希望写的够详细

在开发 Iphone 应用程序时,我遇到了一些奇怪的行为。我的“WebserviceConnection”类的某个实例的成员变量似乎获得了我分配给同一类的另一个实例的值。

举例说明:这是我日志的摘录。我假设 0x000000 是一个实例 ID。第四个响应应该是“<-: 1”。

2011-11-03 16:25:13.227 Dashboard[540:707] ->: 1, <WebserviceConnection: 0x11f950>
2011-11-03 16:25:13.256 Dashboard[540:707] ->: 0, <WebserviceConnection: 0x323db0>
2011-11-03 16:25:15.318 Dashboard[540:707] <-: 0, <WebserviceConnection: 0x323db0>
2011-11-03 16:25:15.325 Dashboard[540:707] <-: 0, <WebserviceConnection: 0x11f950>

该类是一个 NSUrlConnection 委托(delegate),它在两个连接同时打开时表现出这种行为。

这个类:WebserviceConnection.h

(ConnectionType 是一个枚举)

#import "WebserviceConnection.h"
#import "WebserviceUtility.h"

@implementation WebserviceConnection

BOOL isCanceled;
NSDictionary *result;
ConnectionType connectionType;
id <WebserviceConnectionDelegate> delegate;

- (id)initWithDelegate:(id)webServiceDelegate connectionType:(ConnectionType) type {
delegate = webServiceDelegate;
connectionType = type;
isCanceled = NO;
NSLog(@"->: %i, %@", connectionType, self);
return self;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

switch (connectionType) {
case GetAllAlerts:
result = [WebserviceUtility getJsonFromData:data];
break;
case GetServerAlerts:
result = [WebserviceUtility getJsonFromData:data];
break;
case GetServers:
result = [WebserviceUtility getJsonFromData:data];
break;
default:
result = nil;
break;
}
}

- (void)displayErrorAlert {
UIAlertView *errorMessage = [[UIAlertView alloc] initWithTitle:@"Fout" message:@"Verbinding met webservice niet mogelijk" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[errorMessage show];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if(!isCanceled) {
@try {
[delegate connection:connection ofType:connectionType didFinishWithError: [NSDictionary dictionaryWithObject:@"error" forKey:@"WebserverConnectionFailed"]];
}
@catch (NSException *e) {}
@finally {}
[self displayErrorAlert];
}
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"<-: %i, %@", connectionType, self);
if(!isCanceled) {
[delegate connection:connection ofType:connectionType didFinishWithResult:result];
}
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSURLCredential *credential = [WebserviceUtility getCredentials];
if ([challenge previousFailureCount] == 0) {
[[challenge sender] useCredential:credential
forAuthenticationChallenge:challenge];
}
else {
[delegate connection:connection ofType:connectionType didFinishWithError: [NSDictionary dictionaryWithObject:@"error" forKey:@"WebserverConnectionFailed"]];
[self displayErrorAlert];

}
}

- (void)delegateDidDealloc {
NSLog(@"!!: %i, %@", connectionType, self);
isCanceled = YES;
}

@end

这样使用:

- (void) getAllAlerts {
NSURLRequest *request = [WebserviceUtility getRequestForPath:@"/dashboard/DashboardAppleConnector.asmx/GetActiveAlerts"];
webserviceConnection = [[WebserviceConnection alloc] initWithDelegate:self connectionType:GetAllAlerts];
connection = [[NSURLConnection alloc] initWithRequest:request delegate: webserviceConnection];
}

当另一个拥有自己的 webserviceConnection 实例的 ViewController 使用它的实例(类似于 getAllAlerts)时,一切都变成了梨形!

有什么想法吗?

问候,伯特

最佳答案

看起来问题的发生是因为您声明变量的方式,例如 connectionType。如果您希望将它们声明为实例变量,则应将它们放在接口(interface)声明中:

@interface WebServiceConnection {
BOOL isCanceled;
NSDictionary *result;
ConnectionType connectionType;
id <WebserviceConnectionDelegate> delegate;
}
@end

通过在@implementation block 中声明它们,您实际上是在创建全局变量,而不是实例变量。

参见 this SO post了解更多信息

关于iphone - 充当 NSUrlConnection 委托(delegate)的 NSObject 实例似乎不是孤立的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7997885/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com