AvenueFetcher 0.4.0

AvenueFetcher 0.4.0

测试已测试
语言语言 Obj-CObjective C
许可证 定制
发布最后发布2015年10月

Dustin Bachrach维护。



 
依赖
Avenue~> 0.4
JSONModel~> 1.1
PromiseKit/CorePromise~> 3.0
 

  • Dustin Bachrach

AvenueFetcher 是一个基于 Avenue 的简单组件。它提供了一个抽象类 AVEFetcher,使得从网络请求中获取 JSONModel 模型对象变得简单。

安装

设置

简单继承 AVEFetcher 并实现 +sharedFetcher

@interface MYFetcher : AVEFetcher

@end
@implementation MYFetcher

+ (instancetype)sharedFetcher
{
    static id sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

你还需要配置 fetcher 的 builder。Builder 是 AVEHTTPRequestOperationBuilder 的一个实例,它描述了所有 fetcher 请求的详细信息,如 baseURL、请求/响应序列化、安全策略等。

通常,你会在 -init 方法中配置 builder

- (instancetype)init
{
    if (self = [super init]) {
        NSURL* baseURL = [NSURL URLWithString:@"https://myurl.com"];
        AVEHTTPRequestOperationBuilder* builder = [[AVEHTTPRequestOperationBuilder alloc] initWithBaseURL:baseURL];

        self.builder = builder;
    }
    return self;
}

使用 Fetcher

使用 fetcher 发起请求很简单

[[MYFetcher sharedFetcher] fetchModel:MYModel.class
                                 path:@"model/1"
                              keyPath:nil
                           parameters:nil
                             priority:[AVENetworkPriority priorityWithLevel:AVENetworkPriorityLevelHigh]
                         networkToken:nil].then(^(MYModel* model) {
    // Use the `model`.
});

你也可以发起 POSTPUT 请求,并将返回的响应视为模型。

[[MYFetcher sharedFetcher] postAndFetchModel:MYModel.class
                                        path:@"model/1"
                                     keyPath:nil
                                  parameters:parameters].then(^(MYModel* model) {
    // Use the `model`.
});

[[MYFetcher sharedFetcher] putAndFetchModel:MYModel.class
                                       path:@"model/1"
                                    keyPath:nil
                                 parameters:parameters].then(^(MYModel* model) {
    // Use the `model`.
});

作者

MediaHound

许可证

Avenue 在 Apache License 2.0 许可下可用。有关更多信息,请参阅 LICENSE 文件。