异步任务可以运行循环吗? (在 iOS 中)
Can an asynchronous task be run in a loop? (in iOS)
基本上我使用 firebase 来查询用户的 'status' 属性 并在 do/while 循环中这样做。
如果状态 属性 是空闲的,那么我想打破循环并继续该方法的其余部分。如果状态 属性 不是免费的,那么我想再次查询 firebase 以获取新用户,直到找到免费用户。
我的 firebase 代码在循环外运行良好,但在循环内似乎没有被调用。这是循环:
__block uint32_t rando;
self.freedom = @"about to check";
do {
//check if free
[self checkIfFree:^(BOOL finished) {
if (finished) {
if ([self.freedom isEqualToString:@"free"]) {
//should break loop here
}
else if ([self.freedom isEqualToString:@"matched"]){
//get another user
do {
//picking another random user from array
rando = arc4random_uniform(arraycount);
}
while (rando == randomIndex && rando == [self.randString intValue]);
self.randString = [NSString stringWithFormat:@"%u", rando];
[users removeAllObjects];
[users addObject:[usersArray objectAtIndex:rando]];
self.freeUser = [users objectAtIndex:0];
//should repeat check here but doesn't work
}
else{
NSLog(@"error!");
}
}
else{
NSLog(@"not finished the checking yet");
}
}];
} while (![self.freedom isEqual: @"free"]);
这是我的 firebase 代码:
-(void)checkIfFree:(myCompletion) compblock{
self.freeUserFB = [[Firebase alloc] initWithUrl:[NSString stringWithFormat: @"https://skipchat.firebaseio.com/users/%@", self.freeUser.objectId]];
[self.freeUserFB observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot)
{
self.otherStatus = snapshot.value[@"status"];
NSLog(@"snapshot info %@", snapshot.value);
if ([self.otherStatus isEqualToString:@"free"]) {
self.userIsFree = YES;
self.freedom = @"free";
}
else{
self.userIsFree = NO;
self.freedom = @"matched";
}
compblock(YES);
}];
}
谢谢!
我不确定我是否正确理解了你的问题。
如果你想再次 运行 你的完成代码直到匹配某些条件(在这种情况下 [self.freedom isEqualToString:@"free"]
)你可以执行以下操作(删除 do - while):
void( ^ myResponseBlock)(BOOL finished) = ^ void(BOOL finished) {
if (finished) {
if ([self.freedom isEqualToString: @"free"]) {
return;
} else if ([self.freedom isEqualToString: @"matched"]) {
//get another user
do {
//picking another random user from array
rando = arc4random_uniform(arraycount);
}
while (rando == randomIndex && rando == [self.randString intValue]);
self.randString = [NSString stringWithFormat: @"%u", rando];
[users removeAllObjects];
[users addObject:usersArray[rando]];
self.freeUser = users.firstObject;
// Schedule another async check
[self checkIfFree: myResponseBlock];
} else {
NSLog(@"error!");
}
} else {
NSLog(@"not finished the checking yet");
}
};
[self checkIfFree: myResponseBlock];
基本上我使用 firebase 来查询用户的 'status' 属性 并在 do/while 循环中这样做。
如果状态 属性 是空闲的,那么我想打破循环并继续该方法的其余部分。如果状态 属性 不是免费的,那么我想再次查询 firebase 以获取新用户,直到找到免费用户。
我的 firebase 代码在循环外运行良好,但在循环内似乎没有被调用。这是循环:
__block uint32_t rando;
self.freedom = @"about to check";
do {
//check if free
[self checkIfFree:^(BOOL finished) {
if (finished) {
if ([self.freedom isEqualToString:@"free"]) {
//should break loop here
}
else if ([self.freedom isEqualToString:@"matched"]){
//get another user
do {
//picking another random user from array
rando = arc4random_uniform(arraycount);
}
while (rando == randomIndex && rando == [self.randString intValue]);
self.randString = [NSString stringWithFormat:@"%u", rando];
[users removeAllObjects];
[users addObject:[usersArray objectAtIndex:rando]];
self.freeUser = [users objectAtIndex:0];
//should repeat check here but doesn't work
}
else{
NSLog(@"error!");
}
}
else{
NSLog(@"not finished the checking yet");
}
}];
} while (![self.freedom isEqual: @"free"]);
这是我的 firebase 代码:
-(void)checkIfFree:(myCompletion) compblock{
self.freeUserFB = [[Firebase alloc] initWithUrl:[NSString stringWithFormat: @"https://skipchat.firebaseio.com/users/%@", self.freeUser.objectId]];
[self.freeUserFB observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot)
{
self.otherStatus = snapshot.value[@"status"];
NSLog(@"snapshot info %@", snapshot.value);
if ([self.otherStatus isEqualToString:@"free"]) {
self.userIsFree = YES;
self.freedom = @"free";
}
else{
self.userIsFree = NO;
self.freedom = @"matched";
}
compblock(YES);
}];
}
谢谢!
我不确定我是否正确理解了你的问题。
如果你想再次 运行 你的完成代码直到匹配某些条件(在这种情况下 [self.freedom isEqualToString:@"free"]
)你可以执行以下操作(删除 do - while):
void( ^ myResponseBlock)(BOOL finished) = ^ void(BOOL finished) {
if (finished) {
if ([self.freedom isEqualToString: @"free"]) {
return;
} else if ([self.freedom isEqualToString: @"matched"]) {
//get another user
do {
//picking another random user from array
rando = arc4random_uniform(arraycount);
}
while (rando == randomIndex && rando == [self.randString intValue]);
self.randString = [NSString stringWithFormat: @"%u", rando];
[users removeAllObjects];
[users addObject:usersArray[rando]];
self.freeUser = users.firstObject;
// Schedule another async check
[self checkIfFree: myResponseBlock];
} else {
NSLog(@"error!");
}
} else {
NSLog(@"not finished the checking yet");
}
};
[self checkIfFree: myResponseBlock];