【发布时间】:2012-01-18 15:13:59
【问题描述】:
我以前从未接触过线程,从外观上看,我可能需要深入研究它们,但我想知道是否有更简单的解决方案来解决以下问题。
我基本上是在UITableView 中显示用户的 Facebook 好友列表。我有一个包含我所有 Facebook 方法的 FacebookWrapper 类,以及一个包含各种按钮/选项的 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 在请求返回列表之前被推送,所以我得到一个空白列表。在刷新表格并显示结果之前,我必须等待“请求确实加载了原始响应”。目前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,即在生成列表时。
【问题讨论】:
标签: objective-c multithreading facebook cocoa-touch ios4