【问题标题】:how to dismiss a UIAlertView once the data finishes loading from Twitter's API using SL Requests使用 SL 请求从 Twitter 的 API 完成加载数据后如何关闭 UIAlertView
【发布时间】:2014-01-09 01:18:21
【问题描述】:

我有两个 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 的标题,但似乎找不到任何方法(例如:SLRequestDidFinishdidCompleted)。

【问题讨论】:

    标签: iphone objective-c cocoa-touch ios6 ios7


    【解决方案1】:

    >我似乎找不到任何方法(例如:SLRequestDidFinish 或 didCompleted)。

    这是由您传递给performRequestWithHandler 函数的处理程序提供的:

    [requestUserInfo performRequestWithHandler:handler]
    

    请求完成时调用处理程序。请注意,不能保证在任何特定线程上调用处理程序。

    您同时触发了两个请求。未定义对其处理程序的调用顺序。这可能是也可能不是问题,您只需要意识到这一点并确保您的逻辑是正确的。

    您从一个处理程序调用dismissWithClickedButtonIndex,而不是从另一个处理程序调用,后者实际上是对来自提要的数据调用。那是你真正想要的吗?

    可能的问题是您在任意线程上调用dismissWithClickedButtonIndex。我建议你在主线程上调用它。 UIKit 方法不是线程安全的(除了少数例外),因此您为更新 UI 元素所做的任何调用都必须始终从主队列中完成。例如

    dispatch_async(dispatch_get_main_queue(), ^{/*Update the UI*/});
    

    【讨论】:

    • 感谢您的及时答复。我尝试添加 [loadingAlert dismissWithClickedButtonIndex:0 animated:true];进入处理程序,但表在 UIAlert 被解除后至少 3-5 秒仍显示数据。因此,在我的情况下,处理程序是注释“检查来自服务器的响应”之后的所有代码。我想我错过了什么。
    • 您的代码在哪里花费时间?难道[tweetTableView reloadData]; 需要 3-5 秒?您是否从同一个处理程序中调用dismissWithClickedButtonIndexreloadData(在您显示的代码中它们位于不同的处理程序中)?
    • 根据上面的代码,reloadData是从request的handler block中调用的,不会在主线程上执行。这可能是数据需要 5 秒才能显示的原因。
    • 此外,即使您从同一个处理程序调用dismissWithClickedButtonIndexreloadData,您也可能不会在刷新表后解除警报框,因为这些方法的实际执行时间可能会颠倒。 reloadData 可能会在主线程上调度异步调用,而dismissWithClickedButtonIndex 可能会同步完成这项工作(除非它添加动画)。不过,这些纯粹是我的猜测。只是让你想一想。
    • @dmitri,做到了。我仍在测试该应用程序,但它似乎可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多