PromiseKit "Common Patterns"文档在Swift中。
如何在Objective C中编写重试/轮询代码?
<form (ngSubmit)="send()" #loginForm="ngForm">
<div class="modal-header">
<!-- code -->
</div>
<div class="modal-body">
<!-- code -->
</div>
<div class="modal-footer">
<!-- code -->
<button class="btn btn-primary btn-lg btn-primary-light-blue" type="submit" (click)="send()">Valider la modification</button>
</div>
</form>
答案 0 :(得分:0)
由PromiseKit开发人员在Github上回答:
- (AnyPromise *) attempt:(NSUInteger) maximumRetryCount delayBeforeRetry:(NSTimeInterval) delay provider:(AnyPromise* (^)()) provide {
__block NSUInteger attempts = 0;
AnyPromise *(^attempt)() = ^{
attempts++;
return provide().catch(^(id error) {
if (attempts >= maximumRetryCount) @throw error;
return PMKAfter(delay).then(attempt);
});
};
return attempt();
}
然后,如果您有这样的方法:
- (AnyPromise *) myPromise {
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
...
resolve(nil) / resolve (error);
}];
}
致电
[self attempt:3 delayBeforeRetry:2 provider:^{ return [self myPromise]; }]