我有一个NSURLConnection从JSON Web服务获取数据,一切正常。我正在使用它向服务器发布内容并获得成功响应。
在调用之后我想启动另一个NSURLConnection来刷新数据,所以我在connectionDidFinishLoading方法中这样做,但是这个第二个连接在加载完成后没有调用connectionDidFinishLoading。
我不能从connectionDidFinishLoading方法中启动NSURLConnection吗?
编辑:下面是代码。我将NSURLConnection子类化为包含Tag NSString,调用新类NSURLConnectionHelper。我用它来区分哪个连接调用了connectionDidFinishLoading。
- (void)connectionDidFinishLoading:(NSURLConnectionHelper *)connection
{
if([connection.Tag isEqual:@"NewMessage"]){
NSString *jsonString = [[NSString alloc] initWithData:receivedNewMessageData encoding:NSASCIIStringEncoding];
NSDictionary *results = [jsonString JSONValue];
[jsonString release];
[connection release];
if ([[results objectForKey:@"MessageAdded"] isEqual:@"True"]) {
User *newUser = [[User alloc] init];
[newUser retrieveFromUserDefaults];
if([newUser IsLoggedIn]){
Message *message = (Message *)[messages objectAtIndex: 0];
NSString *urlAsString = // url for webservice goes here
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnectionHelper *connection1 = [[NSURLConnectionHelper alloc] initWithRequest:request delegate:self];
connection1.Tag = @"GetLatestMessages";
[request release];
if (connection1) {
receivedLatestMessagesData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}
}
}
}else if([connection.Tag isEqual:@"GetLatestMessages"]){
//do some other stuff but this code is never reached
}
}
答案 0 :(得分:0)
我不熟悉NSURLConnectionHelper
,但看起来你永远不会开始连接。
答案 1 :(得分:0)
我最终在我的网络服务网址中留了一个空格,一旦我纠正它有效。