在 Objective C 中的委托方法上设置超时

发布于 2024-11-27 17:40:55 字数 1062 浏览 2 评论 0原文

我需要设置一个 NSTimer 对象,以便在服务器调用时间超过 10 秒时手动超时(Restkit 不支持)

这是我的代码如下。本质上,我的加载器类将使用 loadObjectsAtResourcePath 委托请求

,如果需要超过 10 秒,我想调用 Restkit 在服务器遇到错误时调用的相同失败方法 (didFailWithError)

但我觉得我正在这样做错误,而且失败方法需要一个仅在委托类中初始化的对象。

//CLASS FOR LOADING OBJECTS

-(void)getObjects{
    RKObjectManager *sharedManager = [RKObjectManager sharedManager];

    // loads the object via delegate
    [sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];

    //creates an error
    NSError *error = [NSError errorWithDomain:@"world" code:200 userInfo:nil];

    // Setting timeout here. goto failure 
    nTimer = [NSTimer scheduledTimerWithTimeInterval:10.0f target:self.delegate selector:@selector:(objectLoader:nil didFailWithError:error:) userInfo:nil repeats:NO];
}


// handles failure
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
..
}

// handles success
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
..
}

这样做的正确方法是什么?

i need to set a NSTimer object to manually timeout a server call if it is taking more than 10 seconds (not supported in Restkit)

This is my code below. Essentially, my loader class will delegate the request with loadObjectsAtResourcePath

If it takes more than 10 seconds, I would like to call the same failure method that Restkit calls when it hits an error with the server (didFailWithError)

But i feel that i am doing it wrong, and furthermore, the failure method requires a object which is only initialized in the delegate class.

//CLASS FOR LOADING OBJECTS

-(void)getObjects{
    RKObjectManager *sharedManager = [RKObjectManager sharedManager];

    // loads the object via delegate
    [sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];

    //creates an error
    NSError *error = [NSError errorWithDomain:@"world" code:200 userInfo:nil];

    // Setting timeout here. goto failure 
    nTimer = [NSTimer scheduledTimerWithTimeInterval:10.0f target:self.delegate selector:@selector:(objectLoader:nil didFailWithError:error:) userInfo:nil repeats:NO];
}


// handles failure
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
..
}

// handles success
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
..
}

What is the right way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

场罚期间 2024-12-04 17:40:55

最好在委托回调以外的方法中处理超时。正如您所说,委托方法需要在类中创建的对象。您可能不想以完全相同的方式处理“真正的错误”和超时,对吗?例如,如果超时,您可能希望选择重试。

如果您确实希望超时和错误失败做完全相同的事情,您仍然可以使用另一种方法:

-(void)getObjects{
    RKObjectManager *sharedManager = [RKObjectManager sharedManager];

    // loads the object via delegate
    [sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];

    //creates an error
    NSError *error = [NSError errorWithDomain:@"world" code:200 userInfo:nil];

    // Setting timeout here. goto failure 
    nTimer = [NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(didTimeout) userInfo:nil repeats:NO];
}


- (void)didTimeout {
    NSLog(@"Error");
}

// handles failure
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
    [self didTimeout];
}

// handles success
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
    //don't forget to invalidate the time or else you'll get errors even when successful
    [nTimer invalidate];
}

如果需要,您当然可以扩展它以使其更加灵活,但这似乎涵盖了您所要求的内容。

It would be better to handle the timeout in a method other than the delegate callback. As you say, the delegate method requires objects that are created within the class. And you probably don't want to handle a "real error" and a timeout in exactly the same way, right? With a timeout you might, for example, want the option of trying again.

If you did want the timeout and failing with an error to do exactly the same thing, you can stil use another method for this:

-(void)getObjects{
    RKObjectManager *sharedManager = [RKObjectManager sharedManager];

    // loads the object via delegate
    [sharedManager loadObjectsAtResourcePath:self.resourcePath delegate:self];

    //creates an error
    NSError *error = [NSError errorWithDomain:@"world" code:200 userInfo:nil];

    // Setting timeout here. goto failure 
    nTimer = [NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(didTimeout) userInfo:nil repeats:NO];
}


- (void)didTimeout {
    NSLog(@"Error");
}

// handles failure
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
    [self didTimeout];
}

// handles success
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
    //don't forget to invalidate the time or else you'll get errors even when successful
    [nTimer invalidate];
}

You can of course extend this to be more flexible if necessary, but this seems to cover what you asked.

迷荒 2024-12-04 17:40:55

对RestKit不熟悉,但我首先想到的是,将计划选择器设置为调用验证方法而不是did失败方法要好得多,让验证方法检查以查看是否已收到有效响应定义的时间,如果还没有,则取消请求等并调用失败。

虽然我不熟悉 RestKit,但我熟悉使用 NSURLRequests,并且我知道当您发出请求以生成超时失败时可以定义超时 - 不知道这是否有帮助......

Not familiar with RestKit but the first thing I think of is that it would be far better to set the scheduled selector to call a verfication method rather than the did fail method, have the verification method check to see if a valid response has been received within the defined time and if it hasn't then cancel the request etc and call the fail.

Whilst I am not familiar with RestKit I am familiar with using NSURLRequests and I know it's possible to define a timeout when you issue the request to generate a timeout failure - don't know if that helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文