如何在applicationDidEnterBackground

如何在applicationDidEnterBackground

本文介绍了如何在applicationDidEnterBackground中调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在中调用一个函数applicationDidEnterBackground 这个函数在其他控制器中定义。

I want to call a function in applicationDidEnterBackground this function is defined in other controller.

我做了一个访问它的对象,但似乎函数在被调用时被杀死。

I have made an object to access it but it seems that function is getting killed when called.

这是它基本上计算距离并发布通知的函数

Here is the function it basically calculates the distance and postes a notification

 -(void)calculateDistance
{

    for (NSMutableDictionary *obj in placeName) {
        CLLocation *userLocation = [[AppHelper appDelegate] mLatestLocation];
        CLLocation *annotation1 = [[CLLocation alloc] initWithLatitude:[[obj objectForKey:@"Lat"]doubleValue] longitude:[[obj objectForKey:@"long"]doubleValue]];

        CGFloat distanceTemp = [annotation1 getDistanceFrom:userLocation];

        [obj setObject:[NSNumber numberWithFloat:distanceTemp] forKey:@"distance"];
        [annotation1 release];
    }


    if ([placeName count])
    {
        NSArray *sortedArray=[placeName sortedArrayUsingFunction:intSort context:NULL];
        self.placeName = [NSMutableArray arrayWithArray:sortedArray];
        NSMutableArray *arrayTemp = [[NSMutableArray alloc] initWithArray:placeName];


        for (int i =0; i < [placeName count]; i++)
        {
            //                  NSArray *sortedArray=[placeName sortedArrayUsingFunction:intSort context:NULL];

            NSMutableArray *tempArray = [sortedArray objectAtIndex:i];
            //DLog(@"sortedArray%@", sortedArray);8=
            NSNumber *DistanceNum = [tempArray objectForKey:@"distance"];
            NSLog(@"distance%@:::",DistanceNum);
            NSInteger intDistance = (int)[DistanceNum floatValue];

            if(intDistance<500)
            {
                NSLog(@"ho gaya bhai");
                NSString *notifications =@"Yes";
                [[AppHelper mDataManager] setObject:notifications forKey:@"notifications"];

                NSLog(@"notifications:%@",notifications);
                RemindMeViewController *object = [[RemindMeViewController alloc] initWithNibName:@"RemindMeViewController" bundle:nil];
                //  RemindMeViewController *object=[[RemindMeViewController alloc]initWithNibName];

                NSLog(@"notifications set");
                [object scheduleNotification];
            }
            else
            {
                // [arrayTemp removeObjectAtIndex:i];
            }
        }

        //after for loop is ended
        self.placeName= arrayTemp;
        DLog(@"remaining",arrayTemp);

        [arrayTemp release];
        [mTableView reloadData];
    }
}


推荐答案

如何你的功能是完整的吗?您只有5秒钟在 applicationDidEnterBackground:中执行任务并返回。

How long is your function taking to complete? You only have 5 seconds to perform tasks in applicationDidEnterBackground: and return.

来自Apple的:

你应该执行与调整用户界面相关的任何任务
在此方法退出之前,但其他任务(例如保存状态)应根据需要将
移动到并发调度队列或辅助线程。
因为你可能在
applicationDidEnterBackground中启动任何后台任务:直到
退出该方法之后才会运行,你应该在
启动这些任务之前请求额外的后台执行时间。换句话说,首先调用
beginBackgroundTaskWithExpirationHandler:然后在
调度队列或辅助线程上运行任务。

You should perform any tasks relating to adjusting your user interface before this method exits but other tasks (such as saving state) should be moved to a concurrent dispatch queue or secondary thread as needed. Because it's likely any background tasks you start in applicationDidEnterBackground: will not run until after that method exits, you should request additional background execution time before starting those tasks. In other words, first call beginBackgroundTaskWithExpirationHandler: and then run the task on a dispatch queue or secondary thread.

这篇关于如何在applicationDidEnterBackground中调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 21:43