2012-01-18 39 views
2

我以前從來沒有碰過線程,並且從外觀上看,我可能不得不深入研究它們,但是我想知道下面的問題是否有更簡單的解決方案。使用線程等待另一個類的方法執行,還是有一個更簡單的方法?

我基本上在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,即生成列表的時候。

回答

2

我不確定我是否理解你的問題,但你可能想看看NSNotificationCenter。你可以用它來發送郵件到您的控制器,這裏是觸發通知的示例:

[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotificationName" 
                 object:self 
                 userInfo:[NSDictionary dictionaryWithObject:XXXX forKey:@"someObject"]]; 

正如你所看到的,你甚至可以將參數傳遞給它。而在目標控制器中,您只需註冊觀察者即可回覆這些消息:

[[NSNotificationCenter defaultCenter] addObserver: self 
              selector: @selector(yourMethodHere:) 
               name: @"myNotificationName" 
               object: nil]; 

此致!

+0

看起來很有希望 - 我會放棄它,謝謝 – Smikey 2012-01-18 16:12:34

+0

那麼,幾乎工作。它現在會在加載FriendsVC之前等待通知,但它仍會首次加載空白......直到我再次返回並再次按下按鈕。我也會在輸出中看到這些錯誤:嵌套的推動動畫會導致導航欄損壞,導致處於意外狀態的導航轉換。導航欄子視圖樹可能會損壞,對於,不平衡調用開始/結束外觀轉換。正如它所描述的那樣,導航欄似乎重複了一遍。這是否與通知有關? – Smikey 2012-01-18 17:07:28

+0

其實我似乎已刪除這些錯誤通過刪除觀察者FriendsVC加載時([[NSNotificationCenter defaultCenter] removeObserver:self name:@「ListReady」object:nil];)它仍然不會加載列表第一次雖然。 ..將繼續調查,但它看起來不錯,謝謝。 – Smikey 2012-01-18 17:24:13

2

這很簡單。把你的VC加載代碼放入didReceiveResponce方法中。並在didFailWithError中顯示UIAlertView。

- (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"Response received"); 

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

除了包含didReceiveResponse方法的包裝類沒有導航控制器對其推送FriendsVC ...? – Smikey 2012-01-18 16:10:30

+0

還是有辦法從包裝類的實例訪問當前的導航控制器? – Smikey 2012-01-18 16:37:35

+0

這是你的包裝類。所以你可以在這個類中添加導航控制器的屬性。並在FriendsVC中創建包裝類時填充它。 – 2012-01-19 06:51:53

相關問題