HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在iOS中定期在后台线程中执行任务大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我是iOS中的新手我正在研究应用程序需要运行一个任务,从后台线程中的服务器获取数据,因为我不想在主线程中锁定UI.这个任务需要很长时间,我尝试使用NSTimer,但它仍然锁定UI.我的任务是检查聊天屏幕中的新消息,我需要每5秒调用一次这个任务.如果我使用NSTimer,当输入文本时,文本似乎在此任务执行时冻结一会儿.有没有办法处理这个任务没有锁UI.请给我一些建议.非常感谢.

==更新代码==

- (void)performBACkgroundTask
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{
        //Do BACkground work

        if([[NSUserDefaults standardUserDefaults] boolForKey:@"LoggedIn"]) {
            NSDictionary * userDictionary = [[NSUserDefaults standardUserDefaults] DictionaryForKey:@"SessionDictionary"];

            NSString *authenKey= [userDictionary valueForKey:@"authToken"];

            NSString* limit = @"1000";

            [[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) {

                         if (!self.isSeller) {

                             lastmessageArray = [self filtermessageData:self.messagesArray withProductID:self.productID withSellerID:self.receiverID withBuyerID:self.senderID];
                         }
                         else
                         {
                             lastmessageArray = [self filtermessageData:self.messagesArray withProductID:self.productID withSellerID:self.senderID withBuyerID:self.receiverID];
                         }

                         NSLog(@"filter array %@",lastmessageArray);

                         if([lastmessageArray count] >0){
                             //[self loadmessages:lastmessageArray];
                             if (self.TemPDAtasource == nil) {
                                 self.TemPDAtasource = [NSMutableArray array];
                             }
                             else
                             {
                                 [self.TemPDAtasource removeAllObjects];
                             }

                             self.TemPDAtasource = [[[ContentManager sharedManager] generateConversation:lastmessageArray withSenderID:self.senderID] mutableCopy];

                         }
                     }
                 }
             }];
        }


        dispatch_async(dispatch_get_main_queue(),^{
            //update UI

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

                [self refreshmessages];
            }

        });
    });
}

解决方法

使用NSTimer调度任务确实是正确的方法.您只需要确保在背景线程上运行沉重的非UI代码.这是一个例子
- (void)viewDidLoad {
    [super viewDidLoad];    
    [self startTimedTask];
}

- (void)startTimedTask
{
    NSTimer *fiveSecondTimer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self SELEctor:@SELEctor(performBACkgroundTask) userInfo:nil repeats:YES];
}

- (void)performBACkgroundTask
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,^{
        //Do BACkground work
        dispatch_async(dispatch_get_main_queue(),^{
            //update UI
        });
    });
}

大佬总结

以上是大佬教程为你收集整理的在iOS中定期在后台线程中执行任务全部内容,希望文章能够帮你解决在iOS中定期在后台线程中执行任务所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。