2017.3.8

继昨天的坑后又出现一个小坑,封装的方法没有回调。

昨天的坑是认证后的用户再调登录接口时,token就不再返回,会返回一个objectID,用这个objectID调用删除认证的接口,先删除再登录。

但是发现删除成功后并没有回调,这就尴尬了。从官方文档上看,删除接口的接口返回值是空。

DELETE /authorizations/:id
Status: 204 No Content

所以AFNetworking调用接口后,返回的responseObject为nil。然后一级一级点开OctoKit源码看了一下,原来当responseObject为nil时,OctoKit没有做任何处理,导致没有回调。所以改了一下源码(本来我的原则是不该远吗,先看看通过扩展的方式能不能实现效果的,可是为了速度,先改源码吧)。

这回终于能够往下进行了。

- (RACSignal *)parsedResponseOfClass:(Class)resultClass fromJSON:(id)responseObject {
    NSParameterAssert(resultClass == nil || [resultClass isSubclassOfClass:MTLModel.class]);

    return [RACSignal createSignal:^ id (id subscriber) {
        void (^parseJSONDictionary)(NSDictionary *) = ^(NSDictionary *JSONDictionary) {
            if (resultClass == nil) {
                [subscriber sendNext:JSONDictionary];
                return;
            }

            NSError *error = nil;
            OCTObject *parsedObject = [MTLJSONAdapter modelOfClass:resultClass fromJSONDictionary:JSONDictionary error:&error];
            if (parsedObject == nil) {
                // Don't treat "no class found" errors as real parsing failures.
                // In theory, this makes parsing code forward-compatible with
                // API additions.
                if (![error.domain isEqual:MTLJSONAdapterErrorDomain] || error.code != MTLJSONAdapterErrorNoClassFound) {
                    [subscriber sendError:error];
                }

                return;
            }

            NSAssert([parsedObject isKindOfClass:OCTObject.class], @"Parsed model object is not an OCTObject: %@", parsedObject);

            // Record the server that this object has come from.
            parsedObject.baseURL = self.baseURL;
            [subscriber sendNext:parsedObject];
        };

        if ([responseObject isKindOfClass:NSArray.class]) {
            for (NSDictionary *JSONDictionary in responseObject) {
                if (![JSONDictionary isKindOfClass:NSDictionary.class]) {
                    NSString *failureReason = [NSString stringWithFormat:NSLocalizedString(@"Invalid JSON array element: %@", @""), JSONDictionary];
                    [subscriber sendError:[self parsingErrorWithFailureReason:failureReason]];
                    return nil;
                }

                parseJSONDictionary(JSONDictionary);
            }

            [subscriber sendCompleted];
        } else if ([responseObject isKindOfClass:NSDictionary.class]) {
            parseJSONDictionary(responseObject);
            [subscriber sendCompleted];
        } else if (responseObject != nil) {
            NSString *failureReason = [NSString stringWithFormat:NSLocalizedString(@"Response wasn't an array or dictionary (%@): %@", @""), [responseObject class], responseObject];
            [subscriber sendError:[self parsingErrorWithFailureReason:failureReason]];
        } else {
            parseJSONDictionary(nil);
            [subscriber sendCompleted];
        }

        return nil;
    }];
}

你可能感兴趣的:(2017.3.8)