iOS获取时间间隔的三种方式
1.NSDate的timeIntervalSinceDate方法。
假设25秒内轮询(递归调用)某个接口:获取时间间隔小于25秒
+(void)queryData:(NSDictionary *)params withQuerySuccessComplete:(void (^)(BOOL success,DYRequestModel *model,NSError *error))complete
{
[DYTransferManager shared].querySuccessBlock = complete;
[DYTransferManager shared].beforeDate = [NSDate date];
[self sendQueryRequestParams:params];
}
+(void)sendQueryRequestParams:(NSDictionary *)params{
if (params == nil) {
return;
}
DYRequest *request = [DYTransferRequestFactory queryResultRequestWithParams:params];
[request.signal subscribeNext:^(id x) {
DYRequestModel *requestModel = (DYRequestModel *)x;
[DYTransferManager shared].nowDate = [NSDate date];
int requestInterval = (int)ceil([[DYTransferManager shared].nowDate timeIntervalSinceDate:[DYTransferManager shared].beforeDate]);
if ([requestModel.data[@"status"] isEqualToString:@"1"] && requestInterval <= 25) {
[self sendQueryRequestParams:params];
}
else
{
if([DYTransferManager shared].querySuccessBlock) [DYTransferManager shared].querySuccessBlock(YES,requestModel,nil);
}
} error:^(NSError *error) {
[DYTransferManager shared].nowDate = [NSDate date];
int requestInterval = (int)ceil([[DYTransferManager shared].nowDate timeIntervalSinceDate:[DYTransferManager shared].beforeDate]);
if(requestInterval <= 25)
{
[self sendQueryRequestParams:params];
}
else
{
if([DYTransferManager shared].querySuccessBlock) [DYTransferManager shared].querySuccessBlock(NO,nil,error);
}
}];
}
2.CFTimeInterval的CACurrentMediaTime()方法
static CFTimeInterval currentTimer = 0;
+(void)queryData:(NSDictionary *)params withQuerySuccessComplete:(void (^)(BOOL success,DYRequestModel *model,NSError *error))complete
{
[DYTransferManager shared].querySuccessBlock = complete;
[self sendQueryRequestParams:params];
}
+(void)sendQueryRequestParams:(NSDictionary *)params{
if (params == nil) {
return;
}
if (currentTimer == 0) {
currentTimer = CACurrentMediaTime();
}
DYRequest *request = [DYTransferRequestFactory queryResultRequestWithParams:params];
[request.signal subscribeNext:^(id x) {
DYRequestModel *requestModel = (DYRequestModel *)x;
CFTimeInterval nowTimer = CACurrentMediaTime();
if ([requestModel.data[@"status"] isEqualToString:@"1"] && nowTimer - currentTimer <= 25) {
[self sendQueryRequestParams:params];
}
else
{
if([DYTransferManager shared].querySuccessBlock) [DYTransferManager shared].querySuccessBlock(YES,requestModel,nil);
}
} error:^(NSError *error) {
CFTimeInterval nowtimer = CACurrentMediaTime();
if(nowtimer - currentTimer <= 25)
{
[self sendQueryRequestParams:params];
}
else
{
if([DYTransferManager shared].querySuccessBlock) [DYTransferManager shared].querySuccessBlock(NO,nil,error);
}
}];
}
3.CFAbsoluteTime的CFAbsoluteTimeGetCurrent()方法
static CFAbsoluteTime currentTimer = 0;
+(void)queryData:(NSDictionary *)params withQuerySuccessComplete:(void (^)(BOOL success,DYRequestModel *model,NSError *error))complete
{
[DYTransferManager shared].querySuccessBlock = complete;
[self sendQueryRequestParams:params];
}
+(void)sendQueryRequestParams:(NSDictionary *)params{
if (params == nil) {
return;
}
if (currentTimer == 0) {
currentTimer = CFAbsoluteTimeGetCurrent();
}
DYRequest *request = [DYTransferRequestFactory queryResultRequestWithParams:params];
[request.signal subscribeNext:^(id x) {
DYRequestModel *requestModel = (DYRequestModel *)x;
CFAbsoluteTime nowTimer = CFAbsoluteTimeGetCurrent();
if ([requestModel.data[@"status"] isEqualToString:@"1"] && nowTimer - currentTimer <= 25) {
[self sendQueryRequestParams:params];
}
else
{
if([DYTransferManager shared].querySuccessBlock) [DYTransferManager shared].querySuccessBlock(YES,requestModel,nil);
}
} error:^(NSError *error) {
CFAbsoluteTime nowtimer = CFAbsoluteTimeGetCurrent();
if(nowtimer - currentTimer <= 25)
{
[self sendQueryRequestParams:params];
}
else
{
if([DYTransferManager shared].querySuccessBlock) [DYTransferManager shared].querySuccessBlock(NO,nil,error);
}
}];
}