创建一个外部 Web 服务并在 ViewdidLoad 中调用它



首先,请原谅我的英语不好,但我是法国人,我会尽力理解。

因此,我正在使用以下结构编写一个简单的应用程序:- 视图控制器类(处理UI)- 产品类别(定义对象产品)- ws_product类(包含一些获取json数据的函数)

我正在尝试做的是返回产品数组,这是我在 viewController 中解析了 ws_product 中的 json 后得到的。多亏了这一点,我可以填满我的表格视图,我的应用程序将不再为空!

我的实际ws_product是:

#import "WS_Produit.h"
#import "Produit.h"
#import "ViewController.h"
@implementation WS_Produit
- (NSMutableArray *)getProduitsJSON
{
    __block NSMutableArray *result;
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^() {
        NSLog(@"on passe en async");
        NSError *error         = nil;
        NSData  *jsonData      = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"the url to load"]];
        NSDictionary *produits = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];

        if( error )
        {
            NSLog(@"%@", [error localizedDescription]);
        }
        else {
            dispatch_sync(dispatch_get_main_queue(), ^(){
                NSLog(@"retour en sync");
                result = [[NSMutableArray alloc] init];
                Produit *tmp;
                NSArray *produit = produits[@"produits"];
                for ( NSDictionary *property in produit )
                {
                    tmp             = [Produit new];
                    tmp.ref         = property[@"ref"];
                    tmp.name        = property[@"name"];
                    tmp.description = property[@"description"];
                    tmp.price       = property[@"price"];
                    tmp.imgURL      = property[@"imgURL"];
                    [result addObject:tmp];
                    NSLog(@"%@", result);
                }
            });
        }
    });
    NSLog(@"sortie du block");
    NSLog(@"%@", result);
    return result;
}
@end

我的问题是当我不在dispatch_queue时,我的结果数组是空的,所以在我的 viewController 类中返回它是没有用的,我该怎么办?

因为您使用的是 dispatch_async,所以您的结果数组在填充之前将返回为空。

块正是您所需要的。它们可以用作异步方法的回调。

在视图控制器中,您应该将块传递给您的方法

[myObject getProduitsJSON:
                   success:^(NSArray *results){
                       // Use your results here
                       // Reload table for example.
                   }
                   failure:^(NSError *error){
                       // Use your error message (show it for example)
                   }];

所以你的方法应该看起来像这样:

-(void)getProduitsJson:(void(^)(NSArray* results))success failure:(void(^)(NSError* error))failure {
{
    NSMutableArray *result = [[NSMutableArray alloc] init];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^() {
         NSError *error         = nil;
         NSData  *jsonData      = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"the url to load"]];
         NSDictionary *produits = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
         if(error) {
             failure(error);
         }else{
             // Fill your array
             success(result);
         }
    }
}

最新更新