大佬教程收集整理的这篇文章主要介绍了在iOS中定期在后台线程中执行任务,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
==更新代码==
- (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]; } }); }); }
- (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,请注明来意。