1

我正在使用 AFNetworking 和 AFHTTPRequestOperation 从 Web 服务中提取 XML 数据。这工作正常,我得到了我需要的数据,但我需要将这些数据拆分为对象并用这些数据初始化一个 NSMutableArray。这在完成块中工作,但就在我在我的方法中返回数组之前,数据消失了吗?我该怎么做呢?

这是我的一些代码:

NSMutableArray *result = [[NSMutableArray alloc] init];    
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString* response = [operation responseString];
    NSData* xmlData = [response dataUsingEncoding:NSUTF8StringEncoding];
    NSError *xmlError;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&xmlError];
    NSArray *allElements = [doc.rootElement elementsForName:@"Misc"];
    for (GDataXMLElement *current in allElements)
    {
        NSString *titel;
        NSString *tekst;

        NSArray *titels = [current elementsForName:@"Titel"];
        if(titels.count > 0)
        {
            GDataXMLElement *firstTitel = (GDataXMLElement *) [titels objectAtIndex:0];
            titel = firstTitel.stringValue;
        } else continue;

        NSArray *teksts = [current elementsForName:@"Tekst"];
        if(teksts.count > 0)
        {
            GDataXMLElement *firstTekst = (GDataXMLElement *) [teksts objectAtIndex:0];
            tekst = firstTekst.stringValue;
        } else continue;

        HVMGUniversalItem *item = [[HVMGUniversalItem alloc] initWithTitel:titel AndTekst:tekst];
        [result addObject:item];
    }
    NSLog(@"%i", result.count);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", [operation error]);
}];

[operation start];
NSLog(@"%i", result.count);
return result;

我究竟做错了什么?为什么返回时数组中没有数据?

4

1 回答 1

1

为什么返回时数组中没有数据?

因为AFNetworking使用异步模式。所以在操作完成之前会执行返回码。

您需要使用不同的方法或遵循AFNetworking 可以同步返回数据(在块内)吗?. 后者是不鼓励的。

一个解决方案可能是:

-> 在您的课程中创建一个NSOperationQueue包含您的操作的课程。将其创建为您的类的属性,例如。

@property (nonatomic, strong, readonly) NSOperationQueue* downloadQueue;

- (NSOperationQueue*)downloadQueue
{
    if(downloadQueue) return downloadQueue;

    downloadQueue = // alloc init here
}

-> 为你的数组创建一个属性(也合成它)

@property (nonatomic, strong) NSMutableArray* result;

-> 将您的代码包装在特定方法中,例如doOperation.

self.result = [[NSMutableArray alloc] init];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[httpClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];

__weak YourClass* selfBlock = self; 

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString* response = [operation responseString];
    NSData* xmlData = [response dataUsingEncoding:NSUTF8StringEncoding];
    NSError *xmlError;
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&xmlError];
    NSArray *allElements = [doc.rootElement elementsForName:@"Misc"];
    for (GDataXMLElement *current in allElements)
    {
        NSString *titel;
        NSString *tekst;

        NSArray *titels = [current elementsForName:@"Titel"];
        if(titels.count > 0)
        {
            GDataXMLElement *firstTitel = (GDataXMLElement *) [titels objectAtIndex:0];
            titel = firstTitel.stringValue;
        } else continue;

        NSArray *teksts = [current elementsForName:@"Tekst"];
        if(teksts.count > 0)
        {
            GDataXMLElement *firstTekst = (GDataXMLElement *) [teksts objectAtIndex:0];
            tekst = firstTekst.stringValue;
        } else continue;

        HVMGUniversalItem *item = [[HVMGUniversalItem alloc] initWithTitel:titel AndTekst:tekst];
        [selfBlock.result addObject:item];
    }
    NSLog(@"%i", result.count);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", [operation error]);
}];

[downloadQueue addOperation:operation];

-> 如果您需要通知result有对象发送通知,请使用委托模式等...

希望有帮助。

于 2012-07-08T15:28:43.317 回答