【问题标题】:How is it better to wait an asynchronous method to be finished in iPhone app?在 iPhone 应用程序中等待异步方法完成如何更好?
【发布时间】:2010-12-05 19:30:34
【问题描述】:

大家。 我想了解,当异步方法具有“didFinish:@selector(SEL)”参数时,我应该如何处理情况。 我的代码示例是:

//
// Authentication check
- ( void )authenticationSuccess: ( GDataServiceTicket* ) ticket
         authenticatedWithError: ( NSError* ) error {

    if ( error == nil )
    {
        NSLog( @"authentication success" );
    }
    else 
    {
        NSLog( @"authentication error" );
    }
}
//

- ( void ) fetchFeedOfSpreadsheets {

    //create and authenticate to a google spreadsheet service 
    if ( !(mService) )
    {
        GDataServiceGoogleSpreadsheet *service = [self spreadsheetService];
        [mService autorelease];
        mService = [service retain];    
    }

    // check autentication success ( invoke "authenticationSuccess" method for debug success & error )
    [mService authenticateWithDelegate: self
               didAuthenticateSelector:@selector(authenticationSuccess:
                                                 authenticatedWithError:) ];


    // HERE I WANT TO MAKE A PAUSE AND WHAIT THE RESULT, EITHER I AUTHENTICATED OR NOT
    // AND MAKE AN "IF" STATEMENT TO CONTINTUE WORKING ON SERVER, OR RETURN ERROR


    //fetch retrieves the feed of spreadsheets entries 
    NSURL *feedURL = [ NSURL URLWithString: kGDataGoogleSpreadsheetsPrivateFullFeed ];
    GDataServiceTicket *ticket;
    ticket = [mService fetchFeedWithURL: feedURL
                               delegate: self
                      didFinishSelector: @selector(spreadsheetsTicket:finishedWithFeed:
                                                   error: ) ];

    // HERE I WANT TO WAIT SECOND TIME. I WANT "spreadsheetsTicket:        
    // finishedWithFeed:error:" TO PROCCEED ERROR AND PUT A FEED IN SOME NSARRAY OBJECT
    // AND AFTER THAT I WANT TO WORK WITH THAT NSARRAY RIGHT HERE
}

我很清楚,我可以将我想要的代码推送到“authenticationSuccess”方法部分的末尾,但也很清楚,这是一种错误的解决问题的方法。有很多这样的情况,我用选择器参数调用异步方法,我想找到一个解决方案,为我提供灵活的代码编写。

提前致谢。

【问题讨论】:

    标签: objective-c multithreading gdata-api


    【解决方案1】:

    Objective-C 的标准做法是将验证后要执行的代码放在authenticationSucess: 方法中。你可能不喜欢它,但这就是生活。

    很多人和你有同样的抱怨,所以 在 iOS 4 及更高版本上,有一些称为块的东西,它允许您在启动身份验证的方法中编写身份验证后要执行的代码,如

    [mService authenticateAndExecute:^{ 
                     code to be executed when successfully authenticated ;
              }            whenError:^{
                     code to be executed when authentication failed;
              } ]; 
    

    但在这种情况下,您需要修改 API,这可以通过使用类别来实现。请参阅 Mike Ash 的this blog post。他在同一个博客的区块上还有很多其他帖子,这些帖子也很有启发性。

    【讨论】:

    • 但是用 blocs 修改 gdata-objectivec-client 是一种更难的方法,然后使这个冒泡逻辑:/ ..so.. 我想我别无选择,并将代码插入“didFinish:@选择器”?
    【解决方案2】:

    如果您要使用异步工作的库(因此不会阻塞您的 UI),您应该有充分的理由尝试强制它同步工作。

    您应该在authenticationSuccess:authenticatedWithError: 方法结束时检查身份验证错误,如果成功则从那里调用下一个请求。同样,在您的spreadsheetsTicket:finishedWithFeed:error: 中检查错误,如果没有则继续处理。以单独的方法继续工作可能是更好的设计,但这取决于您。

    您是否有特定原因要以同步方式使用 GData API?

    【讨论】:

      猜你喜欢
      • 2013-02-15
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 2014-08-09
      • 1970-01-01
      相关资源
      最近更新 更多