我以前從來沒有碰過線程,並且從外觀上看,我可能不得不深入研究它們,但是我想知道下面的問題是否有更簡單的解決方案。使用線程等待另一個類的方法執行,還是有一個更簡單的方法?
我基本上在UITableView
顯示用戶的Facebook朋友列表。我有一個FacebookWrapper
類,其中包含我所有的Facebook方法,以及一個FacebookViewController
,它具有各種按鈕/選項。最後,有一個FriendsViewController
顯示朋友列表。用戶可以通過按FacebookViewController
中的相關按鈕來選擇顯示男性朋友或女性朋友的列表。
如果他們碰到男性朋友,例如我使用FBL創建查詢字符串,我將其添加到字典中並用於返回男性朋友列表(全部在包裝中)。
[facebook requestWithMethodName:@"fql.query" andParams:dictionary andHttpMethod:@"GET" andDelegate:self];
包裝類中的委託方法處理返回的FBRequest
。我有以下的方法來確定發生了什麼事的時候:
- (void)requestLoading:(FBRequest *)request {
NSLog(@"Loading...");
}
- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Response received");
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
NSLog(@"Request failed");
}
- (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data {
NSLog(@"Request did load raw response");
}
然而,當我打的「男性朋友」按鈕例如,我要等到響應已推FriendsViewController
和顯示列表之前已經產生。就目前而言,FriendsVC在請求返回列表之前被推送,因此我得到一個空白列表。在刷新表格並顯示結果之前,我必須等待「Request did load raw response」。目前在FacebookViewController
調用和顯示好友列表的方法是這樣的:
- (IBAction)friendsButtonPressed:(id)sender {
int selectionTag = [sender tag];
//Get access to the instance of the wrapper in the app delegate (since it needs to remain refereced after app exits to log in and returns
MiniBfAppDelegate *appDelegate = (MiniBfAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.facebookWrapper loadListDataFor:selectionTag];
//Initialise the friends view controller
FriendsViewController *friendsViewController = [[FriendsViewController alloc] init];
//NEED TO WAIT UNTIL LISTDATA IS RETURNED?
//Give the friends view controller the list data
friendsViewController.listData = appDelegate.facebookWrapper.friendsList;
//Push the friends view controller
friendsViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self.navigationController pushViewController:friendsViewController animated:YES];
[friendsViewController release];
}
那麼理想,我想顯示加載符號,而我等待包裝返回列表,一旦確實,推送FriendsViewController,以便立即填充它。那麼我需要使用線程還是有更簡單的方法?
也許這是回答我自己的問題,也可能是我沒有掌握一個基本概念在這裏,但除了在我的App Delegate中創建FriendsVC實例,有沒有一種方法可以發送當前實例來自Wrapper類的消息?所以我可以在包裝的request: didLoadRawResponse:
方法中告訴它refresh
,即生成列表的時候。
看起來很有希望 - 我會放棄它,謝謝 – Smikey 2012-01-18 16:12:34
那麼,幾乎工作。它現在會在加載FriendsVC之前等待通知,但它仍會首次加載空白......直到我再次返回並再次按下按鈕。我也會在輸出中看到這些錯誤:嵌套的推動動畫會導致導航欄損壞,導致處於意外狀態的導航轉換。導航欄子視圖樹可能會損壞,對於,不平衡調用開始/結束外觀轉換。正如它所描述的那樣,導航欄似乎重複了一遍。這是否與通知有關? –
Smikey
2012-01-18 17:07:28
其實我似乎已刪除這些錯誤通過刪除觀察者FriendsVC加載時([[NSNotificationCenter defaultCenter] removeObserver:self name:@「ListReady」object:nil];)它仍然不會加載列表第一次雖然。 ..將繼續調查,但它看起來不錯,謝謝。 – Smikey 2012-01-18 17:24:13