使用NSOperationQueue方法解決使用者登入非同步處理

來源:互聯網
上載者:User

最近做一個項目,之前的使用者登入是同步的,點擊登入後,在登入結果還沒有返回之前所有的介面都不能操作,使用者體驗非常不好。所以使用NSOperationQueue以非同步方式處理了。

///////////////////////////.h

#import <Foundation/Foundation.h>

@interface MyTask : NSOperation {

    id _target;

    SEL _action;

}

- (id)initWithTargetAndAction:(id)theTarget action:(SEL)theAction;

@end

///////////////////////////.m檔案

- (id)initWithTargetAndAction:(id)theTarget action:(SEL)theAction {

    self = [super init];

    if (self) {

        _action = theAction;

        _target = theTarget;

    }

    return self;

}

- (void)main {

    //發送請求命令

}

- (void)recvData:(id)data {

        [_target performSelectorOnMainThread:_action withObject:nil waitUntilDone:NO];

}

// 調用地方LoginVIew.m

- (void)sendReq {

    [self startAnima];

    NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease];

    MyTask *task = [[MyTask alloc] initWithTargetAndAction:self action:@selector(didFinish:)];

    [queue addOperation:task];

}

-(void)didFinishIn:(id)data {

    [self stopAnima];

} 本執行個體主要處理重新整理進度條的顯示和隱藏的處理,同時也是登入按鈕的非同步(之前不採用非同步話,點擊按鈕後就假死狀態)。 本主要使用NSOperationQueue實現非同步處理:開始的時候建立target和action相關動作。由main函數啟動了一個子線程,相關處理在子線程中完成. 接收完成調用recvData函數。通過recvData函數通知到上層。從而實現非同步處理。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.