2015-02-05 122 views
0

基本上我使用firebase來查詢用戶的'狀態'屬性,並在do/while循環中這樣做。異步任務可以在循環中運行嗎? (在iOS中)

如果狀態屬性是空閒的,那麼我想打破循環並繼續執行其餘的方法。如果狀態屬性不是空閒的,那麼我想再次查詢一個新用戶的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"]); 

這是我的火力代碼:

-(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); 
}]; 
} 

謝謝!

+0

旁註:你應該使用枚舉或最小命名的字符串常量,而不是像'self.freedom = @「free」;''self.freedom = @「」matched「;'' – 2015-02-05 23:29:41

+0

爲什麼不添加觀察者來查看狀態更改時間:FEventTypeChildChanged。那麼你可以檢查用戶狀態並將它們添加到免費用戶數組中,如果他們是免費的? – 2015-02-06 02:53:57

+0

我不認爲這會做這項工作,因爲我想在該時間點的價值,所以當前用戶可以立即與另一個免費用戶匹配。查詢的用戶類型也根據用戶的當前偏好(如tinder)進行更改,因此這需要查看我猜測的所有用戶? 另外,我仍然會遇到如果免費用戶數組是空的問題,我將不得不再次調用異步的firebase方法我相信 – ryder 2015-02-07 00:28:54

回答

0

我不知道我是否正確理解你的問題。 如果您想再次運行完成代碼,直到某些條件匹配(在這種情況下[self.freedom isEqualToString:@"free"]),你可以做以下(去除DO - 一會兒):

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]; 
+0

感謝您的迴應,我會盡快嘗試這個。 只是想知道爲什麼這應該工作,爲什麼do/while循環不會? 謝謝 – ryder 2015-02-07 00:33:27

+0

我在'checkIfFree'完成塊的最後一行發現錯誤'bad access':'completion(YES)' – ryder 2015-02-07 10:32:00

+0

得到它的工作,感謝您的幫助!看到解決'不良訪問'錯誤http://stackoverflow.com/questions/28388668/bad-access-after-block-within-a-block-is-called-ios – ryder 2015-02-08 01:14:46

相關問題