【问题标题】:How to run task every 5 seconds in background thread without block UI in ios如何在后台线程中每 5 秒运行一次任务,而不会在 ios 中阻塞 UI
【发布时间】:2014-09-07 11:09:46
【问题描述】:

我正在开发聊天应用程序,在聊天屏幕中,我的应用程序需要每 5 秒检查一次新消息。我想在后台线程中执行此操作,这样我的应用程序就不会被阻止 UI。我尝试了下面的代码,但是当用户输入消息时,UI 似乎被阻止了。此外,当用户退出聊天屏幕时,我无法触发此任务。

这是我尝试过的代码:

getLatestMessagesWithInterval()viewwillAppear() 中被调用

-(void) getLatestMessagesWithInterval
{

    NSLog(@"GET MESSAGE INTERVAL");
    [self retrieveLatestChatMessages];

    // Call this method again using GCD
    dispatch_queue_t q_background = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
    double delayInSeconds = 5.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, q_background, ^(void){
        [self getLatestMessagesWithInterval];
    });

}




 -(void) retrieveLatestChatMessages
{
    if([[NSUserDefaults standardUserDefaults] boolForKey:@"LoggedIn"]) {
        NSDictionary * userDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"SessionDictionary"];
        .....

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            [[LSDataManager sharedDataManager] getLatestMessagesWithAuthKey:authenKey andLimit:limit withBlock:^ (NSDictionary* responseDict)
             {
                 if (responseDict) {
                     [self loadDataFromServer:responseDict];

                     NSArray* lastMessageArray= nil;

                     //filter message data
                     if (self.MessagesArray.count >0) {

                        //perform data

                             self.TempdataSource = [[[ContentManager sharedManager] generateConversation:lastMessageArray withSenderID:self.senderID] mutableCopy];

                             //compare 2 arrays
                             if ([self.TempdataSource count] == [self.dataSource count]) {
                                 NSLog(@"both are same");
                             }
                             else{
                                 NSLog(@"both are different");
                                 self.dataSource = [self.TempdataSource mutableCopy];


                                 dispatch_async(dispatch_get_main_queue(), ^(){
                                     //Add method, task you want perform on mainQueue
                                     [self refreshMessages];
                                 });

                             }

                         }
                     }
                 }
             }];
        });
    }

}

我已致电retrieveLatestChatMessages() 从服务器获取消息。服务器将在块中返回。执行数据后,我在主线程中重新加载 tableview。请。帮我纠正它。提前致谢。

【问题讨论】:

    标签: ios objective-c multithreading background nstimer


    【解决方案1】:

    与 API 进行异步通信,例如使用 AFNetworking 并且您的 UI 不会被阻塞,您也可以设置每秒计时器并调用类似的东西

    if (numberOfSeconds % 5 == 0) {
         numberOfSeconds = 0;
         retrieveLatestChatMessages()
    }
    

    【讨论】:

    • 我使用 AFnetworking 与 API 进行通信。我的问题是当我运行上面的代码时,用户界面似乎被阻止了。
    【解决方案2】:

    两个选项

    选项 1

    使用 NSTimer 每 5 秒调用一次该方法。

    我建议不要使用它。

    选项 2

    有一个基于 TCP 的应用程序用于聊天消息。

    这将是聊天消息的最佳选择。


    编辑 1

    了解如何根据您的语言使用 TCP。对于 iOS TCP,请点击链接

    http://www.tekritisoftware.com/sites/default/files/Socket_Programing_for_IOS.pdf

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-16
    • 2019-07-28
    • 1970-01-01
    • 2014-10-20
    • 2013-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多