我有一個類,我正在使用的目標C中的Facebook與我的iPhone應用程序進行交互。使用while循環拖動,直到我得到一些數據返回
GWFacebook.h
@interface GWFacebook : Facebook <FBRequestDelegate>
{
bool query_running;
NSArray* query_result;
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error;
- (void)request:(FBRequest *)request didLoad:(id)result;
- (void)LogIn;
- (NSArray*)getAllFriends;
@end
GWFacebook.m
#import "GWFacebook.h"
@implementation GWFacebook
- (void)request:(FBRequest *)request didLoad:(id)result
{
query_result = result;
query_running = false;
}
- (NSArray*)getAllFriends
{
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"SELECT first_name, last_name, uid, pic_big FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY mutual_friend_count desc",@"query",nil];
query_running = true;
bool localbool = true;
[self requestWithMethodName: @"fql.query" andParams: params andHttpMethod: @"POST" andDelegate: self];
while(localbool)
localbool = query_running;
return query_result;
}
@end
基本上函數getAllFriends使得FB請求,並將此對象作爲請求的Facebook委託。然後調用委託(self)的函數請求didLoad,並使用變量將數據傳回給getAllFriends函數,然後將函數返回給調用它的任何函數。
它的長短之處在於,我試圖使用while循環來停止函數,直到它收到響應。在調試中,它明確指出請求的卸載函數永遠不會被facebook對象調用。它在我刪除while循環時調用...因此數據請求成功,while循環成爲問題。
我想知道是否有一種方法來拖延一個函數,直到數據被接收......我可能做的事情非常愚蠢,所以我需要一些幫助!
謝謝