У меня есть два запроса SL:как уволить UIAlertView когда данные завершает загрузку из API Твиттера с помощью SL просит
- (void)viewDidLoad
{
#pragma alertView
// load an alert to indicate that the app is loading from the internet
loadingAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Updating" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
#pragma activity indicator
// create an activity indicator and dump it into the alert
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)];
progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[loadingAlert addSubview:progress];
[progress startAnimating];
// display alert
[loadingAlert show];
// load the nib customized cell
UINib* twitterCellNib = [UINib nibWithNibName:@"TwitterViewCell" bundle:nil];
if (twitterCellNib != nil) {
[tweetTableView registerNib:twitterCellNib forCellReuseIdentifier:@"TwitterCell"];
}
// all tweeter operations need to go through this filter
// create an account-store object
ACAccountStore *accountStore = [[ACAccountStore alloc]init];
if (accountStore != nil) {
// create an account type
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
if (accountType != nil) {
// request access (on device pop-up) on sim. permanent
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
// code that needs to exect if access is granted here
// get a list with all the accounts in the device/sim
NSArray *twitterAccount = [accountStore accountsWithAccountType:accountType];
if (twitterAccount != nil) {
ACAccount *workingAccount = [twitterAccount objectAtIndex:0];
if (workingAccount != nil) {
// username of the account
NSString *userName = workingAccount.username;
// create SL request for user profile info ______________________________
NSMutableString* userProfileRequest = [[NSMutableString alloc] initWithString:@"https://api.twitter.com/1.1/users/show.json?screen_name="];
[userProfileRequest appendString:userName];
// create an SL request for user information
SLRequest *requestUserInfo = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:userProfileRequest] parameters:nil];
if (requestUserInfo != nil) {
[requestUserInfo setAccount:workingAccount];
// perform request
[requestUserInfo performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
// check response from the server
NSInteger codeServer = [urlResponse statusCode];
if (codeServer == 200) {
// catch the user info from the api
userProfileArray = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
if (userProfileArray != nil) {
[loadingAlert dismissWithClickedButtonIndex:0 animated:true];
}
}
}];
}
// create a string type url
NSString *userTimeString = @"https://api.twitter.com/1.1/statuses/user_timeline.json";
// create SL request for time-line ____________________________
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:userTimeString] parameters:nil];
if (request != nil) {
// user needs to be logged in
[request setAccount:workingAccount];
// perform request
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
// check the response from the server
NSInteger responseCode = [urlResponse statusCode];
if (responseCode == 200) {
// we are good to go
// catch the feed
twitterFeed = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
if (twitterFeed != nil) {
[tweetTableView reloadData];
}
}
else {
NSLog(@"Debugging Error: <the server response is not 200>");
}
}];
}
}
}
}
else {
// debugging: access granted error goes here
NSLog(@"User did not grant access");
}
}];
}
}
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
Я хотел бы знать, если есть способ, чтобы отклонить UIAlertView
после всех информация загружается в мой стол. Я заглянул в заголовки Social Framework
, но я не могу найти какой-либо метод (что-то вроде: SLRequestDidFinish
или didCompleted
).
спасибо за ваш быстрый ответ. Я попытался добавить [loadAlert rejectWithClickedButtonIndex: 0 animated: true]; в обработчик, но таблица по-прежнему показывает данные не менее 3-5 секунд после отклонения UIAlert. Итак, в моем случае обработчик - это весь код, следующий за комментарием «Проверить ответ с сервера». Кажется, я что-то упускаю. – Nactus
Где ваш код проводит время? Может быть, что '[tweetTableView reloadData];' занимает 3-5 секунд? Вы вызываете 'rejectWithClickedButtonIndex' и' reloadData' из того же обработчика (они находятся в разных обработчиках кода, который вы показывали)? – dmitri
В соответствии с приведенным выше кодом 'reloadData' вызывается из блока обработчика' request', который не выполняется в основном потоке. Вероятно, поэтому данные забирают 5 секунд. –