What will be if I call simultaneously [[RKClient s

2019-09-19 08:03发布

What will be if I calls simultaneously [[RKClient sharedClient] get@"foo.xml" delegate:self] in two UIViewControllers? Do I have any problems?

viewController_A
{
[[RKClient sharedClient] get:@"foo.xml" delegate:self];
}

viewController_B
{
 [[RKClient sharedClient] get:@"foo.xml" delegate:self];
}

2条回答
狗以群分
2楼-- · 2019-09-19 08:10

I'm guessing that the two different calls would be on two different threads. With this being said no problems should occur, the system should be able to sort out the process, however I am not sure would return 'first'.

So in conclusion as long as the object at [RKClient sharedClient] is thread safe (which most objects seem to be unless stated otherwise) no problems should be found

查看更多
劳资没心,怎么记你
3楼-- · 2019-09-19 08:21

If you have a look at the RKClient implementation for get:delegate: it simply does this

- (RKRequest *)get:(NSString *)resourcePath delegate:(id)delegate {
    return [self load:resourcePath method:RKRequestMethodGET params:nil delegate:delegate];
}

and the implementation for load:method:params:delegate: is

- (RKRequest *)load:(NSString *)resourcePath method:(RKRequestMethod)method params:(NSObject<RKRequestSerializable> *)params delegate:(id)delegate {
    NSURL* resourcePathURL = nil;
    if (method == RKRequestMethodGET) {
        resourcePathURL = [self URLForResourcePath:resourcePath queryParams:(NSDictionary*)params];
    } else {
        resourcePathURL = [self URLForResourcePath:resourcePath];
    }
    RKRequest *request = [[RKRequest alloc] initWithURL:resourcePathURL delegate:delegate];
    [self setupRequest:request];
    [request autorelease];
    request.method = method;
    if (method != RKRequestMethodGET) {
        request.params = params;
    }

    [request send];

    return request;
}

It's not using any RKClient state / shared data so you won't see a problem. The method get:delegate: is asynchronous itself so this stuff will happen in the background anyway.

查看更多
登录 后发表回答