URLTransaction 1.0

URLTransaction 1.0

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布Nov 2015

DanKalinin 维护。




  • DanKalinin

描述

用法

要运行示例项目,请克隆仓库,然后首先从 Example 目录中运行 pod install

URLRequest

URLTransaction

@interface Department : NSObject

@property NSNumber *deptID;
@property NSString *name;
@property NSSet *employees;

@end
@interface Employee : NSObject

@property NSNumber *empID;
@property NSString *name;
@property NSDate *birthDay;

@end
@interface URLRequest (Company)

+ (instancetype)getDepartment:(NSNumber *)deptID;
+ (instancetype)getEmployee:(NSNumber *)empID;

- (Department *)mapDepartment;
- (Employee *)mapEmployee;

@end

@implementation URLRequest (Company)

#pragma mark - Build requests

+ (instancetype)getDepartment:(NSNumber *)deptID {

    NSURLComponents *components = [NSURLComponents new];
    components.scheme = @"http";
    components.host = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Host"];       // Make sense to add your host to Info.plist
    components.path = [NSString stringWithFormat:@"/department/%i", deptID.intValue];

    URLRequest *request = [URLRequest requestWithURL:components.URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    return request;
}

+ (instancetype)getEmployee:(NSNumber *)empID {

    NSURLComponents *components = [NSURLComponents new];
    components.scheme = @"http";
    components.host = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"Host"];
    components.path = [NSString stringWithFormat:@"/employee/%i", empID.intValue];

    URLRequest *request = [URLRequest requestWithURL:components.URL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    return request;
}

#pragma mark - Map responses

- (Department *)mapDepartment {
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:nil];
    if (json) {
        Department *department = [Department new];
        department.deptID = json[@"id"];
        department.name = json[@"name"];
        department.employees = json[@"employees"];
        return department;
    }
    return nil;
}

- (Employee *)mapEmployee {
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:self.data options:0 error:nil];
    if (json) {
        Employee *employee = [Employee new];
        employee.empID = json[@"id"];
        employee.name = json[@"name"];
        employee.birthDay = [NSDate dateWithTimeIntervalSince1970:[json[@"dirthDay"] doubleValue]];
        return employee;
    }
    return nil;
}

@end
[[URLRequest getDepartment:@1] sendWithSuccess:^(URLRequest *request) {     // TRY:
    Department *department = [request mapDepartment];                       // Map response
                                                                            // Perform next request
    NSMutableSet *employees = [NSMutableSet set];

    for (NSNumber *empID in department.employees) {
        [[URLRequest getEmployee:empID] addToTransaction:@10 success:^(URLRequest *request) {   // Add to transaction with ID
            Employee *employee = [request mapEmployee];
            [employees addObject:employee];
        } failure:nil completion:nil];
    }

    [[URLTransaction transaction:@10] sendWithSuccess:^(URLTransaction *transaction) {          // Get transaction by ID & perform
        department.employees = employees;
    } failure:^(URLTransaction *transaction) {
        NSLog(@"Transaction failed - %@", transaction.error);
    } completion:^(URLTransaction *transaction) {
        // Hide activity indicator
    } queue:dispatch_get_main_queue()];

} failure:^(URLRequest *request) {                                          // CATCH:
    NSLog(@"Request failed - %@", request.error);                           // Process error

} completion:^(URLRequest *request) {                                       // FINALLY:
    // Hide activity indicator                                              // Perform cleanup

} queue:nil];

需求

安装

URLTransaction 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile

pod "URLTransaction"

作者

DanKalinin, [email protected]

许可

URLTransaction 可在 MIT 许可下使用。有关更多信息,请参阅 LICENSE 文件。