【问题标题】:NSURLConnection for multiple views not receiving data asynchronously多个视图的 NSURLConnection 不异步接收数据
【发布时间】:2011-12-20 13:12:56
【问题描述】:

在我的 iPAD 应用程序中,我有 6 个 UITableView。为了获取每个 tableview 的数据,我使用 NSURLConnection 调用 Webservice 并解析从 Webservice 返回的 xml 并将数据存储到数据库中。

由于我有 6 个 UITableView,我同时为每个视图发送 Webservice 请求。但是,我面临的问题是,对于我的应用程序要从 -(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 上的 Web 服务接收 1 个表视图的数据,这取决于其他表视图的解析器执行的数据库操作。

比如tableview的A,B,C,D的webservice请求都是同时发送的。如果我在 -(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 函数上取回数据,直到接收到的 xml 被解析并保存到我的数据库中,我不会得到其他表视图的响应。

我无法弄清楚我做错了什么。我知道 NSURLConnection 是异步的,但我得到的响应似乎并非如此。

这是我的代码 -

用于发送 Webservice 请求 -

- (void) callMedicationWebService
{
    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn) 
    {
        webData = [[NSMutableData data] retain];
    }
}

-(void) connection:(NSURLConnection *) connection 
didReceiveResponse:(NSURLResponse *) response 
{
    [webData setLength: 0];
}


-(void) connection:(NSURLConnection *) connection 
    didReceiveData:(NSData *) data 
{
    [webData appendData:data];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSString *alertMessage = [formatter stringFromDate:[NSDate date]];
    [formatter release];

    NSLog(@"got data back from WS %@", alertMessage);
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{
    [connection release];

    // Parse xml
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:[CommonHelper decodeHTMLCharactorsFromString:webData]];

    TableAHandler *handler = [[TableAHandler alloc] init];
    [handler initTableAHandler];
    [xmlParser setDelegate:handler];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser setShouldProcessNamespaces:YES];

    BOOL success = [xmlParser parse];
   }

有人能帮我解决我做错了什么吗?

【问题讨论】:

  • 效果如何?我也有同样的问题!!!

标签: ios xml-parsing nsurlconnection


【解决方案1】:

异步并不一定意味着回调函数本身是在单独的线程中调用的。

如果您希望所有解析过程同时发生,您将不得不将解析过程移动到单独的线程。

虽然更好的解决方案是不使用 5 个不同的 URLRequest,而只使用一个返回所有必需信息的 URLRequest。

【讨论】:

    猜你喜欢
    • 2012-11-25
    • 1970-01-01
    • 2010-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多